Network Automation
with Ansible
Anas Tarsha
Bay Area Network Operators Group (BANOG)
Anas Tarsha
• Network/Cloud Architect, CCIE
• Areas of focus: data center, cloud, and network
automation
• BANOG founder & organizer
• Follow me on Twitter @AnasTarsha
• Blog: AnasTarsha.com
Agenda
• About Ansible
• Use Cases
• How Ansible Works?
• Install Ansible
• Run Ansible
• Live Demos
What Is Ansible?
• IT Automation and configuration management tool
• Emerged in 2012 to initially deploy Linux
applications; support for networking modules was
added in release 2.x
• Open source; code available on Github
• Often compared to Chef, Puppet, and SaltStack
Why Ansible?
• Agentless and extensible
• You don’t need to be a programmer to get started
• Saves time (maybe)
• Speed, but do you care?
• Predictable outcome; reduces human error
Ansible Use Cases
• Generating device configurations
• Saving and collecting running device
configurations
• Pushing out configurations to the network
• Upgrading devices
How Does Ansible Work?
Ansible
Control Host
Inventory File
sw1
r1
fw1
Network
Device
Playbook
play 1
hosts: all
- task 1
- task 2
SSH
Netconf
eAPI
NX-API
Python code
executed here
Install Ansible
• Easy to install using utilities like pip, yum, apt
• Runs only on Linux-based machines
• Official Install Guide:
http://docs.ansible.com/ansible/intro_installation.html
Ansible Playbooks
• Describe a policy you want to enforce or tasks you want o execute on your
devices
• Expressed in YAML
• “We use YAML because it is easier for humans to read and write than other
common data formats like XML or JSON” — ansible.com
• Each playbook can have a play or more and each play can have one task or more
• Tasks are executed sequentially
- name: example playbook
hosts: junos
tasks:
- tasks you want to automate
Ansible Modules
• They do the actual work in playbook; take arguments
• Mostly written in Python and return JSON data
• Core modules come installed with Ansible; non-core modules can be installed manually
• Run in playbook, but can run single module from command line
• Use ansible-doc module_name command for help
• Ansible Networking Modules http://docs.ansible.com/ansible/list_of_network_modules.html
• Example:
- name: collect default set of facts and configuration in XML & JSON format
junos_facts:
config: yes
config_format: xml
Run Ansible
1. Create an inventory file and define your devices:
[ios]
router1 ansible_host=192.168.0.201
switch1 ansible_host=192.168.0.203
[junos]
router2 ansible_host=192.168.0.130
2. Create a playbook:
- name: ping Google’s DNS from the device
hosts: ios
tasks:
- ios_command:
commands: ping 8.8.8.8
….. <omitted>
3. Run the playbook: ansible-playbook myPlaybook.yml
Sample inventory file:
2 groups, total of 3
network devices defined
Sample playbook:
Include plays and tasks
to execute
Live Demos
• Vagrant running on macOS Sierra to provision the
host VM
• Ansible 2.2 running on Ubuntu 14.4 LTS
• Cisco CSR 1000v and Juniper vSRX
• SSH is enabled on CSR; Netconf is enabled on
vSRX
Demo 1: Ping Module
# ansible all -m ping
vsrx | SUCCESS => {
"changed": false,
"ping": "pong"
}
csr | SUCCESS => {
"changed": false,
"ping": "pong"
}
Demo 2: show_arp.yml
---
- hosts: ios
tasks:
- ios_command:
commands: show arp
host: csr
username: cisco
password: cisco
transport: cli
ios_command module
with required parameters
Demo 3:
show_arp_result.yml
---
- hosts: ios
tasks:
- ios_command:
commands: show arp
host: csr
username: cisco
password: cisco
transport: cli
register: result
- debug: var=result
Demo 4:
change_hostname.yml
---
- name: Change hostname on a Cisco IOS router
hosts: ios
tasks:
- ios_config:
commands:
- hostname CSR1
provider: "{{ cli }}"
ios_config module with
required arguments
Demo 5:
show_arp_result_junos.yml
---
- hosts: junos
tasks:
- junos_command:
commands: show arp
provider: "{{ netconf }}"
register: result
- debug: var=result
junos module with
required arguments
Additional Resources
• Network Automation with Ansible, report by Jason Edelman (free,
login required)
https://www.oreilly.com/learning/network-automation-with-ansible
• Up and Running with Ansible (free eBook)
https://ipfs.io/ipfs/
QmTJaLdhUW6jTdXGFoqv7wZe5KguBi5F2u4ihBdrUMVPhw
• Ivan Pepeljak’s Blog http://ipspace.net
• Learn Linux: video training from safaribooksonline.com or
lynda.com or pluralsight.com
Thank You
Stay in touch
Questions?

Network Automation with Ansible

  • 1.
    Network Automation with Ansible AnasTarsha Bay Area Network Operators Group (BANOG)
  • 2.
    Anas Tarsha • Network/CloudArchitect, CCIE • Areas of focus: data center, cloud, and network automation • BANOG founder & organizer • Follow me on Twitter @AnasTarsha • Blog: AnasTarsha.com
  • 3.
    Agenda • About Ansible •Use Cases • How Ansible Works? • Install Ansible • Run Ansible • Live Demos
  • 4.
    What Is Ansible? •IT Automation and configuration management tool • Emerged in 2012 to initially deploy Linux applications; support for networking modules was added in release 2.x • Open source; code available on Github • Often compared to Chef, Puppet, and SaltStack
  • 5.
    Why Ansible? • Agentlessand extensible • You don’t need to be a programmer to get started • Saves time (maybe) • Speed, but do you care? • Predictable outcome; reduces human error
  • 6.
    Ansible Use Cases •Generating device configurations • Saving and collecting running device configurations • Pushing out configurations to the network • Upgrading devices
  • 7.
    How Does AnsibleWork? Ansible Control Host Inventory File sw1 r1 fw1 Network Device Playbook play 1 hosts: all - task 1 - task 2 SSH Netconf eAPI NX-API Python code executed here
  • 8.
    Install Ansible • Easyto install using utilities like pip, yum, apt • Runs only on Linux-based machines • Official Install Guide: http://docs.ansible.com/ansible/intro_installation.html
  • 9.
    Ansible Playbooks • Describea policy you want to enforce or tasks you want o execute on your devices • Expressed in YAML • “We use YAML because it is easier for humans to read and write than other common data formats like XML or JSON” — ansible.com • Each playbook can have a play or more and each play can have one task or more • Tasks are executed sequentially - name: example playbook hosts: junos tasks: - tasks you want to automate
  • 10.
    Ansible Modules • Theydo the actual work in playbook; take arguments • Mostly written in Python and return JSON data • Core modules come installed with Ansible; non-core modules can be installed manually • Run in playbook, but can run single module from command line • Use ansible-doc module_name command for help • Ansible Networking Modules http://docs.ansible.com/ansible/list_of_network_modules.html • Example: - name: collect default set of facts and configuration in XML & JSON format junos_facts: config: yes config_format: xml
  • 11.
    Run Ansible 1. Createan inventory file and define your devices: [ios] router1 ansible_host=192.168.0.201 switch1 ansible_host=192.168.0.203 [junos] router2 ansible_host=192.168.0.130 2. Create a playbook: - name: ping Google’s DNS from the device hosts: ios tasks: - ios_command: commands: ping 8.8.8.8 ….. <omitted> 3. Run the playbook: ansible-playbook myPlaybook.yml Sample inventory file: 2 groups, total of 3 network devices defined Sample playbook: Include plays and tasks to execute
  • 12.
    Live Demos • Vagrantrunning on macOS Sierra to provision the host VM • Ansible 2.2 running on Ubuntu 14.4 LTS • Cisco CSR 1000v and Juniper vSRX • SSH is enabled on CSR; Netconf is enabled on vSRX
  • 13.
    Demo 1: PingModule # ansible all -m ping vsrx | SUCCESS => { "changed": false, "ping": "pong" } csr | SUCCESS => { "changed": false, "ping": "pong" }
  • 14.
    Demo 2: show_arp.yml --- -hosts: ios tasks: - ios_command: commands: show arp host: csr username: cisco password: cisco transport: cli ios_command module with required parameters
  • 15.
    Demo 3: show_arp_result.yml --- - hosts:ios tasks: - ios_command: commands: show arp host: csr username: cisco password: cisco transport: cli register: result - debug: var=result
  • 16.
    Demo 4: change_hostname.yml --- - name:Change hostname on a Cisco IOS router hosts: ios tasks: - ios_config: commands: - hostname CSR1 provider: "{{ cli }}" ios_config module with required arguments
  • 17.
    Demo 5: show_arp_result_junos.yml --- - hosts:junos tasks: - junos_command: commands: show arp provider: "{{ netconf }}" register: result - debug: var=result junos module with required arguments
  • 18.
    Additional Resources • NetworkAutomation with Ansible, report by Jason Edelman (free, login required) https://www.oreilly.com/learning/network-automation-with-ansible • Up and Running with Ansible (free eBook) https://ipfs.io/ipfs/ QmTJaLdhUW6jTdXGFoqv7wZe5KguBi5F2u4ihBdrUMVPhw • Ivan Pepeljak’s Blog http://ipspace.net • Learn Linux: video training from safaribooksonline.com or lynda.com or pluralsight.com
  • 19.
    Thank You Stay intouch Questions?