Network Configuration with Ansible
Move from imperative scripts to declarative automation: use Ansible playbooks to configure and manage network devices and Linux hosts at scale.
Network Configuration with Ansible is a free Linux Networking & TCP/IP for Developers lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Linux Networking & TCP/IP for Developers learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Declarative Automation
Bash and Python scripts describe how to do something step by step. Ansible describes the desired state and converges hosts toward it.
This is idempotent: running the same playbook twice leaves the system in the same correct state.
Agentless by Design
Ansible needs no agent on managed hosts. It connects over SSH (or network device APIs) and pushes changes, making it easy to adopt across a fleet.
The Inventory
The inventory lists the hosts you manage, grouped logically. Below is a simple INI inventory.
[routers]
rtr1 ansible_host=10.0.0.1
rtr2 ansible_host=10.0.0.2
[switches]
sw1 ansible_host=10.0.1.1Ad-Hoc Commands
For quick one-off tasks, run a module directly without a playbook. This pings all hosts in a group.
ansible routers -m pingYour First Playbook
A playbook is a YAML file describing tasks. Each task invokes a module with parameters.
- hosts: switches
tasks:
- name: Ensure NTP is installed
apt:
name: ntp
state: presentVariables and Templates
Variables parameterize playbooks. The template module renders Jinja2 files, perfect for generating per-device config from one source of truth.
- name: Render interface config
template:
src: iface.j2
dest: /etc/network/interfacesNetwork Device Modules
Ansible ships vendor collections (e.g. cisco.ios, arista.eos) that speak each platform's CLI or API to push configuration.
- name: Configure interface
cisco.ios.ios_config:
lines:
- description Uplink
- ip address 10.0.0.1 255.255.255.0
parents: interface GigabitEthernet0/1Handlers
Handlers run only when notified by a changed task — ideal for restarting a service or saving a device config exactly once after changes.
handlers:
- name: save config
cisco.ios.ios_config:
save_when: alwaysCheck Mode (Dry Run)
Run any playbook with --check to preview changes without applying them — a safe way to validate before touching production.
ansible-playbook site.yml --check --diffRoles for Reuse
Roles package tasks, templates, and variables into reusable units. A base-network role can be applied to every device for consistent baseline config.
- hosts: all
roles:
- base-network
- monitoringSecuring Secrets with Vault
Never store passwords in plaintext. ansible-vault encrypts sensitive variables, keeping credentials safe in version control.
ansible-vault encrypt group_vars/routers/secrets.ymlQuick Check
Test your Ansible understanding.
Recap
You can now automate declaratively with Ansible:
- Agentless, idempotent state management
- Inventories, playbooks, and variables
- Vendor network modules and Jinja2 templates
- Handlers, roles, and
--checkdry runs - Encrypt secrets with
ansible-vault
This extends your Bash, Python, and REST API automation lessons to fleet scale.
Frequently asked questions
Is the “Network Configuration with Ansible” lesson free?
Yes — the full text of “Network Configuration with Ansible” is free to read here on the web, and the Linux Networking & TCP/IP for Developers course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Linux Networking & TCP/IP for Developers course, upgrade to CoddyKit PRO.
What will I learn in “Network Configuration with Ansible”?
Move from imperative scripts to declarative automation: use Ansible playbooks to configure and manage network devices and Linux hosts at scale. You practise Linux Networking & TCP/IP for Developers with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Linux Networking & TCP/IP for Developers?
No prior experience is required. Linux Networking & TCP/IP for Developers on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Network Configuration with Ansible” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Linux Networking & TCP/IP for Developers lesson?
Yes. Every Linux Networking & TCP/IP for Developers lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Bash Scripting for Networking
- Python for Network Automation
- REST APIs for Network Devices
- Network Configuration with Ansible