Build Your Inventory

A fully-featured inventory file can serve as the source of truth for your network. Using an inventory file, a single playbook can maintain hundreds of network devices with a single command. This page shows you how to build an inventory file, step by step.

Basic Inventory

First, group your inventory logically. Best practice is to group servers and network devices by their What (application, stack or microservice), Where (datacenter or region), and When (development stage):

  • What: db, web, leaf, spine
  • Where: east, west, floor_19, building_A
  • When: dev, test, staging, prod

Avoid spaces, hyphens, and preceding numbers (use floor_19, not 19th_floor) in your group names. Group names are case sensitive.

This tiny example data center illustrates a basic group structure. You can group groups using the syntax [metagroupname:children] and listing groups as members of the metagroup. Here, the group network includes all leafs and all spines; the group datacenter includes all network devices plus all webservers.

  1. [leafs]
  2. leaf01
  3. leaf02
  4.  
  5. [spines]
  6. spine01
  7. spine02
  8.  
  9. [network:children]
  10. leafs
  11. spines
  12.  
  13. [webservers]
  14. webserver01
  15. webserver02
  16.  
  17. [datacenter:children]
  18. network
  19. webservers

Add Variables to Inventory

Next, you can set values for many of the variables you needed in your first Ansible command in the inventory, so you can skip them in the ansible-playbook command. In this example, the inventory includes each network device’s IP, OS, and SSH user. If your network devices are only accessible by IP, you must add the IP to the inventory file. If you access your network devices using hostnames, the IP is not necessary.

  1. [leafs]
  2. leaf01 ansible_host=10.16.10.11 ansible_network_os=vyos ansible_user=my_vyos_user
  3. leaf02 ansible_host=10.16.10.12 ansible_network_os=vyos ansible_user=my_vyos_user
  4.  
  5. [spines]
  6. spine01 ansible_host=10.16.10.13 ansible_network_os=vyos ansible_user=my_vyos_user
  7. spine02 ansible_host=10.16.10.14 ansible_network_os=vyos ansible_user=my_vyos_user
  8.  
  9. [network:children]
  10. leafs
  11. spines
  12.  
  13. [servers]
  14. server01 ansible_host=10.16.10.15 ansible_user=my_server_user
  15. server02 ansible_host=10.16.10.16 ansible_user=my_server_user
  16.  
  17. [datacenter:children]
  18. leafs
  19. spines
  20. servers

Group Variables within Inventory

When devices in a group share the same variable values, such as OS or SSH user, you can reduce duplication and simplify maintenance by consolidating these into group variables:

  1. [leafs]
  2. leaf01 ansible_host=10.16.10.11
  3. leaf02 ansible_host=10.16.10.12
  4.  
  5. [leafs:vars]
  6. ansible_network_os=vyos
  7. ansible_user=my_vyos_user
  8.  
  9. [spines]
  10. spine01 ansible_host=10.16.10.13
  11. spine02 ansible_host=10.16.10.14
  12.  
  13. [spines:vars]
  14. ansible_network_os=vyos
  15. ansible_user=my_vyos_user
  16.  
  17. [network:children]
  18. leafs
  19. spines
  20.  
  21. [servers]
  22. server01 ansible_host=10.16.10.15
  23. server02 ansible_host=10.16.10.16
  24.  
  25. [datacenter:children]
  26. leafs
  27. spines
  28. servers

Variable Syntax

The syntax for variable values is different in inventory, in playbooks and in group_vars files, which are covered below. Even though playbook and group_vars files are both written in YAML, you use variables differently in each.

  • In an ini-style inventory file you must use the syntax key=value for variable values: ansible_network_os=vyos.
  • In any file with the .yml or .yaml extension, including playbooks and group_vars files, you must use YAML syntax: key: value
    • In group_vars files, use the full key name: ansible_network_os: vyos.
    • In playbooks, use the short-form key name, which drops the ansible prefix: network_os: vyos

Group Inventory by Platform

As your inventory grows, you may want to group devices by platform. This allows you to specify platform-specific variables easily for all devices on that platform:

  1. [vyos_leafs]
  2. leaf01 ansible_host=10.16.10.11
  3. leaf02 ansible_host=10.16.10.12
  4.  
  5. [vyos_spines]
  6. spine01 ansible_host=10.16.10.13
  7. spine02 ansible_host=10.16.10.14
  8.  
  9. [vyos:children]
  10. vyos_leafs
  11. vyos_spines
  12.  
  13. [vyos:vars]
  14. ansible_connection=network_cli
  15. ansible_network_os=vyos
  16. ansible_user=my_vyos_user
  17.  
  18. [network:children]
  19. vyos
  20.  
  21. [servers]
  22. server01 ansible_host=10.16.10.15
  23. server02 ansible_host=10.16.10.16
  24.  
  25. [datacenter:children]
  26. vyos
  27. servers

With this setup, you can run first_playbook.yml with only two flags:

  1. ansible-playbook -i inventory -k first_playbook.yml

With the -k flag, you provide the SSH password(s) at the prompt. Alternatively, you can store SSH and other secrets and passwords securely in your group_vars files with ansible-vault.

Protecting Sensitive Variables with ansible-vault

The ansible-vault command provides encryption for files and/or individual variables like passwords. This tutorial will show you how to encrypt a single SSH password. You can use the commands below to encrypt other sensitive information, such as database passwords, privilege-escalation passwords and more.

First you must create a password for ansible-vault itself. It is used as the encryption key, and with this you can encrypt dozens of different passwords across your Ansible project. You can access all those secrets (encrypted values) with a single password (the ansible-vault password) when you run your playbooks. Here’s a simple example.

Create a file and write your password for ansible-vault to it:

  1. echo "my-ansible-vault-pw" > ~/my-ansible-vault-pw-file

Create the encrypted ssh password for your VyOS network devices, pulling your ansible-vault password from the file you just created:

  1. ansible-vault encrypt_string --vault-id [email protected]~/my-ansible-vault-pw-file 'VyOS_SSH_password' --name 'ansible_password'

If you prefer to type your ansible-vault password rather than store it in a file, you can request a prompt:

  1. ansible-vault encrypt_string --vault-id [email protected] 'VyOS_SSH_password' --name 'ansible_password'

and type in the vault password for my_user.

The —vault-id flag allows different vault passwords for different users or different levels of access. The output includes the user name my_user from your ansible-vault command and uses the YAML syntax key: value:

  1. ansible_password: !vault |
  2. $ANSIBLE_VAULT;1.2;AES256;my_user
  3. 66386134653765386232383236303063623663343437643766386435663632343266393064373933
  4. 3661666132363339303639353538316662616638356631650a316338316663666439383138353032
  5. 63393934343937373637306162366265383461316334383132626462656463363630613832313562
  6. 3837646266663835640a313164343535316666653031353763613037656362613535633538386539
  7. 65656439626166666363323435613131643066353762333232326232323565376635
  8. Encryption successful

This is an example using an extract from a YAML inventory, as the INI format does not support inline vaults:

  1. ...
  2.  
  3. vyos: # this is a group in yaml inventory, but you can also do under a host
  4. vars:
  5. ansible_connection: network_cli
  6. ansible_network_os: vyos
  7. ansible_user: my_vyos_user
  8. ansible_password: !vault |
  9. $ANSIBLE_VAULT;1.2;AES256;my_user
  10. 66386134653765386232383236303063623663343437643766386435663632343266393064373933
  11. 3661666132363339303639353538316662616638356631650a316338316663666439383138353032
  12. 63393934343937373637306162366265383461316334383132626462656463363630613832313562
  13. 3837646266663835640a313164343535316666653031353763613037656362613535633538386539
  14. 65656439626166666363323435613131643066353762333232326232323565376635
  15.  
  16. ...

To use an inline vaulted variables with an INI inventory you need to store it in a ‘vars’ file in YAML format,it can reside in host_vars/ or group_vars/ to be automatically picked up or referenced from a play via vars_files or include_vars.

To run a playbook with this setup, drop the -k flag and add a flag for your vault-id:

  1. ansible-playbook -i inventory --vault-id [email protected]~/my-ansible-vault-pw-file first_playbook.yml

Or with a prompt instead of the vault password file:

  1. ansible-playbook -i inventory --vault-id [email protected] first_playbook.yml

To see the original value, you can use the debug module. Please note if your YAML file defines the ansible_connection variable (as we used in our example), it will take effect when you execute the command below. To prevent this, please make a copy of the file without the ansible_connection variable.

  1. cat vyos.yml | grep -v ansible_connection >> vyos_no_connection.yml
  2.  
  3. ansible localhost -m debug -a var="ansible_password" -e "@vyos_no_connection.yml" --ask-vault-pass
  4. Vault password:
  5.  
  6. localhost | SUCCESS => {
  7. "ansible_password": "VyOS_SSH_password"
  8. }

Warning

Vault content can only be decrypted with the password that was used to encrypt it. If you want to stop using one password and move to a new one, you can update and re-encrypt existing vault content with ansible-vault rekey myfile, then provide the old password and the new password. Copies of vault content still encrypted with the old password can still be decrypted with old password.

For more details on building inventory files, see the introduction to inventory; for more details on ansible-vault, see the full Ansible Vault documentation.

Now that you understand the basics of commands, playbooks, and inventory, it’s time to explore some more complex Ansible Network examples.