How to deploy Netplan with Ansible

Ansible-Netplan: – https://github.com/Perfect10NickTailor/ansible-netplan

This role will push out the config to the designated host and apply it
It will make a backup of the previous config before applying the new config, this is just incase your config change had an yaml error and you need to quickly go in and revert back.
There is a defaults/main.yml file that all the flags and how to use them.

.

Netplan.io- what is it is? Basically yaml files to deploy network configurations in a scalable manner by Ubuntu

How to use this role:

1.You must first download the git repository into your roles directory usually ansible/role/

.

2.Now you want edit the hosts.client file name file or create it if it doesn’t exist under your “ansible/inventory/dev:staging:prod” directory. This is a good way to separate environments with ansible, inside each environment you should have a hosts.file like indicated below.

Example file: hosts.dev, hosts.staging, hosts.prod

Put your server under the appropriate group inside the file and save
i.Testmachine1 ansible_host=192.168.90.10

Note: If there is no group simply list the server outside grouping, the –limit flag will pick it

up.

3.Now inside this directory you should see hosts & host_vars, group_vars

Descriptions:

Hosts. – is where you will list your servers under specific groups which tell the playbook (what the server is, if it the server should have a specific task run on it, and how to find it)
Host_vars – Inside this directory is where you list the server by name which is you will list under hosts. Inside these files you pass variable parameters to the specific roles when running your playbook. Without these the playbook cant do the tasks you want it to.
Group_vars – Are how a way to group variables for sets of servers and this keeps code cleaners and easier to manage.

Operational Use:

4.Move inside host_var
cd host_var
create a file called {{ servername }} and save it for us its testmachine1

Okay now here is where VSC is handy. You want to connect your visual studio code to the management server under your user. I have provided a link which shows you how to setup your keys and get VSC working with it.

.

Note: You don’t have to use VSC you can use good old nano or vim, but it’s a pain. Up to you.

https://medium.com/@sujaypillai/connect-to-your-remote-servers-from-visual-studio-code-eb5a5875e348

.

.

5.Now Netplans can be simple or very complicated. Ansible-netplan is broken up into segments that look for these variables to pass.
Network, vlans, ethernets, bridges & bonds

.

6.Now my advice is not to copy the block from this document and to copy download the repo open in visual studio and copy it there.

.

Example files:

ansible/inventory/dev/host_var$ testmachine1 (with Bonding)

 

.

Example Yaml Block :

# testmachine1 netplan config

# This is the network for testmachine1 with network bonding

netplan_configuration:

    network:

      bonds:

        bond0:

          interfaces:

          – ens1f0

          – ens1f1

          parameters:

            mode: balance-rr

      ethernets:

        eno1:

          dhcp4: false

        eno2:

          dhcp4: false

        ens1f0: {}

        ens1f1: {}

      version: 2

.

      vlans:

        vlan.180:

          id: 180

          link: bond0

        #  dhcp4: false

        #  dhcp6: false

        vlan.3200:

          id: 3200

          link: bond0

        #  dhcp4: false

        #  dhcp6: false

        vlan.3300:

          id: 3300

          link: bond0

        #  dhcp4: false

        #  dhcp6: false

.

      bridges:

        br200:

          interfaces: [ vlan.200 ]

          addresses: [ 192.168.50.9/24 ]

          gateway4: 192.168.50.1

          nameservers:

                  addresses: [ 8.8.8.8,8.8.4.8 ]

                  search: [ nicktailor.com ]        

          dhcp4: false

          dhcp6: false

        br3000:

          interfaces: [ vlan.3000 ]

          dhcp4: false

          dhcp6: false

        br3200:

          interfaces: [ vlan.3200 ]

          dhcp4: false

          dhcp6: false

.

Example files:
ansible/inventory/dev/host_var$ testmachine1 (without Bonding)

.

Example Yaml Block :

#testmachine1

netplan_configuration:

    network:

      version: 2

      renderer: networkd

      ethernets:

        eno1:

          dhcp4: false

          dhcp6: false

        eno2:

          dhcp4: false

          dhcp6: false

.

      bridges:

        br0:

          interfaces: [ eno1 ]

          dhcp4: false

          dhcp6: false

        br1:

          interfaces: [ eno2 ]

          dhcp4: false

          dhcp6: false

        br1110:

          interfaces: [ vlan1110 ]

          dhcp4: false

          dhcp6: false

          addresses: [ 172.16.52.10/26 ]

          gateway4: 172.17.52.1

          nameservers:

                  addresses: [ 8.8.8.8,8.8.4.8 ]

.

        br600:

          interfaces: [ vlan600 ]

          dhcp4: false

          dhcp6: false

          addresses: [ 192.168.0.34/24 ]

        br800:

          interfaces: [ vlan800 ]

          dhcp4: false

          dhcp6: false

        br802:

          interfaces: [ vlan802 ]

          dhcp4: false

          dhcp6: false

        br801:

          interfaces: [ vlan801 ]

          dhcp4: false

          dhcp6: false

.

      vlans:

        vlan600:

          id: 600

          link: br0

          dhcp4: false

          dhcp6: false

        vlan800:

          id: 800

          link: br1

          dhcp4: false

          dhcp6: false

        vlan801:

          id: 801

          link: br1

          dhcp4: false

          dhcp6: false          

        vlan802:

          id: 802

          link: br1

          dhcp4: false

          dhcp6: false  

          

.

.

8.You must now edit the the appropriate lines and save the file
vlans, ethernets, blond, addresses, & bridges

.

9.Once saved you want to run the playbook against a test server before you push the code into the git repository. So it good to have a test vm to run your code against first.

.

Running your playbook:

1.You must run your play book from inside parent directory always “ansible
2.Now create a playbook called deploynetplan.yml in the ansible directory which simply calls the ansible-netplan role inside the roles directory.

Example: of ansible/deploynetplan.yml

hosts: all

  gather_facts: yes

  any_errors_fatal: true

  roles:

    – role: ansible-netplan

      netplan_enabled: true

.

Command:

ansible-playbook -i inventory/dev/hosts deploynetplan.yml -u nickadmin -Kkb –ask-become –limit=’testmachine1′

-i : This flag tells ansibe-playbook command which hosts file to use, these are always defined by environment like hosts.dev or hosts.staging  
-u : this is the ssh_user you will be connecting to the servers with
-Kkb : this tells ansible that you will be using sudo su – for the ssh_user when running all role/tasks
-ask-beocme : is saying become root
-limit=’server’ : this allows you to segement which server you want to run the playbook against.

.

Successful example run with bonding:

.

ntailor@KVMtestbox:~/ansible$ ansibleplaybooki inventory/dev/hosts deploynetplan.ymlu nickadminKkbaskbecomelimit=‘testmachine1’

SSH password:

BECOME password[defaults to SSH password]:

.

PLAY [all] *********************************************************************************************************************************************************************************************

.

TASK [Gathering Facts] *********************************************************************************************************************************************************************************

ok: [testmachine1]

.

TASK [ansiblenetplan : Install netplan] ***************************************************************************************************************************************************************

ok: [testmachine1]

.

TASK [ansiblenetplan : Backup exitsing configurations before removing live ones] **********************************************************************************************************************

changed: [testmachine1]

.

TASK [ansiblenetplan : copy 00install* netplan existing file to /etc/netplan/backups] ****************************************************************************************************************

changed: [testmachine1]

.

TASK [ansiblenetplan : keep only 7 days of backups of previous network config /etc/netplan/backups] ***************************************************************************************************

changed: [testmachine1]

.

TASK [ansiblenetplan : Capturing Existing Configurations] *********************************************************************************************************************************************

skipping: [testmachine1]

.

TASK [ansiblenetplan : debug] *************************************************************************************************************************************************************************

skipping: [testmachine1]

.

TASK [ansiblenetplan : Removing Existing Configurations] **********************************************************************************************************************************************

skipping: [testmachine1]

.

TASK [ansiblenetplan : Configuring Netplan] ***********************************************************************************************************************************************************

ok: [testmachine1]

.

TASK [ansiblenetplan : netplan apply] *****************************************************************************************************************************************************************

changed: [testmachine1]

.

TASK [ansiblenetplan : debug] *************************************************************************************************************************************************************************

ok: [testmachine1] => {

    “netplanapply”: {

        “changed”: true,

        “cmd”: “netplan apply”,

        “delta”: “0:00:00.601112”,

        “end”: “2022-01-31 16:43:45.295708”,

        “failed”: false,

        “msg”: “”,

        “rc”: 0,

        “start”: “2022-01-31 16:43:44.694596”,

        “stderr”: “”,

        “stderr_lines”: [],

        “stdout”: “”,

        “stdout_lines”: []

    }

}

.

TASK [ansiblenetplan : Show vlans that are up or down] ************************************************************************************************************************************************

changed: [testmachine1]

.

TASK [ansiblenetplan : debug] *************************************************************************************************************************************************************************

ok: [testmachine1] => {

    “vlan.stdout_lines”: [

        “14: vlan.180@bond0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state LOWERLAYERDOWN group default qlen 1000”,

        “15: vlan.3300@bond0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state LOWERLAYERDOWN group default qlen 1000”

    ]

}

.

TASK [ansiblenetplan : show bridge details] ***********************************************************************************************************************************************************

changed: [testmachine1]

.

TASK [ansiblenetplan : debug] *************************************************************************************************************************************************************************

ok: [testmachine1] => {

    “bridges.stdout_lines”: [

        “bridge name\tbridge id\t\tSTP enabled\tinterfaces”,

        “br180\t\t8000.000000000000\tyes\t\t,

        “br3200\t\t8000.000000000000\tyes\t\t,

        “br3300\t\t8000.000000000000\tyes\t\t

    ]

}

.

PLAY RECAP *********************************************************************************************************************************************************************************************

testmachine1               : ok=12   changed=6    unreachable=0    failed=0    skipped=3    rescued=0    ignored=0   

.

.

.

Push your inventory/dev/host_var/testmachine1 code to Git :

 

Once you successfully checked your deploy worked by logging on to the client host and confirming everything looks good. You now want to push your code to git repo. Since you were able to clone you repo, you should be able to push to it.

.

Git Add Commands.

1.Git add . (will do every file you changed)
2.Git add filename will only add the file you want

.

Git Commit Commands

1.Git commit
a.This will take you to a message screen. Just type a note of what you did save the file
2.Git push
b.This will push your changes

.

.

Leave a Reply

Your email address will not be published. Required fields are marked *

0