iteration using with_items and register

折月煮酒 提交于 2019-12-03 10:49:40
Rowley

OK so I found a post on stackoverflow that helped me better understand what is going on here and how to access the elements in result.results.

The resultant code I ended up with was:

---

- hosts: localhost
  gather_facts: false

  vars:
    numbers:
      - name: "first"
        int: "1"
      - name: "second"
        int: "2"

  tasks:

    - name: Register output
      command: "/bin/echo {{ item.int }}"
      register: echo_out
      with_items: "{{ numbers }}"

    - debug: msg="item.item={{item.item.name}}, item.stdout={{item.stdout}}"
      with_items: "{{ echo_out.results }}"

Which gave me the desired result:

"msg": "item.item=first, item.stdout=1"
"msg": "item.item=second, item.stdout=2"

I am not sure if I understand the question correctly, but maybe this can help:

    - debug: msg="{{ item.stdout }}"
      with_items: echo_out.results

Please note that Ansible will print each item and the msg both - so you need to look carefully for a line that looks like "msg": "2".

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!