# Need a register value to persist

**URL:** <https://forum.ansible.com/t/need-a-register-value-to-persist/28414>\
**Category:** Ansible Project\
**Created:** [August 24, 2018, 12:44pm UTC](https://forum.ansible.com/t/need-a-register-value-to-persist/28414 "2018-08-24T12:44:40Z")\
**Posts on this page:** 10\
**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:** [August 24, 2018, 12:44pm UTC](https://forum.ansible.com/t/need-a-register-value-to-persist/28414/1 "2018-08-24T12:44:40Z")

</div>

Hello, all.

I’ve created the following playbook:

> * * *
> 
> - hosts: Host1  
> gather\_facts: false  
> tasks:
> - name: SQL Query Pending Import Jobs  
> script: /etc/ansible/files/mssql\_opm\_getImportJobs.ps1  
> register: import\_job\_count
> - debug:  
> msg: “Number of running imports: {{ import\_job\_count.rc }}”  
> tags:
> - test
> - hosts: Host2  
> gather\_facts: false  
> tasks:
> - name: Stop 1Point Import Services  
> win\_service: name={{ item }} state=stopped  
> with\_items: [“ImportRecipientsService”,“ImportRecipientsService\_V2”,“OnepointImportService\_High”,“OnepointImportService\_Higher”,“OnepointImportService\_Highest”,“OnepointImportServi  
> ce\_Low”]  
> when: import\_job\_count.stdout == “0”  
> tags:
> - stop\_opm\_import\_services

As I hope you can see, the idea is to use the register value from the first play to work in the second, which is aimed at another host. I get “‘import\_job\_count’ is undefined” because the register value doesn’t persist. Is there any way to do this?

With thanks.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [August 24, 2018, 1:40pm UTC](https://forum.ansible.com/t/need-a-register-value-to-persist/28414/2 "2018-08-24T13:40:26Z")

</div>

Register variables is per host not global, so you need to specify the host you want to retrieve it from.  
To do this you need to use hostvars  
&nbsp;&nbsp;hostvars['Host1'].import\_job\_count.stdout

---

<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:** [August 24, 2018, 1:54pm UTC](https://forum.ansible.com/t/need-a-register-value-to-persist/28414/3 "2018-08-24T13:54:00Z")

</div>

Beautiful, Kai, thanks! I was just testing something like that, and it seems to work:

- hosts: Host2  
gather\_facts: false  
tasks:
- name: Stop 1Point Import Services  
win\_service: name={{ item }} state=stopped  
with\_items: [“ImportRecipientsService”,“ImportRecipientsService\_V2”,“OnepointImportService\_High”,“OnepointImportService\_Higher”,“OnepointImportService\_Highest”,“OnepointImportServi  
ce\_Low”]  
**when: hostvars[‘Host1’][‘import\_job\_count’] is defined**  
tags:
- stop\_opm\_import\_services

Does that seem right to you?

---

<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:** [August 24, 2018, 2:08pm UTC](https://forum.ansible.com/t/need-a-register-value-to-persist/28414/4 "2018-08-24T14:08:11Z")

</div>

Actually, the value of " import\_job\_count" must be 0 (zero) in order for the play to proceed. How do I do that?

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [August 24, 2018, 2:08pm UTC](https://forum.ansible.com/t/need-a-register-value-to-persist/28414/5 "2018-08-24T14:08:21Z")

</div>

Yes, but a registered variable will always be defined if the task is run or skipped.  
So if you have the play Host1 before this, the variable import\_job\_count will be defined and the when will always be true.

And a tips to improve readability, you can write your with\_items like this.

&nbsp;&nbsp;with\_items:  
&nbsp;&nbsp;&nbsp;&nbsp;- ImportRecipientsService  
&nbsp;&nbsp;&nbsp;&nbsp;- ImportRecipientsService\_V2  
&nbsp;&nbsp;&nbsp;&nbsp;- OnepointImportService\_High  
&nbsp;&nbsp;&nbsp;&nbsp;- OnepointImportService\_Higher  
&nbsp;&nbsp;&nbsp;&nbsp;- OnepointImportService\_Highest  
&nbsp;&nbsp;&nbsp;&nbsp;- OnepointImportService\_Low

---

<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:** [August 24, 2018, 2:15pm UTC](https://forum.ansible.com/t/need-a-register-value-to-persist/28414/6 "2018-08-24T14:15:25Z")

</div>

Thanks for the “with\_items tip. Please allow me to ask this again: the value of " import\_job\_count” must be 0 (zero) in order for the play to proceed. How do I do that?

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [August 24, 2018, 2:20pm UTC](https://forum.ansible.com/t/need-a-register-value-to-persist/28414/7 "2018-08-24T14:20:31Z")

</div>

You can choose one of these, they do the same but just written differently to just show you some options.

&nbsp;&nbsp;hostvars.Host1.import\_job\_count.stdout | int == 0  
&nbsp;&nbsp;hostvars['Host1']['import\_job\_count']['stdout'] | int == 0  
&nbsp;&nbsp;hostvars['Host1'].import\_job\_count.stdout == "0"

---

<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:** [August 24, 2018, 2:42pm UTC](https://forum.ansible.com/t/need-a-register-value-to-persist/28414/8 "2018-08-24T14:42:24Z")

</div>

Yup! Did this: hostvars[‘Host1’].import\_job\_count.stdout == “0” , and it worked a treat. I really appreciate your help!!!

---

<div class="post-metadata">

**Author:** ![Karl\_Auer](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Karl\_Auer](https://forum.ansible.com/u/Karl_Auer)\
**Post date:** [August 27, 2018, 11:39pm UTC](https://forum.ansible.com/t/need-a-register-value-to-persist/28414/9 "2018-08-27T23:39:55Z")

</div>

I have a similar problem but the solution doesn’t seem to apply.

I have multiple plays in a single playbook, but all the plays are on localhost. A fact set in one play does not seem to be available in other plays, because the hostname is “localhost” for all of them! Is there anyway to distinguish between such plays?

Here’s a very simple proof of concept 🙂

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [August 28, 2018, 7:21am UTC](https://forum.ansible.com/t/need-a-register-value-to-persist/28414/10 "2018-08-28T07:21:56Z")

</div>

It works, you probably just overlooked the variable.

$ ansible --version | head -n 1  
ansible 2.6.3

test.yml
