Team, I was working on the ansible inventory .. I have created this inventory w . . .

Subhankar Banerjee:
Team, I was working on the ansible inventory … I have created this inventory with host & IP . now I need to fetch that group name using playbook …while running I will give any of the machine name… command : ansible-playbook ./04-group-name.yaml --limit kube-worker … but that I am getting an error

WARNING]: Could not match supplied host pattern, ignoring: kube-worker
ERROR! Specified hosts and/or --limit does not match any hosts

---
- hosts: all
  gather_facts: no
  tasks:
  - name: show the groups the host(s) are in
    debug:
      msg: "{{group_names}}"
all:
 children: 
   master:
    children:
      p2:
       hosts:
        kube-master 10.128.0.3
   worker:
    children:
      p2: 
       hosts:
        kube-worker 10.128.0.4
#localhost 10.120.0.2

Al West:
I don’t think you have created your inventory correctly:

al@Delos:~/kodekloud/ansible-inventory$ ansible-inventory -i yaml_invetory.yaml --yaml --list
all:
  children:
    master:
      children:
        p2:
          hosts:
            kube-master 10.128.0.3: {}
            kube-worker 10.128.0.4: {}
    worker:
      children:
        p2: {}
al@Delos:~/kodekloud/ansible-inventory$ ansible-playbook groups.yaml -i yaml_invetory.yaml

PLAY [all] ************************************************************************************************************
TASK [show the groups the host(s) are in] *****************************************************************************
ok: [kube-master 10.128.0.3] => {
    "msg": [
        "master",
        "p2",
        "worker"
    ]
}
ok: [kube-worker 10.128.0.4] => {
    "msg": [
        "master",
        "p2",
        "worker"
    ]
}

PLAY RECAP ************************************************************************************************************
kube-master 10.128.0.3     : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
kube-worker 10.128.0.4     : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Subhankar Banerjee:
thanks for the information … but I was checking if I passed the hostname in command: ansible-inventory -i yaml_invetory.yaml --yaml --list --limit <host_name> whether it will return group name or not … I need to collect group name like master or worker from the hostname…

Al West:
Did you not see the parsed inventory I post at the top of my message? It seems your inventory is not quite correct.

Al West:
Most people use toml as it is clearer to understand

Subhankar Banerjee:
not sure . I checked this

Al West:
Why is p2 repeated?

Al West:
This is what it looks like when converted to toml:

al@Delos:~/kodekloud/ansible-inventory$ ansible-inventory -i yaml_invetory.yaml --toml --list
[master]
children = [ "p2",]

[worker]
children = [ "p2",]

[p2.hosts."kube-master 10.128.0.3"]

[p2.hosts."kube-worker 10.128.0.4"]

Subhankar Banerjee:
assum p2 is the region , so master can be in p2 region and worker can be in p2 region

Subhankar Banerjee:
but the question is how to get only the group name like master …I will give in my command the host name

Al West:
Which group - with your inventory they are in 3 groups, as shown in my output above.

Subhankar Banerjee:
yes, it has 3 groups…I need only master or worker

Al West:
Have you read what I have posted carefully?

Al West:
Because your hosts are in all 3 groups.

Subhankar Banerjee:
that is group and parentgroup I hope

Subhankar Banerjee:
FYI … I am referring to this …trying to test my logic

Al West:
notice the groups are not repeated

Subhankar Banerjee:
yes you are correct

Subhankar Banerjee:
in the above screenshot how can I get only group name webservers from the hostname ?

Al West:
Please paste this sort of information as text in code blocks.