How to store command output into array in Ansible?

久未见 提交于 2019-11-30 14:21:10

Ansible stores the output of shell and command action modules in stdout and stdout_lines variables. The latter contains separate lines of the standard output in a form of a list.

To iterate over the elements, use:

with_items:
  - "{{ annoying.stdout_lines }}"

You should remember that parsing ls output might cause problems in some cases.

Can you try as below.

- name: Run command to cat each file and then capture that output.
   shell: cat {{ item.stdout_lines }}
   register: annoying_words
   with_items:
    - "{{ annoying.results }}"

annoying.stdout_lines is already a list.

From doc of stdout_lines

When stdout is returned, Ansible always provides a list of strings, each containing one item per line from the original output.

To assign the list to another variable do:

    ..
      register: annoying
    - set_fact:
        varName: "{{annoying.stdout_lines}}"
    # print first element on the list
    - debug: msg="{{varName | first}}"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!