NetAuto Academy

Ansible for Network Engineers

Inventory: From CSV to Ansible Inventory

Intermediate12 min read

What you'll learn

  • How to translate a CSV inventory into Ansible's INI inventory format
  • How to translate the same inventory into Ansible's YAML inventory format
  • What groups, host variables, and group variables are for
  • That this is the same 5-device fleet from every prior capstone, in a new format
🎓 Teach This Lesson

Listen to a guided explanation of this lesson.

Spoken narration isn't supported in this browser.

The same fleet, one more time

Every capstone since "Your First Network Automation" has used the same five-device fleet:

devices.csv — unchanged since Phase 2.6
hostname,ip,platform
SW1,10.10.10.1,cisco_ios
SW2,10.10.10.2,cisco_ios
SW3,10.10.10.3,cisco_ios
SW4,10.10.10.4,cisco_ios
SW5,10.10.10.5,cisco_ios

Three columns: hostname, ip, platform. This lesson translates exactly this data into the two inventory formats Ansible understands — nothing about the underlying fleet changes.

Ansible doesn't read CSV directly. It reads its own inventory format — in either of two shapes: INI-style, or YAML.

Quick checkConcept check
Does Ansible read a devices.csv file directly as its inventory?

INI-style inventory

inventory.ini
[switches]
SW1 ansible_host=10.10.10.1
SW2 ansible_host=10.10.10.2
SW3 ansible_host=10.10.10.3
SW4 ansible_host=10.10.10.4
SW5 ansible_host=10.10.10.5

[switches:vars]
ansible_network_os=ios
ansible_connection=network_cli

[switches] declares a group containing all five hosts. ansible_host maps each hostname to its IP — exactly devices.csv's hostname and ip columns. [switches:vars] applies shared variables to every host in the group at once, instead of repeating them per host.

platform becomes ansible_network_os — and the value itself changes too

devices.csv's platform column held Netmiko's device-type string: "cisco_ios". Ansible's own convention for the same fact is the variable ansible_network_os, and its value for Cisco IOS devices is "ios" — shorter, and specific to Ansible's own network modules. Same fact (this is a Cisco IOS device), two different tools' own naming conventions for it.

Quick checkIdentify the correct code
Which line correctly assigns SW3's management IP in INI-style inventory?

YAML inventory — the same data, Ansible's other format

inventory.yml
switches:
hosts:
  SW1:
    ansible_host: 10.10.10.1
  SW2:
    ansible_host: 10.10.10.2
  SW3:
    ansible_host: 10.10.10.3
  SW4:
    ansible_host: 10.10.10.4
  SW5:
    ansible_host: 10.10.10.5
vars:
  ansible_network_os: ios
  ansible_connection: network_cli

Identical information to inventory.ini above — a switches group, five hosts each with an ansible_host, and shared vars for the whole group. This is real, valid YAML — worth reading closely against 'Understanding YAML for Network Automation''s indentation rules from Phase 2.5.

Reading this, not writing a parser for it

This is the same YAML-reading skill Phase 2.5 built — indentation shows nesting, a colon-and-space separates key from value. What's different here is what reads it: Ansible itself parses this file when a playbook runs. Nothing in this module writes Python code to parse it — that was a deliberate choice explained further in Lesson 4.

Quick checkPredict output
In the YAML inventory above, which hosts belong to the switches group?

Groups and variables, briefly

A group (switches, here) is a named collection of hosts a playbook can target all at once, instead of listing five hostnames every time. A host variable (ansible_host) belongs to one specific host. A group variable (ansible_network_os, ansible_connection, under vars) applies to every host in the group — set once, not five times.

Quick checkScenario
A sixth switch, SW6, needs to be added to this fleet with a different connection type than the rest. Where does that difference belong?

Key takeaways

  • Ansible reads its own inventory format — INI-style or YAML — not CSV directly; devices.csv's exact data (hostname, ip, platform) translates into either.
  • platform's value changes from Netmiko's 'cisco_ios' to Ansible's own 'ios' convention for ansible_network_os — same fact, different tool's naming.
  • Groups let a playbook target many hosts at once; group variables apply to every host in a group; host variables override the group default for one specific host.
  • This is the same 5-device fleet (SW1–SW5) from every prior capstone — only the file format describing it has changed.