Pass/loop 2 个寄存器输出行(每个都有值列表)到 Ansible 中的下一个任务

Pass/loop 2 register output lines (has list of values each) to next task in Ansible

我正在尝试将 2 个寄存器列表值传递给任务。但是当我同时传递 list 时,它不会循环到寄存器值,而只是传递完整的列表数组。

数据(ls -ld)

drwxr-xr-x  2 root           root                 4096 Jun  4  2019 dir_1
drwxr-xr-x  2 root           root                 4096 Jun  4  2019 dir_2
drwxr-xr-x  2 ram            ram                  4096 Jun  4  2019 dir_3
drwxr-xr-x  2 mike           mike                 4096 Jun  4  2019 dir_4

代码

- name: Get Owners
  become: false
  shell: ls -ld  | awk '{print }'
  args:
    chdir: /data
  register: owner

- name: Get foldernames
  become: false
  shell: ls -ld | awk '{print  }'
  args:
    chdir: /data
  register: foldername

- name: check if its able to loop.
  debug:
    msg: "{{item.a}}:{{item.b}}"
  loop:
    - { a: "{{ owner.stdout_lines }}", b: "{{ foldername.stdout_lines }}" }

输出

Instead of having the output looped its copying the whole (stdout_lines) list in array 

"msg": "[u'root', u'root', u'ram', u'mike']:[u'dir_1', u'dir_2', u'dir_3', u'dir_4']

如何让数据(所有者和文件夹名称)循环并按顺序一个接一个地传递给 msg(以及 folername 的正确所有者)。

Q: "How can I get the data (owner and foldername) looped and passed to the msg one by one in a sequence (and right owner to folername)."

答:还有更多选择。

1) 使用zip。例如

- name: debug:
    msg: "{{ item.0 }} - {{ item.1 }}"
  loop: "{{ owner.stdout_lines|zip(foldername.stdout_lines)|list }}"

2) find the directories and json_query 可以在 loop 中注册 result。例如

    - find:
        path: data
        file_type: directory
      register: result

    - debug:
        msg: "{{ item.0 }} - {{ item.1 }}"
      loop: "{{ result.files|json_query('[].[pw_name, path]') }}"