Ansible - проверьте, существует ли несколько файлов локально, и скопируйте на удаленный - PullRequest
0 голосов
/ 25 сентября 2019

Я очень плохо знаком с Ansible и пытаюсь проверить, существуют ли файлы на моей машине управления Ansible (по крайней мере, так должно быть), если это так, скопируйте в удаленное местоположение.

Я пришелс нижеследующим, но ansible проверяет файлы в удаленном, а не локальном.Я не совсем уверен - как использовать "with_items".

---
- 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

Сообщение об ошибке:

TASK [Validate if file exists] ****************************************************************************************************************************************** failed: [remoteserver_1 -> localhost] (item=files) => {"changed": false, "item": "files", "msg": "file (/login/my_home/files) is absent, cannot continue", "path": "/login/my_home/files", "state": "absent"} failed: [remoteserver_2 -> localhost] (item=files) => {"changed": false, "item": "files", "msg": "file (/login/my_home/files) is absent, cannot continue", "path": "/login/my_home/files", "state": "absent"}

1 Ответ

1 голос
/ 25 сентября 2019

Вы можете использовать команду stat, чтобы проверить, существует файл или нет.Если существует, copy это на удаленный сервер:

---
- 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
...