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

@KodeKloud 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 @KodeKloud @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