简体   繁体   中英

Ansible: Loop through multiple variables in 'lineinfile'

I am not able to loop through multiple register variables stdout in lineinfile module in Ansible.

      - lineinfile:
          state: present
          insertafter: EOF
          path: /tmp/file.txt
          line: "{{ item }}"
        with_items:
          - "{{ result1.stdout }}"
          - "{{ result2.stdout }}"

And I am getting FAILED: => {"msg": "'dict object' has no attribute 'stdout'"} error message.

But when I debug the variable, "{{result1.stdout}}" stdout is getting printed in the output.

Result1 Output:

"msg": "192.162.212.XXX\nNo Tasks Performed"

Regarding the error message 'dict object' has no attribute 'stdout' I've setup a small test

---
- hosts: localhost
  become: no
  gather_facts: no

  vars:
    result1:
      stdout: "STDOUT 1"
    result2:
      stdout: "STDOUT 2"

  tasks:

  - name: Loop through multiple variables in 'lineinfile'
    lineinfile:
      path: /tmp/test.txt
      line: "{{ item }}"
      insertafter: EOF
      state: present
      create: yes
    with_items:
      - "{{ result1.stdout }}"
      - "{{ result2.stdout }}"

and found it working.

Therefore you should test the content of the variables or debug it as already mentioned in comments,

Did you test the content of result2 ? ... that one does not have a stdout field...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM