4. Create Ansible Inventory YML Format

Preview

Welcome to the Ansible video training for network engineers, especially Cisco network engineers. In the last video we configured ansible configuration file as well as inventory file. The format of our last inventory file was the INI format. There is also another format called YAML or YML which is more readable and may be required depending on the version of ansible or module. It is therefore a good idea to learn how to create an inventory file in Yaml format, which is the subject of this video (Ansible Inventory YML Format).

Ansible Inventory YML Format

access ansible codes of this course in github

Yaml is important to us as we will be writing playbooks in YAML format throughout the course and it is the only format that ansible supports.

Uses a specific YAML file as an inventory source in Ansible Website

this is a video-based training therefore the output of running commands are not show in the text.

All YAML can optionally begin with —

nearly every YAML file starts with a list. Each item in the list is a list of key/value pairs, commonly called a “hash” or a “dictionary”.

There are three important keys that are mostly used in the inventory file in Ansible. “children” to create subgroups, “hosts” to add devices to the device list, and “vars” to define variables.
Another important point about the Yaml file is indentation. This means that all keys and values at the same level must have the same indentation. This is the only way to separate key/value pairs without parentheses or brackets.

To better understand how we create a Yaml format inventory file, let’s convert our traditional INI format to a new Yaml format.

Let first create the groups, then the devices and then the variables.

“all” is always a super group. In our inventory file we have “router” and “srx” groups as subgroups and under “router” we also have “cisco” and “junos” subgroups. So first we create groups with the help of “children key”.

Then we will create devices with the help of “hosts” key.

Then we will create variables with the help of “vars”

This the result of inventory file in the format of YAML.

Now let’s check Ansible’s connectivity to the devices, this time using this new inventory.

root@debian:~/ansible-project1# cat hosts.yml
---
  all:
    vars:
      ansible_user: rayka
      ansible_password: rayka-co.com
      ansible_port: 22
      ansible_connection: ansible.netcommon.network_cli
      ansible_network_os: cisco.ios.ios
    children:
      csr:
        hosts:
          csr1:
          csr2:
      router:
        children:
          cisco:
            hosts:
              core1:
              core2:
              csr1:
              csr2:
          junos:
            vars:
              ansible_user: root
              ansible_port: 2222
            hosts:
              access1:
                vars:
                  ansible_host: 10.10.10.1
                  ansible_user: sara
                  ansible_port: 2222
              access2:
      srx:
        hosts:
          srx1:
          srx2:
          srx3:
Ansible Inventory YML Format
Ansible Inventory YML Format

In the next video we will look at some Cisco specific modules.

Back to: Ansible for Network Engineers > basic Configurations and adhoc Commands

Leave a comment

Your email address will not be published. Required fields are marked *