Expert · Automation, SDN and cloud networking · topic 52 of 57 · 30 min
Configuration management: Ansible, Puppet, Chef and NETCONF
Explanation
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.
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))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.
[store_switches]
sw-pune-01 ansible_host=10.20.1.2
sw-pune-02 ansible_host=10.20.1.3---
- 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.5Puppet 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.
| Ansible | Puppet | Chef | |
|---|---|---|---|
| Agent on the device? | No (agentless) | Yes | Yes |
| Push or pull? | Push from the control node | Pull by the agent | Pull by the agent |
| How it reaches devices | SSH (or an API) | Agent talks to the Puppet server | Agent talks to the Chef server |
| Files are called | Playbooks (and roles) | Manifests (grouped into modules) | Recipes (grouped into cookbooks) |
| Language | YAML | Puppet DSL, based on Ruby | Ruby |
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.
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
}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.
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
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)
Which tool is agentless and pushes changes over SSH?
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.
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
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Virtualisation, VRFs and cloud networkingAutomation, SDN and cloud networking · same module
- Why automate: SDN and controller-based networkingAutomation, SDN and cloud networking · same module
- What Ansible is and how it worksLinux and Red Hat (RHCSA to RHCE) · Ansible fundamentals
- Ansible: inventory and ad-hoc commandsDevOps: Git, Docker, Kubernetes and CI/CD · Configuration management with Ansible, and YAML
- Roles, templates, Galaxy and VaultDevOps: Git, Docker, Kubernetes and CI/CD · Configuration management with Ansible, and YAML
- Terraform state, backends and driftDevOps: Git, Docker, Kubernetes and CI/CD · Infrastructure as code with Terraform
Questions people ask about this
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.
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.
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.
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.
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 more topic in “Automation, SDN and cloud networking” before its exam: Virtualisation, VRFs and cloud networking.
- Module 11 exam: Automation, SDN and cloud networking · 13 questions · 20 min, without help. 4 topics of this module still to finish first.
- Then the next module, Troubleshooting and the CCNA exam. 57 topics left in the course before the final exam.