# Re-using "register" variable in multiple tasks

**URL:** <https://forum.ansible.com/t/re-using-register-variable-in-multiple-tasks/20604>\
**Category:** Ansible Project\
**Created:** [February 17, 2016, 5:07pm UTC](https://forum.ansible.com/t/re-using-register-variable-in-multiple-tasks/20604 "2016-02-17T17:07:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dyioulos](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dyioulos/32/8363_2.png) [@dyioulos](https://forum.ansible.com/u/dyioulos)\
**Post date:** [February 17, 2016, 5:07pm UTC](https://forum.ansible.com/t/re-using-register-variable-in-multiple-tasks/20604/1 "2016-02-17T17:07:40Z")

</div>

I have the following play in a playbook:

- hosts: talk

gather\_facts: false

tasks:

- name: win update  
win\_updates:  
category: [‘SecurityUpdates’,‘CriticalUpdates’,‘Updates’,‘Tools’,‘DefinitionUpdates’,‘UpdateRollups’]  
register: reboot\_hint

I’d like to re-use reboot\_hint in additional plays in the playbook via “when”, but the playbook ultimately fails after the first “when”, as in:

- hosts: talk  
gather\_facts: false

tasks:

- name: start Lyris services

win\_service: name={{ item }} state=started start\_mode=auto  
with\_items: [“ListManager”, “LyrisAlert”]  
when: (lyris\_hint.rc == 0) and (reboot\_hint.reboot\_required == true) # ie, the ListManager service is installed and reboot is required

- hosts: admin2  
gather\_facts: false  
sudo: yes

tasks:

- nagios: action=downtime minutes=10 author=“Dimitri Yioulos” service=host host={{item}}  
with\_items:

- talk  
when: reboot\_hint.reboot\_required == true  
delegate\_to: admin2

- nagios: action=downtime minutes=20 author=“Dimitri Yioulos” service=all host={{item}}

with\_items:

- talk  
when: reboot\_hint.reboot\_required == true  
delegate\_to: admin2  
tags:

- nagios\_downtime

- hosts: talk

gather\_facts: false

tasks:

- name: reboot server  
raw: ‘cmd /c shutdown /r /t 0’  
when: reboot\_hint.reboot\_required == true

This is another of my Ansible 101 questions; help making it work is appreciated!

---

<div class="post-metadata">

**Author:** ![dyioulos](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dyioulos/32/8363_2.png) [@dyioulos](https://forum.ansible.com/u/dyioulos)\
**Post date:** [February 17, 2016, 9:55pm UTC](https://forum.ansible.com/t/re-using-register-variable-in-multiple-tasks/20604/2 "2016-02-17T21:55:39Z")

</div>

I’ve found that my register variable only fails when applied to the play involving another host in my playbook. After having poured over lots of posts from various other sources, I saw one that says, “use hostvars to access the register variables from a different host”. If, indeed, that’s the solution, how do I use hostvars to achieve it?

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [February 20, 2016, 5:52pm UTC](https://forum.ansible.com/t/re-using-register-variable-in-multiple-tasks/20604/3 "2016-02-20T17:52:12Z")

</div>

hostvars[\<otherhost][\<varname]

---

<div class="post-metadata">

**Author:** ![dyioulos](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dyioulos/32/8363_2.png) [@dyioulos](https://forum.ansible.com/u/dyioulos)\
**Post date:** [February 20, 2016, 7:16pm UTC](https://forum.ansible.com/t/re-using-register-variable-in-multiple-tasks/20604/4 "2016-02-20T19:16:51Z")

</div>

Thanks, Brian. OK, I’d like to use that in a “when” with the register variable “reboot\_hint.reboot\_required == true”. Will the following work?:

when: hostvars[myhost][reboot\_hint.reboot\_required == true]

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [February 20, 2016, 7:48pm UTC](https://forum.ansible.com/t/re-using-register-variable-in-multiple-tasks/20604/5 "2016-02-20T19:48:20Z")

</div>

no, you are conflating several things there

when: hostvars[myhost][‘reboot\_hint’][‘reboot\_required’]

^ that will execute when reboot\_required is true.

---

<div class="post-metadata">

**Author:** ![dyioulos](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dyioulos/32/8363_2.png) [@dyioulos](https://forum.ansible.com/u/dyioulos)\
**Post date:** [February 22, 2016, 5:13pm UTC](https://forum.ansible.com/t/re-using-register-variable-in-multiple-tasks/20604/6 "2016-02-22T17:13:46Z")

</div>

OK. Here’s what I’m trying to accomplish:

- hosts: all  
gather\_facts: false

tasks:

- name: win update  
win\_updates:  
category: [‘SecurityUpdates’,‘CriticalUpdates’,‘Updates’,‘Tools’,‘DefinitionUpdates’,‘UpdateRollups’]  
register: reboot\_hint

- hosts: admin2  
gather\_facts: false  
sudo: yes

tasks:

- nagios: action=downtime minutes=20 author=“Dimitri Yioulos” service=host host={{item}}  
with\_items: “{{ groups[‘lyris’] }}”  
delegate\_to: admin2

# when: hostvars[myhost][‘reboot\_hint’][‘reboot\_required’]

tags:

- nagios\_downtime
- nagios: action=downtime minutes=20 author=“Dimitri Yioulos” service=all host={{item}}  
with\_items: “{{ groups[‘lyris’] }}”  
delegate\_to: admin2

# when: hostvars[myhost][‘reboot\_hint’][‘reboot\_required’]

Since “reboot\_hint” is coming from the previous play, and since the host or group to run the playbook against is defined at the command line, as in: “ansible-playbook -l lyris myplaybook.yml -u myuser”, what do I specify in the [myhost] section? This must be a variable to accept the host name OR group name I specified at the command line, but I don’t know what that would be.

Further, and maybe another post altogether, how do I make the substitution in “with\_items: “{{ groups[‘lyris’] }}”” so that I don’t have to change e.g. lyris is I’m specifying another host or group in running the playbook?

With thanks,

Dimitri
