Here is a working YAML file for connection to local WPA2 wifi Network.
Create this file in /etc/netplan.
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.