如何获取 Dir 下的远程文件列表并迭代以通过 ansible 列出内容

How to get the list of remote files under a Dir and iterate over to list out the contents via ansible

有谁知道如何获取特定目录下的远程文件列表并遍历它们并通过 ansible 列出每个文件的内容? 例如,我有一个位置 /var/spool/cron,它有很多文件,我需要通过遍历每个文件来 cat <file>

filegloblookup 在本地工作。

下面是剧本,但没有按预期工作。

---
- name: Playbook to quick check the cron jobs for user
  hosts: all
  remote_user: root
  gather_facts: False
  tasks:
  - name: cron state
    shell: |
      ls /var/spool/cron/
    register: cron_status
  - debug: var=item
    with_items:
        - "{{ cron_status.stdout_lines }}"

以此为例

 ---
  - name: Playbook
    hosts: all
    become: root
    gather_facts: False
    tasks:
    - name: run ls-
      shell: |
        ls -d1 /etc/cron.d/*
      register: cron_status

    - name: cat files
      shell: cat {{ item }}
      register: files_cat
      with_items:
        - "{{ cron_status.stdout_lines }}" 

    - debug: var=item
      with_items:
        - "{{ files_cat.results| map(attribute='stdout_lines')|list }}" 

为了你的兴趣.. 下面的 Play 会给你更干净的 stdout

---
- name: hostname
  hosts: all
  become: root
  gather_facts: False
  tasks:
  - name: checking cron entries
    shell: more /var/spool/cron/*
    register: cron_status
  - debug: var=cron_status.stdout_lines