AI, and Network Operations and Management · Topic 5 of 6
Ansible for Network Configuration Management
Learn how Ansible runs commands on network devices: agentless push over SSH, inventory, playbooks, the ios_command and ios_config modules and the output.
- 5 min
- Ansible is agentless and pushes tasks over SSH or an API.
- Instructions are written in YAML playbooks.
- The inventory lists the devices and groups them.
What it is
Ansible is an automation tool that runs tasks on many devices from one control machine, using instructions you write in a text file.
Typing the same command on fifty switches is slow and error prone. With Ansible you write the task once, list the devices, and run it. Ansible connects to each device, runs the task and reports the result.
Ansible is agentless, which means nothing is installed on the managed devices. For network devices it connects over SSH or an API. It uses a push model: the control machine sends the work out when you run it.
How it works
Three things are needed: an inventory of devices, a playbook of tasks, and modules that do the work.
The main parts
| Part | What it is |
|---|---|
| Control node | The machine where Ansible is installed and run |
| Inventory | The list of managed devices, often in groups |
| Playbook | A YAML file that lists what to do and to which devices |
| Play | One section of a playbook, tying a group of hosts to tasks |
| Task | One action, run in order |
| Module | The unit of code that does the work, such as running a command |
| Variables | Values such as connection details that let one playbook serve many devices |
Inventory
The inventory names the devices and puts them in groups. A simple one looks like this:
[switches]
sw1 ansible_host=10.1.1.11
sw2 ansible_host=10.1.1.12
[switches:vars]
ansible_network_os=cisco.ios.ios
ansible_connection=ansible.netcommon.network_cli
ansible_user=admin
The variables tell Ansible that these are Cisco IOS devices and that it should reach them over SSH using the network CLI connection. Passwords should not be written in plain text. Store them in a protected way, for example with Ansible Vault.
A playbook that executes a command
The module cisco.ios.ios_command sends show commands to a device and returns the output.
- name: Check interface status
hosts: switches
gather_facts: false
tasks:
- name: Run show ip interface brief
cisco.ios.ios_command:
commands:
- show ip interface brief
register: result
- name: Display the output
ansible.builtin.debug:
var: result.stdout_lines
hosts: switchesselects the inventory group.gather_facts: falseskips the default fact collection that is made for servers.commands:lists what to run, in order.register: resultstores the output in a variable.- The debug task prints the stored output.
You run it from the control machine:
$ ansible-playbook -i inventory.ini check_interfaces.yml
Changing configuration
The module cisco.ios.ios_config sends configuration commands. The lines option holds the commands, and parents names the section they belong in.
- name: Set an interface description
cisco.ios.ios_config:
lines:
- description Uplink to core
parents: interface GigabitEthernet0/1
Other modules in the same collection manage specific features, for example cisco.ios.ios_vlans for VLANs and cisco.ios.ios_facts for collecting device information. Modules for a feature check the current state first, so running them twice changes nothing the second time. This property is called idempotence.
Reading the result
Ansible prints a recap at the end of the run.
PLAY RECAP *********************************************
sw1 : ok=2 changed=0 unreachable=0 failed=0
sw2 : ok=1 changed=0 unreachable=0 failed=1
- ok: tasks that ran successfully.
- changed: tasks that changed something on the device.
- unreachable: the control machine could not connect, so check the address, SSH and credentials.
- failed: the task ran and failed, so read the error message for the cause.
Where Ansible fits
Ansible is a configuration management tool that applies the same tasks to many devices. It fits the automation based approach, and the playbooks can be kept in version control as infrastructure as code. It does not replace the CLI, but it removes the repetition.
Associate Ansible with YAML playbooks, an inventory, modules and no agent on the device.
Common mistakes
Calling Ansible agent based. It is agentless. Nothing is installed on the managed device.
Forgetting the inventory. Without a list of hosts there is nothing to run against.
Treating a playbook as a shell script. It lists tasks that call modules, written in YAML.
Putting passwords in plain text. Protect credentials, for example with Ansible Vault.
Running a change on every device untested. A bad change is applied everywhere at once. Test on one device first.
YAML depends on indentation. A wrong number of spaces can break a playbook or change its meaning.
What to remember for the exam
- Ansible is agentless and pushes tasks over SSH or an API.
- Instructions are written in YAML playbooks.
- The inventory lists the devices and groups them.
- A playbook contains plays, and plays contain tasks that call modules.
cisco.ios.ios_commandruns show commands.cisco.ios.ios_configsends configuration lines.registerstores task output so it can be displayed or checked.- Feature modules are idempotent, so repeating them is safe.
- The recap shows ok, changed, unreachable and failed counts.
Check yourself
What does agentless mean for Ansible and network devices?
Nothing needs to be installed on the device. The control machine connects over SSH or an API and pushes the tasks.
What are the roles of the inventory, the playbook and a module?
The inventory lists the devices, the playbook lists the tasks to run on them, and a module is the code that performs one kind of action, such as running a command.
Which module would you use to run
show ip interface briefon a switch, and how do you keep its output?Use
cisco.ios.ios_commandwith the command in itscommandslist, and addregisterto store the output in a variable.
Topic 5 of 6 in AI, and Network Operations and Management
Test yourself on AI, and Network Operations and Management
The full Cisco CCNA 200-301 v2.0 question bank has 1,242 questions, with timed practice exams and an explanation for every answer.
This study guide is independent and is not affiliated with, endorsed by, or sponsored by Cisco. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.