EX294 2.3: Static inventories and groups
Write an inventory in INI and in YAML, with a group of groups and a group variable, and add the control node so its tasks run locally.
Before you start
This VM is the control node. The lab creates the user greg (password
Lab-Pass-1) with an SSH key, and installs ansible-core. Work as greg,
in /home/greg/ansible: its ansible.cfg reads the inventory
./inventory, which doesn't exist yet, and logs in as devops.
The managed nodes node1 (10.99.1.11), node2 (10.99.1.12) and node3
(10.99.1.13) are AlmaLinux 10 containers reached over SSH by name. On
each, devops accepts greg's key. For a root shell on a node,
sudo podman exec -it node1 bash.
Tasks
- Write
/home/greg/ansible/inventoryin INI format, with node1 in groupdev, node2 intest, node3 inprod, and a groupwebserversthat contains the groupprod.ansible-inventory --graphshows node3 under bothprodandwebservers, andansible webservers --list-hostslists node3. - Write the same inventory in YAML as
/home/greg/ansible/inventory.yml, with a variable set on thedevgroup.ansible-inventory -i inventory.yml --graph --varsshows the same tree, with the variable on node1. - Add the control node to
/home/greg/ansible/inventoryascontrol, so tasks for it run locally:ansible control -a hostnameprints this VM's name without SSH.
Hints
[group:children]lists groups;[group:vars]sets variables.- In YAML,
hosts:andchildren:map names to their contents, even when empty:node1:with a trailing colon. Group variables go undervars:. ansible-inventory -y --listprints an INI inventory as YAML.ansible_connection=localon the host's line.ansible all -m pingchecks every node answers.