How to store command output into array in Ansible?

前端 未结 3 595
长情又很酷
长情又很酷 2020-12-31 21:51

Essentially, I want to be able to handle \"wildcard filenames\" in Linux using ansible. In essence, this means using the ls command with part of a filename followed by an \

相关标签:
3条回答
  • 2020-12-31 22:29

    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.

    0 讨论(0)
  • 2020-12-31 22:41

    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 }}"
    
    0 讨论(0)
  • 2020-12-31 22:55

    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}}"
    
    0 讨论(0)
提交回复
热议问题