1
votes

I'm trying sort out my inventory.yaml file to arrange hosts by groups and then call them in an Ansible playbook.

The inventory.yaml file looks like this:

---
all:
  hosts:
    children:
      MY_Lab:
        Ubuntu_18:
          hosts:
            BWT_BKP_A:
              ansible_host: "10.2.8.19"

            BWT_BKP_B:
              ansible_host: "10.2.8.22"

            BWT_BKP_C:
              ansible_host: "10.2.9.12"
        vars:
          ansible_connection: ssh
          ansible_user: administrator
          ansible_ssh_pass: P@ssword
          ansible_sudo_pass: P@ssword

From my Ansible playbook I'd like to access hosts on Ubuntu_18 section, but I can't:

name: Test connectivity
  hosts: BWT_BKP_A
  vars:
   ansible_python_interpreter: /usr/bin/python3
  tasks:
    - name: Ping test
      ping:

I get the following error:

 ansible-playbook ping-test.yaml -i inventory.yaml
[WARNING]: Could not match supplied host pattern, ignoring: BWT_BKP_A

PLAY [Test connectivity] *****************************************************************************************************
skipping: no hosts matched

If I try the whole group, like :

hosts: Ubuntu_18

I get the same error.

Not sure what I'm missing. I'm using ansible 2.9.12

1

1 Answers

1
votes

You have an error in your inventory file. The children key should be a peer of hosts, not a child, and every time you are trying to create a "subgroup" you need another children: key.

Using this as my test inventory:

---
all:
  children:
    MY_Lab:
      children:
        Ubuntu_18:
          hosts:
            BWT_BKP_A:
              ansible_host: "127.0.0.1"

            BWT_BKP_B:
              ansible_host: "127.0.0.1"

            BWT_BKP_C:
              ansible_host: "127.0.0.1"

I can run:

$ ansible -i hosts.yml -m ping MY_Lab
BWT_BKP_B | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
BWT_BKP_A | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
BWT_BKP_C | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}

Or:

$ ansible -i hosts.yml -m ping BWT_BKP_A
BWT_BKP_A | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}

Etc.