SANOG32 Network Automation - With - Ansible
SANOG32 Network Automation - With - Ansible
bdNOG9
02-10 August, 2018
Dhaka, Bangladesh
NETWORK
AUTOMATION (NetDevOps)
Imtiaz Rahman
SBAC Bank Limited
with ANSIBLE
https://imtiazrahman.com
Sessions
• Session 1:
o 14:30 PM – 16:00 PM (Theory with example)
• Session 2:
o 16:30 PM – 18:00 PM (Configuration and hands on LAB)
Today’s Talk
>devops ?
DevOps
>devops !=
DevOps
infrastructure as code
Why automation ?
Avoid
Avoid repeated Faster Identical
typographical
task deployment configuration
error (Typos)
Tools for automation
What is ANSIBLE?
• Need python
Why ANSIBLE?
• Simple
• Push model
• Agentless
Why ANSIBLE?
Puppet
SSL
Puppet Puppet
master Client/agent
Agentless
Ansible
Controller SSH
node Managed
with ansible node’s
How it works
1 2 3 4
Return result
5
What can be done??
• Configuration Management
• Software Testing
• No more Dockerfile
Hosts
Inventory
Jinja2
YAML YAML
Playbooks
Playbooks
Jinja2
Inventory
Task
YAML Task
Playbooks
Hosts
Inventory
YAMLRolesInventory Facts
Task Playbooks Roles Facts
Hosts
Jinja2
Playbooks
Real world
Master Plan
Build a house work tools
(small plan)
playbook
Configure a device tasks modules
(play, play)
ANSIBLE Introduction
YAML
---
- hosts: ios-routers
tasks:
• File extention .yml/.yaml - name: Save Configuration
ios_command:
commands:
- write memory
• Easy for a human to read host: "{{ ansible_host }}"
ANSIBLE Introduction
Playbook
• Tell Ansible what to do
tasks:
Module
http://docs.ansible.com/ansible/list_of_network_modules.html
ANSIBLE Introduction
Task
Playbook
1 2 3
Task Task Task
tasks:
[ios-routers] groups
R_2691 ansible_host=192.168.45.3
R_3745 ansible_host=192.168.45.4
[v6-router] groups
R_7200 ansible_host=2001:db8::1001::1
ANSIBLE Introduction
Inventory
• Collections of files or directories inside a directory
• hosts (file)
• host_vars (dir)
• group_vars (dir)
Roles
• Ansible roles are a special kind of playbook that are fully
self-contained with tasks, variables, configuration
templates and other supporting files
Jinja2
• template engine for the Python programming language
• Variable declaration
ANSIBLE Introduction jinja2 sample
{% for interface in cisco_1921_interfaces %}
interface {{ interface }}
{% if interface == 'GigabitEthernet0/0' %}
description {{ item.int_descp }}
ip address {{ item.ipv4_addp }} {{ item.ipv4_mus }}
{% elif interface == 'GigabitEthernet0/1' %}
description {{ item.int_descs }}
ip address {{ item.ipv4_adds }} {{ item.ipv4_mus }}
{% endif %}
no shutdown
exit
{% endfor %}
Variable
Ansible Language Basics : Variable
Introduction to ansible variable
• Variable names should be letters, numbers, and underscores.
Variables Dictionaries
isp1_dc: 10.x.x.2 isp :
- isp_dc: 10.x.x.2
subnet: 255.255.255.252
Lists - isp_dc: 20.x.x.6
isp : subnet: 255.255.255.248
- isp1_dc: 10.x.x.2
- isp2_dc: 20.x.x.6
Ansible Language Basics : Variable
Accessing Variable
Lists (looping)
{{ item }}
Ansible Language Basics : Variable
host_vars and group_vars
├── apricot
│ ├── inventory
│ │ ├── group_vars
│ │ │ ├── all.yml
│ │ │ └── ipv4_router.yml
│ │ ├── host_vars
│ │ │ └── south_router.yml
│ │ └── hosts
│ ├── playbook.yml
│ └── roles
Ansible Language Basics : Variable
host_vars
Host-specefic variables
host_vars/south_router.yml
group_vars/ipv4_router.yml
LOOPS
Ansible Language Basics : loops
Introduction to Loops
• A loop is an instruction that repeats until a specified
condition is reached
• Nested
• Do-Until
• for
Ansible Language Basics : loops
cat vars/main.yml
interface_address:
- INTERFACE: “GigabitEthernet0/0” 3
DESC: “ISP1”
1 DC_IP: "10.X.X.1"
MASK: “255.255.255.252” roles
Output
Comments
Ansible Language Basics : comments
Comments in ansible
#
{# #}
Ansible Language Basics
Conditionals
Ansible Language Basics : conditionals
Filters
Ansible Language Basics : filters
Introduction to filters
{{ list1 | min }}
{{ myvar | ipaddr }}
http://docs.ansible.com/ansible/latest/playbooks_filters.html
Ansible Language Basics : filters
ipaddr filter for static routes
{{ item.ISP_BR | ipv4('netmask') }}
(Subnet mask)
Facts
Ansible Language Basics : facts
Collecting facts
Is a module and called by playbook to gather useful
information about remote host
gather_facts: yes/no
Ansible Language Basics
Templating (jinja2)
Ansible Language Basics : jinja2 templating
Filters
Condition
Many
more
Variable Loop
Ansible Language Basics : jinja2
├── router_config
│ ├── inventory
│ │ └── hosts
│ ├── output
│ │ └── SOUTH.cfg
│ ├── playbook.yml
│ └── roles
│ └── router
│ ├── tasks
│ │ └── main.yml
│ ├── templates
│ │ └── router_config.j2
│ └── vars
│ └── main.yml
Ansible Language Basics : jinja2
Jinja2 template
hostname {{ item.hostname }}
{# Physical interface #}
{% for interface in cisco_1921_int %}
interface {{ interface }}
description ->> {{ cisco_1921_int[interface].dess }}
ip address {{ cisco_1921_int[interface].addrs }}
{{ cisco_1921_int[interface].sub }}
no shutdown
exit
{% endfor %}
Ansible Language Basics
Roles setup
Ansible Language Basics : roles
Roles structure and files
├── router_config
tasks │ ├── inventory
tasks/main.yml │ │ └── hosts
│ ├── output
templates │ │ └── SOUTH.cfg
│ ├── playbook.yml
templates/router_config.j2 │ └── roles
│ └── router
vars │ ├── tasks
vars/main.yml │ │ └── main.yml
│ ├── templates
│ │ └── router_config.j2
files │ └── vars
files/myscript.sh │ └── main.yml
Ansible Language Basics
Debugging
Ansible Language Basics : debugging
Ansible debugging
error_on_undefined_vars in ansible.cfg
Ansible Vault
• It keeps sensitive data such as password, keys, variable
name in encrypted format
Ansible Vault
---
$ANSIBLE_VAULT;1.1;AES256
---creds: 643364643164623266393365366
username: "imtiaz" 561613566303362303933343662
password: ”password" 30653866373635386261643432
auth_pass: ”password”
http://docs.ansible.com/ansible/latest/intro_installation.html
How to run
• ansible <inventory> -m
• ansible-playbook
Ansible host
node 1 node 2
Demo 2
2. Ansible installation
6. Ansible GALAXY
???
Thank You
[email protected]
https://imtiazrahman.com
https://github.com/imtiazrahman/SANOG32-NETDEVOPS.git