Ansible-检查本地是否存在多个文件并复制到远程

Ansible-检查本地是否存在多个文件并复制到远程,ansible,ansible-2.x,Ansible,Ansible 2.x,我对Ansible非常陌生,我正在尝试检查我的Ansible控制机器中是否存在至少一个应该存在的文件,如果存在,请复制到远程位置 我提出了以下建议,但ansible正在远程而不是本地检查文件。我不太确定-如何使用_项目以及 --- - hosts: linux gather_facts: no vars: source_dir: /login/my_home server_type: WRITEVIEW files: - app.xslt - C

我对Ansible非常陌生,我正在尝试检查我的Ansible控制机器中是否存在至少一个应该存在的文件,如果存在,请复制到远程位置

我提出了以下建议,但ansible正在远程而不是本地检查文件。我不太确定-如何使用_项目以及

---
- hosts: linux
  gather_facts: no
  vars:
   source_dir: /login/my_home
   server_type: WRITEVIEW
   files:
      - app.xslt
      - Configuration.xml
      - fcCN.xslt

  tasks:
   - name: Validate if file exists
     local_action: file path="{{ source_dir }}/{{ item }}" state=file
     with_items: files
错误消息:

任务[验证文件是否存在]****************************************************************************************************************************************** 失败:[remoteserver_1->localhost]item=files=>{更改:false,item:files,msg:file/login/my_home/files不存在,无法继续,路径:/login/my_home/files,状态:不存在} 失败:[remoteserver_2->localhost]item=files=>{changed:false,item:files,msg:file/login/my_home/files不存在,无法继续,路径:/login/my_home/files,状态:不存在}

您可以使用stat命令检查文件是否存在。如果存在,则将其复制到远程服务器:

---
- hosts: linux
  gather_facts: no
  vars:
   source_dir: /login/my_home
   server_type: WRITEVIEW
   files:
      - app.xslt
      - Configuration.xml
      - fcCN.xslt

   tasks:
    - name: check if file exists
      local_action: stat path=/path/of/file/{{ item }}
      with_items: "{{ files }}"
      register: check_file_name

    - name: Verifying if file exists
      debug: msg="File {{ item.item }} exist"
      with_items: "{{ check_file_name.results }}"
      when: item.stat.exists

    - name: Copying the file
      copy: src=/path/of/file/local/{{ item.item }} dest=/path/of/file/remote/
      with_items: "{{ check_file_name.results }}"
      when: item.stat.exists

@ChenA可能重复:谢谢你的建议,但我想在复制之前检查我的本地文件中是否存在多个文件。Atlesat一个文件应该存在。非常感谢,只是一个小的澄清。调试:msg=文件{{item.item}}存在-为什么item.item不是唯一的item?