Ansible Playbook Example of Copy/Fetch with Mellanox Spectrum Switch installed with Cumulus Linux

Version 3

    In this post, there is an example that shows the Ansible playbook fetches the networking configuration from a Mellanox Switch and saved it on the local host.

    This post will help you to learn how to write  a very basic playbook which can interact with switch and copy/fetch files.

    Apart from giving the basic playbook example, this post can be used for running Ansible for:

     

    • Upgrade scenarios
    • Taking a snapshot of the network before trying something new
    • Reverting quickly back to a snapshot
    • Providing an introduction to automation

     

    References

     

    Before we start, make sure you have a server installed with Ansible, and a Mellanox Spectrum Switch installed with Cumulus Linux.

    see Getting Started with Ansible Management of Spectrum Switches Installed with Cumulus Linux for details.

     

    Configuration

    1. Define the set of switches you want to control in hosts file (which is also known as inventory file) and it is located at /etc/ansible/hosts

    10.20.4.86 ansible_host=10.20.4.86 ansible_connection=ssh ansible_user=cumulus ansible_ssh_pass=CumulusLinux! ansible_sudo_pass=CumulusLinux!

     

    2. Here I have defined a host named 10.20.4.86, and the IP, username, password and sudo password is defined in this file.

    You can also define multiple hosts in a group name. It is easy to define a group, and then run a single playbook for them.

    [Network]
    10.20.4.86 ansible_host=10.20.4.86 ansible_connection=ssh ansible_user=cumulus ansible_ssh_pass=CumulusLinux! ansible_sudo_pass=CumulusLinux!

     

    In above example – the group name is Network. We can define multiple hosts under it, and when we run playbook we can call Network and the playbook will run for all hosts in network.

    I have used this group name in this example, and because we have only one host defined under it, the playbook will run for only that host.Playbook file definition, it is saved as read_files.yml on the Ansible server at the location /etc/ansible/ .

    Please go through the basic playbook tutorial from Ansible to understand more about the structure of this file.

    ---

    - hosts: [Network]

      become: yes

      tasks:

        - name: fetch ports.conf

          fetch: dest=save/{{ansible_hostname}}/ports.conf src=/etc/cumulus/ports.conf flat=yes

     

        - name: fetch interfaces

          fetch: dest=save/{{ansible_hostname}}/interfaces src=/etc/network/interfaces flat=yes

     

        - name: copy quagga daemons file

          fetch: dest=save/{{ansible_hostname}}/daemons src=/etc/quagga/daemons flat=yes

     

        - name: copy Quagga.conf

          fetch: dest=save/{{ansible_hostname}}/Quagga.conf src=/etc/quagga/Quagga.conf flat=yes

     

    Verification

    1. The playbook can be run with a simple ansible command "ansible-playbook <playbook_name.yml>

    root@c-csi-0123s:/etc/ansible# ansible-playbook read_files.yml

    /usr/local/lib/python2.7/dist-packages/cffi/model.py:532: UserWarning: 'point_conversion_form_t' has no values explicitly defined; guessing that it is equivalent to 'unsigned int'

      % self._get_c_name())

     

    PLAY [Network] *****************************************************************

     

    TASK [setup] *******************************************************************

    ok: [10.20.4.86]

     

    TASK [fetch ports.conf] ********************************************************

    ok: [10.20.4.86]

     

    TASK [fetch interfaces] ********************************************************

    ok: [10.20.4.86]

     

    TASK [copy quagga daemons file] ************************************************

    ok: [10.20.4.86]

     

    TASK [copy Quagga.conf] ********************************************************

    ok: [10.20.4.86]

     

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

    10.20.4.86                 : ok=5    changed=0    unreachable=0    failed=0

     

    root@c-csi-0123s:/etc/ansible#

    >> In the end it says that 5 tasks has gone OK. These tasks were defined in playbook

     

    2. Check if playbook has fetched the logs or not?

    root@c-csi-0123s:/etc/ansible# ls -lrt

    total 28

    drwxr-xr-x 2 root root     6 Jan 16 09:11 roles

    -rw-r--r-- 1 root root 14387 Mar 10 12:21 ansible.cfg

    drwxr-xr-x 2 root root     6 Mar 10 12:27 logs

    -rw-r--r-- 1 root root   535 Mar 10 14:29 read_files.yml

    -rw-r--r-- 1 root root    11 Mar 10 14:34 read_files.retry

    -rw-r--r-- 1 root root  1169 Mar 10 14:39 hosts

    drwxr-xr-x 3 root root    20 Mar 10 14:40 save

    drwxr-xr-x 2 root root    76 Mar 10 16:06 playbooks

    root@c-csi-0123s:/etc/ansible# cd save

    root@c-csi-0123s:/etc/ansible/save# ls -lrt

    total 0

    drwxr-xr-x 2 root root 72 Mar 10 15:20 cumulus

    root@c-csi-0123s:/etc/ansible/save# cd cumulus/

    root@c-csi-0123s:/etc/ansible/save/cumulus# ls -lrt

    total 16

    -rw-r--r-- 1 root root 711 Mar 10 14:40 ports.conf

    -rw-r--r-- 1 root root 425 Mar 10 14:40 interfaces

    -rw-r--r-- 1 root root 988 Mar 10 14:40 daemons

    -rw-r--r-- 1 root root 909 Mar 10 14:40 Quagga.conf

    root@c-csi-0123s:/etc/ansible/save/cumulus#

    >> All files generated by the playbook which fethced these files from switch 10.20.4.86