iteration using with_items and register

前端 未结 2 1910
独厮守ぢ
独厮守ぢ 2021-02-12 21:13

Looking for help with a problem I\'ve been struggling with for a few hours. I want to iterate over a list, run a command, register the output for each command and then iterate w

相关标签:
2条回答
  • 2021-02-12 21:38

    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"
    
    0 讨论(0)
  • 2021-02-12 21:49

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

    0 讨论(0)
提交回复
热议问题