Register Variables in Loop in an Ansible Playbook

Is this what you’re looking for:

Variables registered for a task that has with_items have different format, they contain results for all items.

- hosts: localhost
  tags: s21
  gather_facts: no
  vars:
    images:
      - foo
      - bar
  tasks:
    - shell: "echo result-{{item}}"
      register: "r"
      with_items: "{{images}}"

    - debug: var=r

    - debug: msg="item.item={{item.item}}, item.stdout={{item.stdout}}, item.changed={{item.changed}}"
      with_items: "{{r.results}}"

    - debug: msg="Gets printed only if this item changed - {{item}}"
      when: "{{item.changed == true}}"
      with_items: "{{r.results}}"

Source: Register variables in with_items loop in Ansible playbook

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)