Introduction to CLI Automation with Ansible Tim Nothnagel, Consulting Engineer Mike Leske, Technical Leader
Cisco Spark How Questions? Use Cisco Spark to communicate with the speaker after the session 1. Find this session in the Cisco Live Mobile App 2. Click Join the Discussion 3. Install Spark or go directly to the space 4. Enter messages/questions in the space cs.co/ciscolivebot#
Agenda Introduction to Ansible Getting Started with Ansible Using Ansible Command Line, Playbooks & Templates Lab Introduction Lab Scenario Conclusion
Session Objective Understanding of the basic principles of Ansible Being able to write a playbook including various Ansible concepts Getting hands-on experience in using Ansible with IOS, IOS XR and NX-OS 5
Timetable 9:00h - 9:45h Ansible & Lab Intro 9:45h - 12:45h Lab time 12:45h - 13:00h Wrap up 6
Related Sessions DEVNET-1002 NetDevOps for the Network Dude DEVNET-1223 IOS-XR Automation with Ansible TECDEV-4500 Automating Network Operations with Ansible BRKDCN-2025 Maximizing Network Programmability and Automation (...) 7
Introduction to Ansible
Ansible Overview Open Source Agentless Simple Wide Adoption Con! guration Management Orchestration Deployment 10
Getting Started with Ansible
Getting started with Ansible Ansible Controller Targets SSH (user/pass, public key) 1. Push configuration 2. Get configuration/state 3. Execute commands ansible.cfg Inventory Playbooks Modules 12
Using Ansible
Common Ansible Terms? Let s first cover the basic terms and concepts. 14
Ansible Configuration ansible.cfg The place for adjusting default settings based on your requirements Multiple alternative places for parameters and setting exists Typically, default settings are sufficient for most users Precedence order of Ansible configuration files (in this order): 1. ANSIBLE_CONFIG (an environment variable) 2. ansible.cfg (in the current directory) 3..ansible.cfg (in the home directory) 4. /etc/ansible/ansible.cfg (global ansible configuration) This lab uses ansible.cfg in scenario/project directory 15
Further reading: http://docs.ansible.com/ansible/latest/intro_inventory.html Inventory INI format file usually called hosts Defines the hosts which Ansible manages Hosts can be grouped together with [] Additional optional parameters can be defined Where does Ansible look for the inventory file: Option 1 (Default): /etc/ansible/hosts Option 2: inventory parameter in local ansible.cfg Option 3: -i <path> option on the command line $ cat ansible/hosts [core] CORE_XR ansible_host=198.18.1.5 [branch1] BRANCH_1_CSR ansible_host=198.18.1.12 BRANCH_1_SWITCH ansible_host=198.18.1.11 [branch2] BRANCH_2_CSR ansible_host=198.18.1.22 BRANCH_2_SWITCH ansible_host=198.18.1.21 [csr] BRANCH_1_CSR BRANCH_2_CSR ansible_host=198.18.1.12 ansible_host=198.18.1.22 [switch] BRANCH_1_SWITCH ansible_host=198.18.1.11 BRANCH_2_SWITCH ansible_host=198.18.1.21 16
Modules Prepared Scripts performing an action on a host All modules (~465+ Oct 2017) ship with Ansible Enormous variety of Ansible modules You can write your own modules Further reading: http://docs.ansible.com/ansible/latest/modules.html http://docs.ansible.com/ansible/latest/modules_by_category.html 17
Ad-hoc Command Allows you to execute tasks really quick without saving steps Useful to understand the basics of how Ansible works Further reading: http://docs.ansible.com/ansible/latest/intro_adhoc.html http://docs.ansible.com/ansible/latest/ping_module.html ansible <hosts_section> -m <module> [-a <arguments>] Default module is command ( -m command can be omitted) -m ping is the `Hello World of Ansible $ ansible -a "date" control localhost SUCCESS rc=0 >> Wed Nov 15 05:58:55 CET 2017 $ ansible -m ping core 172.16.20.30 SUCCESS => { "changed": false, "failed": false, "ping": "pong" } 18
Further reading: http://docs.ansible.com/ansible/latest/playbooks.html Playbooks Ansible s method of procedures (MoP) Playbooks store task sequences for later reuse Can have one or more plays and tasks Playbooks are written in YAML $ cat sample.yaml --- - hosts: control gather_facts: no connection: local tasks: - name: PING ANSIBLE CONTROL ping: - name: DATE COMMAND ON CONTROL command: date $ ansible-playbook sample.yaml PLAY [control] ********************************************************************************************************************** TASK [PING ANSIBLE CONTROL] ********************************************************************************************************************** ok: [localhost] 19
Further reading: http://docs.ansible.com/ansible/latest/playbooks_templating.html Ansible & Jinja2 Templates Jinja2 templates further enhance Ansible s configuration modelling capabilities, e.g. represent configlets in native device CLI Jinja2 templates have access to Ansible variables and implement many filters and tests for validation Templating is executed on Ansible controller $ cat ios_interface.j2 {% for interface in interfaces %} {% if interface.name.startswith('gigabit') %} interface {{ interface.name }} ip address {{ interface.intf_address }} {{ interface.intf_netmask }} no shutdown exit! {% endif %} {% endfor %} $ cat ios_interface.yaml - hosts: branch1[0] gather_facts: no connection: local vars: interfaces: - name: GigabitEthernet4 intf_address: 10.1.10.2 intf_netmask: 255.255.255.252 - name: GigabitEthernet5 intf_address: 10.1.10.6 intf_netmask: 255.255.255.252 tasks: - name: BUILD DATA INTERFACE CONFIG template: src: templates/ios_interface.j2 dest: configs/ios_interface.cfg - name: CONFIGURE DATA INTERFACES ios_config: src: configs/ios_interface.cfg 20
Typical Folder Structure Project specific inventory Project specific configuration YAML playbooks Folder for configurations created by templates Folder for jinja2 templates 21
Ansible vs. & Cisco NSO
10,000-feet Comparison Ansible Run to completion or error No rollback SSH towards target device Explicit tasks to wrap CLI or operation Cisco NSO Transactions all or nothing Rollback built-in Variety of southbound protocols (Netconf, REST, SNMP,...) Model based abstraction via YANG 23
Reference Architectures Spanning Applications and Networks Further reading: https://www.ansible.com/networks-with-cisco-nso-ansible Application Centric Connectivity Centric Ansible Playbooks NSO NSO Ansible Playbooks App App App App 24
Lab Introduction
Lab Setup Lab contains 1 XRv core router, 2 IOSv branch routers, 2 NX-OS switches and 2 PCs to verify end-to-end connectivity Ansible VM (Ubuntu) running Ansible operations 26
Lab Access Cisco dcloud is used for this lab, a Cisco CCO account is mandatory Click View to get more lab access details In-browser Remote Desktop session Get your dcloud Lab VPN details 27
Lab Access, cont. Option A Access the Windows Workstation directly via in-browser RDP session Option B Use the Cisco AnyConnect Client on your laptop with your Cisco dcloud Lab VPN settings to connect to your lab instance Use RDP to connect to the virtual Windows Workstation Alternatively, while inside dcloud VPN ssh directly into the Ansible controller 28
Lab Access, cont. The Windows Workstation provides access to multiple resources Putty: Atom: Chrome: Maestro: Doubleclick to direcly log into the Ansible controller Text editor with yaml syntax highlighting & FileSync plugin to copy files from/to Ansible controller Access to Internet & Ansible Docs Access to VIRL (not required for this Lab) 29
Lab Scenario
Lab Scenario Exercise Ansible Concepts & Objectives 1 Basics: ansible.cfg, inventory Ad-Hoc Operations, modules 2 Playbooks, parents, wait_for, tags, variables Prepare core config for CORE_XR: Loopback0 Interface, global OSPF activation 3 with_items, when, Jinja2 templates, register, debug Activate OSPF on BRANCH_1_CSR, configure interfaces on CORE_XR and BRANCH_1_CSR 4 nxos_nxapi, device-specific modules OSPF & Interface configuration on BRANCH_1_SWITCH 5 Playbook optimization, vars_dir, import_playbook Re-use playbooks to deploy configuration for BRANCH_2 service 31
Lab Time
Conclusion
Conclusion Ansible is simple and easy to kickstart Ansible can work together with Cisco NSO Before you write a shell script consider to write a playbook! Networking 34
Cisco Spark How Questions? Use Cisco Spark to communicate with the speaker after the session 1. Find this session in the Cisco Live Mobile App 2. Click Join the Discussion 3. Install Spark or go directly to the space 4. Enter messages/questions in the space cs.co/ciscolivebot#
Please complete your Online Session Evaluations after each session Complete 4 Session Evaluations & the Overall Conference Evaluation (available from Thursday) to receive your Cisco Live T-shirt All surveys can be completed via the Cisco Live Mobile App or the Communication Stations Complete Your Online Session Evaluation Don t forget: Cisco Live sessions will be available for viewing on-demand after the event at www.ciscolive.com/global/on-demand-library/.
Continue Your Education Demos in the Cisco campus Walk-in Self-Paced Labs Tech Circle Meet the Engineer 1:1 meetings Related sessions 37
Thank you