# how to get the host in groups children?

**URL:** <https://forum.ansible.com/t/how-to-get-the-host-in-groups-children/34485>\
**Category:** Ansible Project\
**Tags:** kubernetes\
**Created:** [January 21, 2021, 6:18am UTC](https://forum.ansible.com/t/how-to-get-the-host-in-groups-children/34485 "2021-01-21T06:18:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![liyo](https://avatars.discourse-cdn.com/v4/letter/l/5fc32e/32.png) [@liyo](https://forum.ansible.com/u/liyo)\
**Post date:** [January 21, 2021, 6:18am UTC](https://forum.ansible.com/t/how-to-get-the-host-in-groups-children/34485/1 "2021-01-21T06:18:35Z")

</div>

my inventroy file is below:

[master]  
172.24.64.44  
172.24.64.42  
172.24.64.43  
[worker]  
172.24.64.46

[cluster:children]  
master  
worker

I want to exec the playbook in [cluster:children]'s master 172.24.64.44,want to get the variables value ,then use it in other group ,how to write the “when” ? please help me ?

my playbook is :

- name: Get master token  
shell: kubeadm token list |grep forever|awk ‘{print $1}’  
register: kubeadm\_token  
when: inventory\_hostname == groups[‘cluster:children’][‘master’]|first

- name: Get master hash  
shell: “openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2\>/dev/null | openssl dgst -sha256 -hex | sed ‘s/^ .\* //’|cut -d’ ’ -f2”  
register: kubeadm\_hash  
when: xxxxxxxxxxxxxxxxxxxxxx

- name: Add master node  
shell:  
cmd: kubeadm join {{ groups[‘master’][0] }}:6443 --token {{ hostvars[groups[‘cluster’][‘master’]|first][‘kubeadm\_token’].stdout }} --discovery-token-ca-cert-hash sha256:{{ hostvars[groups[‘master’]|first][‘kubeadm\_hash’].stdout }}  
when:

- inventory\_hostname in groups[‘worker’]

I want to get the token and hash value,then use it in “worker” groups

---

<div class="post-metadata">

**Author:** ![jean-yves](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@jean-yves](https://forum.ansible.com/u/jean-yves)\
**Post date:** [January 21, 2021, 7:37am UTC](https://forum.ansible.com/t/how-to-get-the-host-in-groups-children/34485/2 "2021-01-21T07:37:11Z")

</div>

Hi,

1/

Sounds like you use a lot the when clause to limit the impact of action to some servers.

Why you did’nt use more the hosts clause to handle the action on specific hosts ?

You can have more than one hosts clause in a playbook

Notice that you can reuse variable in a previous play in another play also.

2/

What do you want to achieve really ?

3/

Sounds weird to have three master and only one worker in a kubernetes cluster… The other way around is more “common”

Regards,

JYL
