register variable can't be used in shell module?

when I edit a playbook test.yml:

Hii

You’re registering a variable for one host (47.108.222.84) but then try to use it for another (47.108.213.148).

I’m have no experience with kubeadm but I think your logic wrt host selection should be improved, so that you can reliably pick the variable from a stable group name, instead of “the last item”.

I know,but I want get the token in A host,use it in other hosts,how to do it ?

在2021年1月20日星期三 UTC+8 下午2:31:36dick....@geant.org 写道:

You could register the variable to a dummy host. It’s a bit of a hack imho but it does work.

https://stackoverflow.com/questions/33896847/how-do-i-set-register-a-variable-to-persist-between-plays-in-ansible

You’re looking for something like this I think (not tested) :

  • name: add master node
    shell: “kubeadm join {{ groups[‘master’]|first }}:6443 --token {{hostvars[‘master’][‘kubeadm_token’].stdout}} --discovery-token-ca-cert-hash sha256:{{hostvars[‘master’][‘kubeadm_hash’].stdout}} --control-plane”
    when: inventory_hostname != groups[‘master’]|first

Please be careful using special hostvars variable, there’s no control in this array if the variable exist, etc… so use with caution

Regards,

JYL

I’m wrong… it’s not hostvars[‘master’], because master is not the name of your first host in group master

yes,I want only get one host in member of group ‘master’,how to write it ?

[root@localhost ~]# cat /etc/ansible/hosts
[master]

47.108.222.84
47.108.234.26
47.108.213.148

only use “47.108.222.84” to do shell command

在2021年1月20日星期三 UTC+8 下午3:47:31jyle...@gmail.com 写道:

I just try to use set_fact,but only one one host: 47.108.234.26 get the token value,the other master node : 47.108.213.148 dont get it

You could try something

hostvars[groups[‘master’]|first] instead of hostvars[‘master’] in my proposition to see it that works…not sure

Use a debug module could help

Regards,

JYL

thanks,I try it :

shell: kubeadm join --control-plane {{ groups[‘master’][0] }}:6443 --token {{ hostvars[groups[‘master’]|first][‘kubeadm_token’].stdout }} --discovery-token-ca-cert-hash sha256:{{ hostvars[groups[‘master’]|first][‘kubeadm_hash’].stdout }}
but the ansible-playbook command is seizing

在2021年1月20日星期三 UTC+8 下午4:03:16jyle...@gmail.com 写道:

it works,thanks very mush

在2021年1月20日星期三 UTC+8 下午4:21:04liyo...@126.com 写道: