# How does 'register' behave when paired with 'with\_items' ?

**URL:** <https://forum.ansible.com/t/how-does-register-behave-when-paired-with-with-items/12612>\
**Category:** Ansible Project\
**Tags:** ansible-project\
**Created:** [April 1, 2013, 3:53pm UTC](https://forum.ansible.com/t/how-does-register-behave-when-paired-with-with-items/12612 "2013-04-01T15:53:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [April 1, 2013, 3:53pm UTC](https://forum.ansible.com/t/how-does-register-behave-when-paired-with-with-items/12612/1 "2013-04-01T15:53:25Z")

</div>

> I'm trying to get into Ansible and was studying some examples. One of the  
> examples lists a task that contains both 'with\_items' and 'register'.

The registered variable will be a list of its own, which you would have to  
loop through, something like  
- ..  
&nbsp;&nbsp;with\_items: $last\_run

As far as I understood the docs, \*with\_item \*acts like a loop\*. \*Does \*

> register\* work on the last item only? Does it register an array of  
> results?

An array of results.​​

---

<div class="post-metadata">

**Author:** ![Andre\_Bierlein](https://avatars.discourse-cdn.com/v4/letter/a/5f9b8f/32.png) [@Andre\_Bierlein](https://forum.ansible.com/u/Andre_Bierlein)\
**Post date:** [April 3, 2013, 7:57am UTC](https://forum.ansible.com/t/how-does-register-behave-when-paired-with-with-items/12612/2 "2013-04-03T07:57:52Z")

</div>

Thanks for that answer!  
I do have another question though. In the example link I added to my first post, the second task looks like:

> ```auto
> 
> - name: Reload the haproxy
> service: name=haproxy state=reloaded
> delegate_to: $item
> with_items: ${groups.lbservers} 
> only_if: ${last_run.changed}
> 
> ```

That one would be incorrect then, I guess, because $last\_run.changed would not exist?

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [April 3, 2013, 8:06am UTC](https://forum.ansible.com/t/how-does-register-behave-when-paired-with-with-items/12612/3 "2013-04-03T08:06:47Z")

</div>

​That's correct. You would need to do something with "with\_items: $last\_run"  
I \*think\* there might be a way to address the earlier items (from  
${groups.lbservers} ) within $last\_run, but those nested lists keep me  
confused, as the outputted items from ansible-playbook are barely readeable.

&nbsp;&nbsp;Serge

---

<div class="post-metadata">

**Author:** ![benno\_joy](https://avatars.discourse-cdn.com/v4/letter/b/73ab20/32.png) [@benno\_joy](https://forum.ansible.com/u/benno_joy)\
**Post date:** [April 3, 2013, 12:48pm UTC](https://forum.ansible.com/t/how-does-register-behave-when-paired-with-with-items/12612/4 "2013-04-03T12:48:11Z")

</div>

Hey Andre,

The register variable will have $last\_run.changed element even if it is an array, I think the value is an OR of the 'changed. Array. So for a single node in lbservers the above code would be fine, but if more than one nodes are there in the group this would have to changed to something like this:

- name: Reload the haproxy  
service: name=haproxy state=reloaded  
delegate\_to: ${item.item}  
only\_if: ${item.changed}  
with\_items: ${last\_run.results}

I will test this out and update the repository, thanks for pointing this out.

Benno
