How does 'register' behave when paired with 'with_items' ?

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

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?

​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

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