Network Automation with Ansible
Foreword: This is only my opinions based on my experience working with network automation.
Network Automation for me is something that I realized a few years ago that I just have to learn, kicking and screaming, because the world is going towards that direction. I suck at programming, so it was a tough battle. I started learning the basics of python but never got to the point where I could comfortably write scripts to make my job easier.
However, when I found out about Ansible, my interest for automation have increased dramatically. It might not be perfect, but you can script a lot with it, without deep knowledge of python programming.
Ansible integrates with many systems and can be used to automate any device that is reachable through SSH. The scripts are called playbooks and are written in YAML format, which is very human readable in my opinion. Here is an example:
---
- hosts: GROUP1
connection: network_cli
gather_facts: false
vars:
- ansible_ssh_user: admin
ansible_ssh_pass: password
ansible_network_os: ios
tasks:
- name: Run a show command
ios_command:
commands:
- show interface GigabitEthernet1/0/1
register: output
- name: display output
debug:
msg: "{{ output.stdout_lines }}"
- name: saving output to local directory
copy:
content: "{{ output.stdout_lines | to_nice_json }}"
dest: /home/bkadm.no/{{ ansible_user }}/ios_commands.txt
I could explain every step but I feel like I don't need to. Just by reading through it, you already know what it does. This is why I like it so much. It hides all the complexity of Python.
I can mention that below "- name:" are the modules. The modules includes different python functions, depending on what purpose the module has. They are well documented on Ansibles documentation pages. The Ansible community is also producing new modules all the time, so the functions are constantly expanding.
Ansible can be complex, especially when working with variables. Here are two examples of variables with filters:
- set_fact:
Vlans_list: "{{ Vlans.results|map(attribute='stdout_lines')|flatten|replace(':', '') }}"
Switchports_list: "{{ Vlans.results|map(attribute='item')|flatten|replace(':', '') }}"
I'm not going to explain that either, but it is included in my guide on how to configure switchports for 802.1x using Ansible if you're interested.
I am planning on releasing my most complex playbooks I have written on Network automation. I'm not planning to write any basic stuff that can easily be learnt from other sources. Personally I started learning Ansible from CBT Nuggets.