Skip to content
This repository has been archived by the owner on Oct 11, 2020. It is now read-only.

How to use NAPALM with Ansible

Khelil Sator edited this page Oct 22, 2017 · 14 revisions

Need help with Ansible?

For help with Ansible you can refer to this repo

napalm-ansible source code

Napalm-ansible source code

napalm-ansible doc

http://napalm.readthedocs.io/en/latest/tutorials/ansible-napalm.html

napalm-ansible installation

pip install napalm-ansible

Update your ansible.cfg file

You also need to update your ansible configuration file

Run this command:

# napalm-ansible
To make sure ansible can make use of the napalm modules you will have
to add the following configurtion to your ansible configureation
file, i.e. `./ansible.cfg`:

    [defaults]
    library = /usr/local/lib/python2.7/dist-packages/napalm_ansible

For more details on ansible's configuration file visit:
https://docs.ansible.com/ansible/latest/intro_configuration.html
root@ubuntu:~/junos-automation-with-NAPALM#

Edit the ansible configuration file

nano ansible.cfg

and add this section

[defaults]
library = /usr/local/lib/python2.7/dist-packages/napalm_ansible

Execute playbooks with ansible-napalm modules

Run a test (the changes wont be applied) with the playbook pb.install-conf.yml

# ansible-playbook pb.install-conf.yml --tag configuration --extra-vars commit_changes=False --limit ex4200_12 --diff

PLAY [configure junos devices with ansible-napalm] **************************************************************************************************************************************************

TASK [Render BGP configuration for junos devices] ***************************************************************************************************************************************************
--- before
+++ after: /tmp/tmpiy5M05/bgp.j2
@@ -0,0 +1,59 @@
+interfaces {
+    ge-0/0/0 {
+        unit 0 {
+            description "ex4200-7";
+            family inet {
+                address 192.168.10.0/31;
+            }
+        }
+    }
+    ge-0/0/1 {
+        unit 0 {
+            description "ex4200-8";
+            family inet {
+                address 192.168.10.3/31;
+            }
+        }
+    }
+}
+protocols {
+    bgp {
+        group underlay {
+            import bgp-in;
+            export bgp-out;
+            type external;
+            local-as 204;
+            multipath multiple-as;
+            neighbor 192.168.10.1 {
+                peer-as 209;
+            }
+            neighbor 192.168.10.2 {
+                peer-as 210;
+            }
+        }
+    }
+    lldp {
+        interface "ge-0/0/0";
+        interface "ge-0/0/1";
+    }
+}
+routing-options {
+    router-id 192.179.0.112;
+    forwarding-table {
+        export bgp-ecmp;
+    }
+}
+
+policy-options {
+    policy-statement bgp-ecmp {
+        then {
+            load-balance per-packet;
+        }
+    }
+    policy-statement bgp-in {
+        then accept;
+    }
+    policy-statement bgp-out {
+        then accept;
+    }
+}

changed: [ex4200_12]

TASK [Install rendered Config] **********************************************************************************************************************************************************************
changed: [ex4200_12]

PLAY [wait for peers to establish connections] ******************************************************************************************************************************************************
skipping: no hosts matched

PLAY [audit junos devices with ansible-napalm] ******************************************************************************************************************************************************

PLAY RECAP ******************************************************************************************************************************************************************************************
ex4200_12                  : ok=2    changed=2    unreachable=0    failed=0

Configure and audit the network devices with the playbook pb.install-conf.yml

# ansible-playbook pb.install-conf.yml

PLAY [configure junos devices with ansible-napalm] **************************************************************************************************************************************************

TASK [Render BGP configuration for junos devices] ***************************************************************************************************************************************************
ok: [ex4200_12]
ok: [ex4200_7]
ok: [ex4200_8]

TASK [Install rendered Config] **********************************************************************************************************************************************************************
changed: [ex4200_12]
changed: [ex4200_7]
changed: [ex4200_8]

PLAY [wait for peers to establish connections] ******************************************************************************************************************************************************

TASK [pause] ****************************************************************************************************************************************************************************************
Pausing for 35 seconds
(ctrl+C then 'C' = continue early, ctrl+C then 'A' = abort)
ok: [localhost]

PLAY [audit junos devices with ansible-napalm] ******************************************************************************************************************************************************

TASK [Render validation files for junos devices] ****************************************************************************************************************************************************
ok: [ex4200_7]
ok: [ex4200_12]
ok: [ex4200_8]

TASK [validate states] ******************************************************************************************************************************************************************************
ok: [ex4200_12]
ok: [ex4200_8]
ok: [ex4200_7]

PLAY RECAP ******************************************************************************************************************************************************************************************
ex4200_12                  : ok=4    changed=1    unreachable=0    failed=0
ex4200_7                   : ok=4    changed=1    unreachable=0    failed=0
ex4200_8                   : ok=4    changed=1    unreachable=0    failed=0
localhost                  : ok=1    changed=0    unreachable=0    failed=0

The device configuration is stored in the directory backup_from_ansible

# ls backup_from_ansible/
ex4200_12  ex4200_7  ex4200_8

The diff between the running configuration and the new configuration is stored in the directory diff

# ls diff/
ex4200_12  ex4200_7  ex4200_8

The template for configuration is rendered in the directory render

# ls render/
ex4200_12_bgp.txt  ex4200_12.conf  ex4200_7_bgp.txt  ex4200_7.conf  ex4200_8_bgp.txt  ex4200_8.conf

The template for auditing is rendered in the directory validation

# ls validation/
ex4200_12.yml  ex4200_7.yml  ex4200_8.yml