ansible 中的文件复制失败! =>{"msg": "'dict object' 没有属性 'files'"}

File copy in ansible failed! =>{"msg": "'dict object' has no attribute 'files'"}

我正在尝试从目录中查找扩展名为 .ign 的文件并将其复制到另一个目录。尝试使用 'find' 和 'copy' 模块如下:

- name: Find files
  find: 
    paths: "{{ item }}"
    recurse: yes
  register: find_result
  with_items:
    - "{{ workdir }}/*.ign"

- name: Copy files
  copy:
    src: "{{ item.path }}"
    dest: "/var/www/html/ignition/"
    mode: o+r
    remote_src: yes
  with_items: "{{ find_result.files }}"

workdir 设置为 /root/openstack-upi。我是 运行 作为非 root(云用户)用户使用命令--

ansible-playbook  -i inventory -e @install_vars.yaml playbooks/install.yaml --become

但是,在 运行 之后,我得到如下错误:

TASK [ocp-config : Find files] ***************************************************
ok: [ash-test-faf0-bastion-0] => (item=/root/openstack-upi/*.ign)

TASK [ocp-config : Copy files] ***********************************************
fatal: [ash-test-faf0-bastion-0]: FAILED! => {"msg": "'dict object' has no attribute 'files'"}

PLAY RECAP **********************************************************************************
ash-test-faf0-bastion-0    : ok=3    changed=0    unreachable=0    failed=1    skipped=1    rescued=0    ignored=0

运行 对变量 find_result 的调试给出了以下 -

"msg": "/root/openstack-upi/*.ign was skipped as it does not seem to be a valid directory or it cannot be accessed\n"

我在这里遗漏了什么吗?谁能告诉我上述场景的 ansible-playbook 的确切命令?

以下解决方案将不使用 with_items 选项。它将递归查找所有以 extension/suffix .ign.

结尾的文件
      - name: Find files                                                         
        find:                                                                    
          paths: "/path/to/directory"
          patterns: "*.ign"                                             
          recurse: yes                                                           
        register: result                                                         
                                                                                 
      - name: Print find result 
        debug:                                                                   
          msg: "{{ item.path }}"                                                 
        with_items:                                                              
          - "{{ result.files }}"  

关于您给定的查找文件循环的解释

  with_items:
    - "{{ workdir }}/*.ign"
  register: find_result

和给定的错误信息

msg": "/root/openstack-upi/*.ign was skipped as it does not seem to be a valid directory or it cannot be accessed

之前需要查找匹配模式的文件。这可以通过 with_fileglob 查找插件来完成。

  with_fileglob:
    - "{{ workdir }}/*.ign"
  register: find_result

或更好,如 中所述。

错误消息只是说路径和文件名被跳过,因为 .../*.ign 不是有效的路径和文件名。这意味着它没有查找任何文件,也没有解析 fileglob。