Ansible From Basics to Network Automation
Ansible From Basics to Network Automation
## Introduction to Ansible
1. Basic Concepts
- Control Node
- Managed Nodes
- Inventory
- Playbooks
- Plays
- Roles
- Tasks
- Handlers
- Modules
- Plugins
- Collections
- Basic Inventory
- Variable Syntax
## 3. Ansible Architecture: Master Node, Slave Node, and Tower Deep Dive
- Module States
- Prerequisites
- Implementation Notes
- Troubleshooting
- General Troubleshooting
- Shell Issues
- Socket/Path Errors
- Timeout Errors
1. Introduction to Ansible
• Configuration Management
• Application Deployment
• Orchestration
• Provisioning
• Continuous Delivery
It uses simple YAML files (called playbooks) and does not require an agent to be installed on target
machines, making it lightweight and easy to manage.
Basic Concepts
Control Node
• The machine where Ansible is installed and from where commands are executed.
• This is the central orchestrator that pushes instructions to other systems (managed nodes) via
SSH.
Managed Nodes
• No need to install an agent; communication happens over SSH or Win-RM (for Windows).
Inventory
• [webservers]
• web1.example.com
• web2.example.com
Playbooks
• Each consists of one or more plays, each targeting a group of hosts with defined tasks.
• Example:
• - hosts: webservers
• tasks:
• apt:
• name: nginx
• state: present
Plays
o Host selection
o Variable declarations
o Task execution
Roles
• Components:
o tasks/
o handlers/
o defaults/
o vars/
o files/
o templates/
o meta/
Example usage:
- hosts: webservers
roles:
- nginx
Tasks
Example:
apt:
name: nginx
state: present
Handlers
• Used for idempotent operations like restarting services after configuration changes.
Example:
service:
name: nginx
state: restarted
Modules
• Examples:
Plugins
• Types:
o Lookup plugins
o Filter plugins
Collections
Example:
hosts: db
collections:
- community.mysql
Deep Dive
Execution Model
1. Inventory is loaded
Ansible Vault
• Usage:
• ansible-vault encrypt secrets.yml
Dynamic Inventory
• Used in cloud environments (AWS, Azure, GCP) where hosts change frequently.
• Loop: j
• - name: Install packages
• apt:
• state: present
• loop:
• - nginx
• - git
• Conditionals:
• apt:
• name: nginx
• state: present
• Filters:
• {{ myvar | lower }}
Templates (Jinja2)
• template:
• src: nginx.conf.j2
• dest: /etc/nginx/nginx.conf
Idempotency
• Ansible ensures that running a playbook multiple times doesn’t change the system if it’s already in
the desired state.
Best Practices
An inventory in Ansible is a file or script that defines managed nodes (hosts) and how to connect to
them. It is the foundation for targeting machines with your playbooks.
• Dynamic: Scripts or plugins that pull real-time host data (e.g., from AWS, Azure)
[webservers]
web1.example.com
web2.example.com
[dbservers]
[routers]
[routers:vars]
ansible_network_os=ios
ansible_connection=network_cli
Structure Example:
inventory/
├── hosts
├── group_vars/
│ └── webservers.yml
├── host_vars/
│ └── web1.example.com.yml
group_vars/webservers.yml
ansible_user: ubuntu
ansible_python_interpreter: /usr/bin/python3
host_vars/web1.example.com.yml
nginx_port: 8080
Ansible merges variables from multiple sources. The precedence order (lowest to highest) includes:
1. Role defaults
3. Inventory group_vars
4. Inventory host_vars
5. Playbook vars
6. set_fact
{{ ansible_hostname }}
{{ nginx_port }}
You can group devices based on their platform or OS for better targeting.
[cisco_ios]
[network_devices:children]
cisco_ios
juniper_junos
hosts: network_devices
gather_facts: no
tasks:
ios_facts:
gather_subset: hardware
Ansible Vault allows encrypting any file that contains secrets (passwords, keys, tokens).
Encrypt a file:
Use in Playbook:
db_password: !vault |
$ANSIBLE_VAULT;1.1;AES256
316637386333376163613...
When working with cloud platforms, you can use dynamic inventory scripts/plugins.
plugin: amazon.aws.aws_ec2
regions:
- us-east-1
filters:
tag:Environment: dev
Run:
Summary Table
Component Purpose
Definition:
The control node (a.k.a. master node) is the machine where Ansible is installed and commands or
playbooks are executed from. It manages remote systems (managed/slave nodes) over SSH (or WinRM
for Windows).
Characteristics:
Common Commands:
ansible-inventory --list
ansible-playbook site.yml
Definition:
A managed node is any target machine (Linux, Windows, network devices, etc.) that Ansible controls.
Requirements:
• For network devices, may use API or CLI interfaces (platform-specific modules)
Authentication Options:
• SSH keys
• Vault-encrypted credentials
| SSH / WinRM
• Ansible Tower is a web-based UI and REST API for managing, visualizing, scheduling, and
auditing your Ansible automation.
Features:
• Workflow visualizer
[ User or API ]
Concept Description
• Username/password
• Vault passwords
Tower vs CLI
GUI support
Scheduling
Job visualization
Audit logs
Security in Tower
• Job isolation
Summary
Component Purpose
a. Sample
b. Master and Slaves
c. Ansible Tower
Part 1: Full-Fledged Ansible Playbook (Single Playbook)
Use Case: Install and configure Apache Web Server on a group of Ubuntu servers, start and
enable the service, and deploy a custom index.html.
site.yml
---
hosts: webservers
become: true
vars:
apache_package: apache2
apache_service: apache2
tasks:
apt:
state: present
update_cache: true
service:
state: started
enabled: true
copy:
owner: www-data
group: www-data
mode: '0644'
ufw:
rule: allow
port: '80'
proto: tcp
register: apache_status
ignore_errors: yes
debug:
var: apache_status.stdout
Step-by-Step Explanation
Line Explanation
• One playbook handles App server setup, the other handles Database setup
---
hosts: appservers
become: true
tasks:
apt:
name: nodejs
state: present
apt:
name: npm
state: present
copy:
src: ./app/
dest: /opt/app/
---
hosts: dbservers
become: true
vars:
mysql_root_password: "StrongRootPass123"
tasks:
apt:
name: mysql-server
state: present
mysql_secure_installation:
login_password: ''
validate_password_policy: LOW
remove_anonymous_users: yes
disallow_root_login_remotely: yes
[appservers]
app01 ansible_host=192.168.10.101
[dbservers]
db01 ansible_host=192.168.10.102
1. Credential
• Type: Machine
2. Project
3. Inventory
• Ties together:
o Project
o Inventory
o Credentials
o Verbosity
├── site.yml
├── roles/
│ └── apache/
│ ├── tasks/
│ │ └── main.yml
│ └── templates/
│ └── index.html.j2
site.yml
hosts: webservers
become: true
roles:
- apache
roles/apache/tasks/main.yml
apt:
name: apache2
state: present
update_cache: yes
- name: Deploy templated index.html
template:
src: index.html.j2
dest: /var/www/html/index.html
roles/apache/templates/index.html.j2
Step Description
ok: [web01]
changed: [web01]
Summary Table
Part Type Tools Description
3 Tower Integration Tower/AWX Git-backed CI/CD-style provisioning with GUI, logging, and RBAC
• Engineers comfortable with CLI-based network configs and basic Ansible usage
Advanced Topics Overview
These are platform-aware modules that manage specific network resources like VLANs, interfaces, and
BGP neighbors.
Unlike raw CLI or command modules, resource modules offer idempotency, better structure, and easier
error handling.
State Behavior
hosts: cisco_devices
gather_facts: no
tasks:
cisco.ios.ios_vlans:
config:
- vlan_id: 10
name: Users
- vlan_id: 20
name: Servers
state: merged
hosts: switches
tasks:
cisco.ios.ios_config:
backup: yes
cisco.ios.ios_vlans:
state: rendered
cisco.ios.ios_facts:
cisco.ios.ios_vlans:
state: merged
Prerequisites
[cisco_devices]
switch1 ansible_host=10.0.0.1
[cisco_devices:vars]
ansible_user=admin
ansible_password=secret
ansible_network_os=ios
ansible_connection=network_cli
hosts: all
gather_facts: no
tasks:
ios_facts:
ios_config:
backup: yes
hosts: all
tasks:
resource: vlans
config:
- vlan_id: 30
name: Voice
state: merged
Implementation Notes
• Use gather_facts: no for network devices unless specific facts are required.
o Juniper: junipernetworks.junos
o Arista: arista.eos
ios_command:
commands:
register: output
set_fact:
• parse_cli
• parse_cli_textfsm
• parse_cli_ttp
Use the validate plugin to verify configs/data structures before applying them.
Steps:
validate:
criteria:
CLI parsing fails Unsupported CLI output Customize TextFSM or TTP template
Proxy issues Restricted outbound traffic Set proper env vars or bypass
LINKEDIN: WWW.LINKEDIN.COM/IN/MACHAN-VISHAL
MAIL [email protected]
CALL (217)5889385