Re: [ansible-project] How does 'register' behave when paired with 'with_items' ?

2,469 views
Skip to first unread message

Serge van Ginderachter

unread,
Apr 1, 2013, 11:53:25 AM4/1/13
to ansible...@googlegroups.com

On 1 April 2013 14:39, André Bierlein <andre.mt...@gmail.com> wrote:
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
- ..
  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.​​


André Bierlein

unread,
Apr 3, 2013, 3:57:52 AM4/3/13
to ansible...@googlegroups.com
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:

- 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?

Serge van Ginderachter

unread,
Apr 3, 2013, 4:06:47 AM4/3/13
to ansible...@googlegroups.com

On 3 April 2013 09:57, André Bierlein <andre.mt...@gmail.com> wrote:
That one would be incorrect then, I guess, because $last_run.changed would not exist?

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


  Serge
 

benno joy

unread,
Apr 3, 2013, 8:48:11 AM4/3/13
to ansible...@googlegroups.com
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





--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages