Ansible Inventory

Hi Team

What is the use of this syntax

[all_servers:childern] ----> what is the need of this children term here
web_servers
db_servers

Hello, @Aashiqahamed

It is possible to aggregate multiple groups as children under a ā€œparentā€ group. The ā€œparentā€ is then called a metagroup . The following example demonstrates another way to organize the previous inventory using metagroups to achieve a comparable, yet more granular arrangement:

[web_dev]
203.0.113.111

[web_prod]
203.0.113.112

[db_dev]
203.0.113.113

[db_prod]
server_hostname

[webservers:children]
web_dev
web_prod

[dbservers:children]
db_dev
db_prod

[development:children]
web_dev
db_dev

[production:children]
web_prod
db_prod

The more servers you have, the more it makes sense to break groups down or create alternative arrangements so that you can target smaller groups of servers as needed.

1 Like

@Ayman to be honest, I did not understand the purpose of children :smiley:

why donā€™t we put it simply and write it like the following ?
[web_dev]
203.0.113.111

[web_prod]
203.0.113.112

[db_dev]
203.0.113.113

[db_prod]
server_hostname

[webservers]
web_dev
web_prod

[dbservers]
db_dev
db_prod

[development
web_dev
db_dev

[production]
web_prod
db_prod

1 Like

Would you please let us know how we can refer to a hostgroup ā€œwebservers:childrenā€ in a playbook?

1 Like

Itā€™s just about the arrangement when you have a lot of servers.
In your playbook:

---
- hosts: webservers
2 Likes

cc: @Aashiqahamed, @Nasri

if you do not write :children, then in that case, ansible will treat web_servers and db_servers as individual hostnames/aliases, and will try to resolve them for DNS, which will fail.

Adding :children tells ansible to look for groups named under children and not treat them as hosts

Sample inventory for test

[web]
web1 ansible_host=10.23.43.122
web2 ansible_host=10.23.43.122

[db]
db1 ansible_host=10.23.43.122
db2 ansible_host=10.23.43.122

[app]
web
db

Result with above inventory

āžœ ansible -m ping app
[WARNING]: Found both group and host with same name: db
[WARNING]: Found both group and host with same name: web
web | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: ssh: Could not resolve hostname web: nodename nor servname provided, or not known",
    "unreachable": true
}
db | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: ssh: Could not resolve hostname db: nodename nor servname provided, or not known",
    "unreachable": true
}

Main error
ssh: Could not resolve hostname web: nodename nor servname provided, or not known

1 Like

@vipra @Ayman @Nasri

Thank you for your clarification, so when ansible finds the term children then it will treat the list under it as group of servers instead of trying to resolve them under DNS

so in a playbook we should use like the following one right ?


  • name : example playbook
    hosts: all_servers:childern
    tasks:
    • name : some tasks hereā€¦

i think its

name : example playbook
hosts: all_server
tasks:

  • name : some tasks hereā€¦

and all_servers:childern is mentioned in inventory

Ansible will look at all_servers in playbook and refer inventory, it finds :children beside all_servers and search for the list of servers in inventory itself instead of resolving in DNS

1 Like

You can use this command to show how Ansible views the inventory:

ansible-inventory --list

If you want to check a specific inventory file (or directory/wildcard) use:

ansible-inventory --list -i [inventory_filename]

Given this inventory.txt

[test]
alpha.example.org
beta.example.org

[test:vars]
test_username=admin
test_password=admin

[web]
www[01:03].example.com

[db]
db-[99:101]-node.example.com

[prod:children]
web
db

Here are some example outputs:

$ ansible-inventory --list -i inventory.txt
{
    "_meta": {
        "hostvars": {
            "alpha.example.org": {
                "test_password": "admin",
                "test_username": "admin"
            },
            "beta.example.org": {
                "test_password": "admin",
                "test_username": "admin"
            }
        }
    },
    "all": {
        "children": [
            "prod",
            "test",
            "ungrouped"
        ]
    },
    "db": {
        "hosts": [
            "db-100-node.example.com",
            "db-101-node.example.com",
            "db-99-node.example.com"
        ]
    },
    "prod": {
        "children": [
            "db",
            "web"
        ]
    },
    "test": {
        "hosts": [
            "alpha.example.org",
            "beta.example.org"
        ]
    },
    "web": {
        "hosts": [
            "www01.example.com",
            "www02.example.com",
            "www03.example.com"
        ]
    }
}
$ ansible-inventory --graph prod -i inventory.txt
@prod:
  |--@db:
  |  |--db-100-node.example.com
  |  |--db-101-node.example.com
  |  |--db-99-node.example.com
  |--@web:
  |  |--www01.example.com
  |  |--www02.example.com
  |  |--www03.example.com

Hi @tabernarious thank you for this info, I am really amazed to find that ansible will search all these different locations for inv file/path

hi team,
at initial stage trying to create inventory.txt file in ansible controller machine .below is txt file
ansibletarget ansible-host=192.168.0.121 ansible-ssh-pass=osboxes.org
ansibletargetā€“target machine name
target machine ip addrees=192.168.0.121
when trying to run command: ansible ansibletarget -m ping -i inventory.txt
getting below error:
ā€¦
ansibletarget | UNREACHABLE! => {
ā€œchangedā€: false,
ā€œmsgā€: ā€œFailed to connect to the host via ssh: ssh: Could not resolve hostname ansibletarget: Name or service not knownā€,
ā€œunreachableā€: true
}
ā€¦
can you plz help me to solved this issue?

Could not resolve hostname ansibletarget: Name or service not known

Did you write this hostname in your hosts file? Because the system is not able to identify the hostname.

ansibletarget ansible-host=192.168.0.121 ansible-ssh-pass=osboxes.org

I did a few corrections.

ansibletarget ansible_host=192.168.0.121 ansible_ssh_pass=osboxes.org ansible_user=<TYPE-USER-NAME> ansible_connection=ssh

Try this.


ansible -m ping -i inventory.txt

hi @Tej-Singh-Rana ,follow above steps getting below error now

ā€¦
ansibletarget | FAILED! => {
ā€œmsgā€: ā€œto use the ā€˜sshā€™ connection type with passwords, you must install the sshpass programā€
}

ā€¦

can you plz help on this?

team,any solution on above issue?

Hi @namratasawalamdocs,
You have to install the sshpass package on the ansibletarget node.

Regards,

Whatā€™s the OS type of the ansibletarget node?

If itā€™s CentOS then use the following command: -

sudo yum install sshpass

If itā€™s Ubuntu then use the following command: -

sudo apt-get install sshpass

hi @Tej-Singh-Rana ,yesterday i tried to install sshpass library but getting below error
ā€¦

[osboxes@ansiblecontroller test-project]$ sudo yum install sshpass
Last metadata expiration check: 8:23:04 ago on Tue 21 Feb 2023 01:15:20 AM EST.
No match for argument: sshpass
Error: Unable to find a match: sshpass
ā€¦
on both host machine and target machine .try to solved this issue on various side but didnt get any solution can you help me on this?

Did you run sudo yum update?

yes i tried
[osboxes@ansiblecontroller test-project]$ sudo yum update
[sudo] password for osboxes:
Last metadata expiration check: 9:04:06 ago on Tue 21 Feb 2023 01:15:20 AM EST.
Dependencies resolved.
Nothing to do.
Complete!
[osboxes@ansiblecontroller test-project]$ sudo yum install sshpass
Last metadata expiration check: 9:04:11 ago on Tue 21 Feb 2023 01:15:20 AM EST.
No match for argument: sshpass
Error: Unable to find a match: sshpass