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
In a nutshell
  • 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.

Control machineruns the playbookreadsInventorylist of devicesControl machinepushes tasks over SSHSwitch 1runs the moduleControl machinepushes tasks over SSHSwitch 2runs the module

The main parts

PartWhat it is
Control nodeThe machine where Ansible is installed and run
InventoryThe list of managed devices, often in groups
PlaybookA YAML file that lists what to do and to which devices
PlayOne section of a playbook, tying a group of hosts to tasks
TaskOne action, run in order
ModuleThe unit of code that does the work, such as running a command
VariablesValues 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: switches selects the inventory group.
  • gather_facts: false skips the default fact collection that is made for servers.
  • commands: lists what to run, in order.
  • register: result stores 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.
Run playbookfrom the control machineRecapok, changed, unreachable, failedUnreachablefix connection and credentialsRecapFailedread the task error

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.

Exam tip

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.

Watch out

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_command runs show commands. cisco.ios.ios_config sends configuration lines.
  • register stores 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 brief on a switch, and how do you keep its output?

    Use cisco.ios.ios_command with the command in its commands list, and add register to 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.