Using Ansible for Network Automation

Ansible is an open-source automation tool primarily used for configuration management, application deployment, and task automation. In network automation, Ansible enables IT teams to manage and configure network devices in a consistent, repeatable, and agentless manner.

Ansible is a tool used to automate Server provisioning and network provisioning.  Ansible is completely free!  You can dive deep into Ansible at their web site.

  • Developed by Red Hat, Ansible uses YAML-based playbooks to define automation tasks.
  • It follows a declarative model: you describe the desired state, and Ansible enforces it.
  • Agentless: No software is required on managed devices; communication is usually over SSH or API.

How Ansible Works for Network Automation

Ansible automates network devices the same way it automates servers, using modules and inventory definitions.

1. Inventory

  • Defines the devices to manage.
  • Can include routers, switches, firewalls, etc.
  • Example:
[routers]
router1 ansible_host=192.168.1.1 ansible_user=admin

2. Modules

  • Reusable units of work (e.g., ios_config, nxos_config, eos_config).
  • Abstract vendor-specific CLI or API commands.
  • Examples:
    • ios_config: Configure Cisco IOS
    • junos_config: Configure Juniper devices

3. Playbooks

  • Written in YAML.
  • Define tasks to apply to network devices.
  • Example:
- name: Configure hostname on Cisco router
  hosts: routers
  gather_facts: no
  tasks:
    - name: Set hostname
      cisco.ios.ios_config:
        lines: hostname Router1

4. Connection Types

  • network_cli: Uses CLI over SSH (e.g., for Cisco IOS).
  • httpapi: Uses REST or API (e.g., for modern platforms like Cisco DNAC or Arista CloudVision).
  • netconf / restconf: For standards-based device management.

An Ansible Typical Workflow

  1. Define inventory of devices.
  2. Write playbooks using vendor modules.
  3. Run Ansible from a control node (your laptop, server, or CI/CD system).
  4. Ansible connects to devices, applies configuration, and reports success/failure.

The Benefits of Using Ansible for Networking

  • Consistent, repeatable configurations
  • Reduces manual errors and effort
  • Works across multi-vendor environments
  • Easily integrates with CI/CD pipelines
  • Scales from a few devices to thousands

A Quick Exercise to Learn Ansible

A great way to learn Ansible as a network automation tool is to use it in an emulated network environment. GNS3 fits that requirement perfectly.  GNS3 is also completely free!

As many of you know, we have been users of GNS3 since forever, plus we were part of their crowd sourcing!

GNS3 has evolved tremendously in the past years. Using the versions after 2.x brings great functionality into GNS3 including Ansible. We strongly urge students to use GNS3 to learn Ansible. If you want to learn how to install and use GNS3, there is a separate reference here.  Assuming you have version 2.x or later of GNS3 running, we will show how to accomplish this in a couple of simple videos below.

Even if you have never used GNS3, you can watch the videos below to get a clear understanding of how Ansible works!

Watch this video first to see exactly how to get Ansible with your GNS3 environment.

Install Ansible in GNS3 from Andrew Walding on Vimeo.

 The next step is to configure IP addresses:

 

 

 Here is the router configuration used:

enable
configure terminal
hostname R1
alias exec s show run
alias exec c config terminal
alias exec i show ip interface brief
alias exec ipr show ip route
ip tcp synwait 5
no service timestamps
no service time debug
line console 0
no login
no exec-timeout
logging synch
privilege level 15
line aux 0
no login
no exec-timeout
logging synch
privilege level 15
line vty 0 4
login local
transport input all
ip domain-name cellstream.com
crypto key generate rsa
1024
interface f0/0
ip address 192.168.1.101 255.255.255.0
no shutdown
username student pass csi123
username student priv 15
end
write

Now let’s add some routers into the topology:

 

 Now it is time to set up a simple Ansible environment. We will create a simple ansible.cfg file and a hosts file so ansible will be able to work with our routers:

 

 It’s finally time to actually create an Ansible playbook and run it against our topology. You will find the playbook1.yml file available to download below the video.

 

 Here is the playbook1.yml content:

---
- name: Install OSPF
  hosts: [gns3routers]
  gather_facts: false
  connection: local

  vars:
    cli:
      username: student
      password: csi123
      transport: cli

  tasks:
    - name: enable ospf
      ios_config:
        provider: "{{ cli }}"
        authorize: yes
        parents: router ospf 1
        lines:
          - network 0.0.0.0 255.255.255.255 area 0

      register: print_output

    - debug: var=print_output

 


I hope you find this article and its content helpful.  Want to learn more about Ansible? Check out these posts:

Comments are welcomed below from registered users.  You can also leave comments at our Discord server

If you would like to see more content and articles like this, please support us by clicking the patron link where you will receive free bonus access to courses and more, or simply buying us a cup of coffee!

Leave a Comment

Scroll to Top