# register variable can't be used in shell module?

**URL:** <https://forum.ansible.com/t/register-variable-cant-be-used-in-shell-module/34476>\
**Category:** Ansible Project\
**Created:** [January 20, 2021, 4:26am UTC](https://forum.ansible.com/t/register-variable-cant-be-used-in-shell-module/34476 "2021-01-20T04:26:25Z")\
**Posts on this page:** 11\
**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 20, 2021, 4:26am UTC](https://forum.ansible.com/t/register-variable-cant-be-used-in-shell-module/34476/1 "2021-01-20T04:26:25Z")

</div>

when I edit a playbook test.yml:

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [January 20, 2021, 6:31am UTC](https://forum.ansible.com/t/register-variable-cant-be-used-in-shell-module/34476/2 "2021-01-20T06:31:11Z")

</div>

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”.

---

<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 20, 2021, 7:17am UTC](https://forum.ansible.com/t/register-variable-cant-be-used-in-shell-module/34476/3 "2021-01-20T07:17:31Z")

</div>

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:36[dick....@geant.org](mailto:dick....@geant.org) 写道：

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [January 20, 2021, 7:20am UTC](https://forum.ansible.com/t/register-variable-cant-be-used-in-shell-module/34476/4 "2021-01-20T07:20:50Z")

</div>

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](https://stackoverflow.com/questions/33896847/how-do-i-set-register-a-variable-to-persist-between-plays-in-ansible)

---

<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 20, 2021, 7:33am UTC](https://forum.ansible.com/t/register-variable-cant-be-used-in-shell-module/34476/5 "2021-01-20T07:33:57Z")

</div>

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

---

<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 20, 2021, 7:47am UTC](https://forum.ansible.com/t/register-variable-cant-be-used-in-shell-module/34476/6 "2021-01-20T07:47:16Z")

</div>

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

---

<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 20, 2021, 7:53am UTC](https://forum.ansible.com/t/register-variable-cant-be-used-in-shell-module/34476/7 "2021-01-20T07:53:15Z")

</div>

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:31[jyle...@gmail.com](mailto:jyle...@gmail.com) 写道：

---

<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 20, 2021, 7:59am UTC](https://forum.ansible.com/t/register-variable-cant-be-used-in-shell-module/34476/8 "2021-01-20T07:59:54Z")

</div>

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

---

<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 20, 2021, 8:03am UTC](https://forum.ansible.com/t/register-variable-cant-be-used-in-shell-module/34476/9 "2021-01-20T08:03:01Z")

</div>

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

---

<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 20, 2021, 8:21am UTC](https://forum.ansible.com/t/register-variable-cant-be-used-in-shell-module/34476/10 "2021-01-20T08:21:04Z")

</div>

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:16[jyle...@gmail.com](mailto:jyle...@gmail.com) 写道：

---

<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 20, 2021, 8:32am UTC](https://forum.ansible.com/t/register-variable-cant-be-used-in-shell-module/34476/11 "2021-01-20T08:32:54Z")

</div>

it works，thanks very mush

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