Skip to content

Expert · Automation, SDN and cloud networking · topic 52 of 57 · 30 min

Configuration management: Ansible, Puppet, Chef and NETCONF

Explanation

Configuration management tools keep many devices in the state you wrote down, instead of you typing the same lines on each one. Ansible is agentless and pushes changes over SSH using YAML playbooks; Puppet and Chef use an agent on each device that pulls its configuration from a server. NETCONF and RESTCONF are standard protocols that change device configuration using YANG data models.

The problem: configuration drift

Configuration drift is when devices that should be configured the same slowly become different. One engineer adds a test ACL and forgets to remove it. Another fixes a problem at 2 am on one switch only. After a year, no two switches are quite alike, and nobody knows which settings are deliberate. Drift causes strange faults and failed security audits.

Configuration management means writing the wanted configuration down in files, keeping those files in version control (such as Git), and letting a tool apply them to every device. This approach is called infrastructure as code: the network's configuration is treated like program code that is reviewed, tested and versioned.

Templates and variables

Most switches in a company differ only in a few values: hostname, management IP, VLAN names. A template is the shared configuration with placeholders, and variables fill in the values for each device. One template plus a small table of variables can produce the configuration for 500 switches. Ansible uses Jinja2 templates; in Python you can do the same with string.Template or f-strings.

python
from string import Template

line = Template("vlan $id\n name $name")
for vlan in [{"id": 10, "name": "BILLING"}, {"id": 20, "name": "CCTV"}]:
    print(line.substitute(vlan))
Template replaces $id and $name with values from each dict. Shows the vlan and name lines for VLANs 10 and 20.

Ansible

Ansible is the most common tool for network automation. It is agentless: nothing has to be installed on the routers and switches. The control node (your Linux machine or a server) connects to each device over SSH and pushes the changes. That suits network devices well, because you usually cannot install extra software on them.

  • Inventory: the list of devices, grouped, for example [store_switches].
  • Playbook: a YAML file listing plays and tasks: what to do on which hosts.
  • Module: the code that does one kind of job, such as cisco.ios.ios_config (send configuration lines) or cisco.ios.ios_vlans (manage VLANs as structured data).
  • Idempotent: running the same playbook twice gives the same result; the second run changes nothing if the device is already right.
ini
[store_switches]
sw-pune-01 ansible_host=10.20.1.2
sw-pune-02 ansible_host=10.20.1.3
inventory.ini: two switches in the group store_switches.
yaml
---
- name: Standard VLANs on store switches
  hosts: store_switches
  gather_facts: false
  connection: ansible.netcommon.network_cli
  vars:
    ansible_network_os: cisco.ios.ios

  tasks:
    - name: Make sure the store VLANs exist
      cisco.ios.ios_vlans:
        config:
          - vlan_id: 10
            name: BILLING
          - vlan_id: 20
            name: CCTV
        state: merged

    - name: Point every switch at the NTP server
      cisco.ios.ios_config:
        lines:
          - ntp server 10.0.0.5
vlans.yml. Run it with: ansible-playbook -i inventory.ini vlans.yml (add --check first for a dry run).

Puppet and Chef

Puppet and Chef came from the server world. Both use an agent, a small program installed on each managed device, which regularly pulls its wanted configuration from a central server and fixes any drift it finds. Only some network platforms support these agents, which is one reason Ansible is more common for networks.

AnsiblePuppetChef
Agent on the device?No (agentless)YesYes
Push or pull?Push from the control nodePull by the agentPull by the agent
How it reaches devicesSSH (or an API)Agent talks to the Puppet serverAgent talks to the Chef server
Files are calledPlaybooks (and roles)Manifests (grouped into modules)Recipes (grouped into cookbooks)
LanguageYAMLPuppet DSL, based on RubyRuby

Terraform, briefly

Terraform is a declarative provisioning tool: you describe what should exist (a cloud network, a subnet, a firewall rule) and Terraform creates, changes or deletes things until reality matches. Declarative means you state the end result, not the steps. It is used most for cloud networks; Ansible is used more for configuring devices that already exist.

output
resource "aws_vpc" "main" {
  cidr_block = "10.0.0.0/16"
}
Terraform (HCL): "there must be a VPC with this address range".

NETCONF, RESTCONF and YANG

Sending CLI lines over SSH works, but the CLI was made for humans. Its output is text that scripts must parse, and the commands differ between vendors. YANG is a data modelling language that describes a device's configuration and state as a clear tree of fields with types, such as interface name (a string) and enabled (true or false).

  • NETCONF: a protocol that reads and changes configuration described by YANG models. It carries XML and runs over SSH on TCP port 830. Operations include get-config and edit-config, and it can validate a change and apply it as one transaction.
  • RESTCONF: the same YANG models, through a REST API over HTTPS. It uses the normal methods (GET, POST, PUT, PATCH, DELETE) and can send JSON or XML.
Careful: Common mistakes: (1) Editing devices by hand after automating them. The next run overwrites your change, or the drift returns. Make every change in the source files. (2) Running a new playbook on all devices at once. Use --check, then a pilot group, then everyone. (3) Bad YAML indentation: YAML uses spaces, never tabs, and items at the same level must line up exactly. (4) Saying NETCONF uses port 22 or 443. NETCONF over SSH uses port 830; RESTCONF uses HTTPS.
Tip: Keep the inventory, playbooks and templates in a Git repository. Then every change has an author, a date and a reason, and you can roll back to yesterday's version.

Need help?

Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.

Try:

Watch the lesson

Configuration management: Ansible, Puppet, Chef and NETCONF · 1/7

Configuration management

In this lesson you will see how tools keep hundreds of devices configured the same way.

Prefer a video by a teacher? Find free videos on “Ansible network automation Cisco IOS playbook tutorial NETCONF RESTCONF YANG” on YouTube ↗

In real life: A college campus every semester

A university IT team in Bengaluru manages 180 access switches across hostels, labs and the library. Every semester the lab VLANs change. They keep one Ansible playbook and an inventory in Git. A junior engineer edits the VLAN list, a senior reviews the change, they run it with --check, then on the two library switches, and then on all 180 in about ten minutes. Before Ansible, the same job took two people three days, and some lab switches were always missed.

Practice (0/3 done)

Task 1 · Question

Which tool is agentless and pushes changes over SSH?

Task 2 · Code · Python

Write vlan_config(vlans) that takes a list of dicts like {"id": 10, "name": "BILLING"} and returns a LIST of Cisco IOS configuration lines: for each VLAN, "vlan <id>" followed by " name <name>" (one space before name). Use string.Template or an f-string.

Task 3 · Code · Python

Write access_port_config(ports) for access ports. Each item is a dict like {"intf": "Gi1/0/5", "vlan": 10, "desc": "Billing PC"}. Return a list of lines in this order for each port: "interface <intf>", " description <desc>", " switchport mode access", " switchport access vlan <vlan>". This is what an Ansible template does for hundreds of ports.

Quick quiz

Question 1

1.What is configuration drift?

Question 2

2.Which TCP port does NETCONF over SSH use?

Question 3

3.In Chef, what is a collection of recipes called?

Question 4

4.What does YANG describe?

Question 5

5.Which tool is mainly used to declare and create cloud resources such as VPCs and subnets?

Read more (free)

Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.

What is configuration drift?

Answer: Devices that should be configured the same slowly becoming different

Drift comes from one-off manual changes. Configuration management tools detect and fix it.

Which TCP port does NETCONF over SSH use?

Answer: 830

NETCONF over SSH uses TCP port 830. RESTCONF runs over HTTPS.

In Chef, what is a collection of recipes called?

Answer: A cookbook

Chef groups recipes into cookbooks. Ansible has playbooks; Puppet has manifests.

What does YANG describe?

Answer: The structure of a device's configuration and state data

YANG is a data modelling language. NETCONF and RESTCONF use YANG models to know which fields exist.

Which tool is mainly used to declare and create cloud resources such as VPCs and subnets?

Answer: Terraform

Terraform is a declarative provisioning tool, widely used to build cloud networks.

What does "agentless" mean for Ansible?Related

Answer: No Ansible software has to run permanently on managed nodes

It connects over SSH and runs modules on demand.

From What Ansible is and how it works

Why prefer ansible.builtin.apt over ansible.builtin.shell with "apt-get install -y nginx"?Related

Answer: apt is idempotent and reports changed only when it really changes something

shell runs every time and always reports changed; the apt module checks state first.

From Ansible: inventory and ad-hoc commands

What is a collection in Ansible?Related

Answer: A package of modules, roles and plugins under a namespace, like community.general

Collections are how modules are packaged and shared, for example through Galaxy.

From Roles, templates, Galaxy and Vault

What does terraform state rm aws_s3_bucket.legacy do to the real bucket?Related

Answer: Nothing: Terraform just stops tracking it

state rm only removes the record. The real resource keeps existing.

From Terraform state, backends and drift

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

Next steps

  1. 1 more topic in “Automation, SDN and cloud networking” before its exam: Virtualisation, VRFs and cloud networking.
  2. Module 11 exam: Automation, SDN and cloud networking · 13 questions · 20 min, without help. 4 topics of this module still to finish first.
  3. Then the next module, Troubleshooting and the CCNA exam. 57 topics left in the course before the final exam.

← Previous: REST APIs and JSON