How to store command output into array in Ansible? How to store command output into array in Ansible? arrays arrays

How to store command output into array in Ansible?


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