how to get the host in groups children?

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

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