Ubuntu - Networking - Netplan - Connection to Wifi WPA2

Here is a working YAML file for connection to local WPA2 wifi Network.

Create this file in /etc/netplan.

/etc/netplan/wpa2.yaml
network:
    version: 2
    wifis:
        wlan0:
             renderer: NetworkManager
             match: {}
             dhcp4: true
             access-points:
                 "wpa2 network":
                     password: "the wifi password goes here"

An assumption is that there is only one wifi device in the system. If this is not true, replace match: {} with something more specific.

The wpa2 network part is actually the SSID.

In YAML, indentation matters, so ensure that this is maintained in the YAML config file.

Run the following to enable the change:

netplan generate; 
netplan apply

and things should work.