I've posted in the forum but thought I would ask here also. So, I am new to Ubuntu Server 18.04, netplan and .yaml. In the past using Ubuntu Server 16.04 I was able to create a pretty decent shell script to setup a static IP on a server in a post-installation setup. Is it not possible to script out the network configuration now that Ubuntu Server 18.04 is using netplan? I was trying to tweak my shell script to configure the writing to the .yaml file but it seems formatting is crucial when editing a .yaml file and I was not having much luck. It seems Ansible may possibly be a solution for this, possibly? But I've yet to master Ansible. Anyone else have any luck writing a script to configure a static IP using netplan and updating the .yaml file?

  • 2
    What was the problem when you tried it? It shouldn't be a problem when using a heredoc within the shell script for instance. – Sebastian Stark Aug 13 '18 at 11:40
  • Would heredoc work with the specific formatting of the .yaml file? – t3kg33k Aug 13 '18 at 14:26
  • 1
    yes, it would work with any special text file formatting. – Sebastian Stark Aug 13 '18 at 14:28

@TygerTy Thanks for that. I forgot to post a follow up a few weeks ago. This is what I came up with that worked for me:

# Creates a backup
cp /etc/netplan/01-netcfg.yaml /etc/netplan/01-netcfg.yaml.bk_`date +%Y%m%d%H%M`
# Changes dhcp from 'yes' to 'no'
sed -i "s/dhcp4: yes/dhcp4: no/g" /etc/netplan/01-netcfg.yaml
# Retrieves the NIC information
nic=`ifconfig | awk 'NR==1{print $1}'`
# Ask for input on network configuration
read -p "Enter the static IP of the server in CIDR notation: " staticip 
read -p "Enter the IP of your gateway: " gatewayip
read -p "Enter the IP of preferred nameservers (seperated by a coma if more than one): " nameserversip
cat > /etc/netplan/01-netcfg.yaml <<EOF
  version: 2
  renderer: networkd
        - $staticip
      gateway4: $gatewayip
          addresses: [$nameserversip]
sudo netplan apply
echo "==========================="

This works for me, although I'm generating a static from the current config (don't ask why).

Just substitute the IP address for your static IP and netmask instead of dynamically looking for it like I do.

You could also change the network manager as well.

------ END_CONFIG=/etc/netplan/01-network-card.yaml
generateAndApply() { sudo netplan generate sudo netplan apply }
getInternetInfo() { local INTERNET_INFO=$( ip r | grep default ) printf "%s" "$( echo $INTERNET_INFO | cut -f$1 -d' ' )" }
#static information NAMESERVERS=("" "") NETWORK_MANAGER="NetworkManager"
# information that varies IP="$( ip r | grep kernel | cut -f9 -d' ' )" GATEWAY="$( getInternetInfo 3 )" DEVICE_NAME="$( getInternetInfo 5 )" METHOD="$( getInternetInfo 7 )" PREFIX="$( ip r | grep kernel | cut -f1 -d' ' | cut -f2 -d'/' )"
createStaticYAML() { local YAML="network:\n" YAML+=" version: 2\n" YAML+=" renderer: $NETWORK_MANAGER\n" YAML+=" ethernets:\n" YAML+=" $DEVICE_NAME:\n" YAML+=" dhcp4: no\n" YAML+=" addresses: [$IP/$PREFIX]\n" YAML+=" gateway4: $GATEWAY\n" YAML+=" nameservers:\n" YAML+=" addresses: [${NAMESERVERS[0]},${NAMESERVERS[1]}]" printf "%s" "$YAML" }
clearConfigs() { [ -f $END_CONFIG ] && sudo rm $END_CONFIG }
setYAML() { sudo echo -e "$(createStaticYAML)" > $END_CONFIG }
clearConfigs setYAML generateAndApply restartNetwork


@t3kg33k, Thanks. The bash script is running and I've modified according to my needs. However, I was wondering how this can be looped if we want to configure multiple interfaces and want those prompt multiple times using user input? For instance, I am automating this using ansible and leveraging bash using ansible 'command' module to prompt for interface name, staticip, gatewayip instead of reading in a bash script itself and then create a 01-netcfg.yaml based on these three inputs.---> So this works well if I have one interface. What I want bash to do is to first prompt, "Enter number of interfaces you want to configure (eg. 1,2, ...) and based on the user response, say upon entering '2', Unlike normal circumstance shown above in your script, I want the screen to prompt for interface name, staticip and gateway twice to input all these to create a 01-netcfg.yaml with two interfaces. I am pretty new to this and was wondering how would I loop this and use if else to achieve this?

New contributor
Shail is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • 2
    If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From Review – Pilot6 yesterday

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.