2

I'm very new to Ansible and I'm trying to check if files exists in my Ansible control machine(At least one should be), if so copy to remote location.

I've came up with the below, but ansible is checking the files in the remote instead of local. I'm not exactly sure -how to use "with_items" as well.

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

Error Message:

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

Rookie1999
  • 111
  • 1
  • 8
  • https://stackoverflow.com/a/32957656/840582 – Chen A. Sep 25 '19 at 08:54
  • Possible duplicate of [ansible - check if file exists on \*local\* machine](https://stackoverflow.com/questions/32957418/ansible-check-if-file-exists-on-local-machine) – Chen A. Sep 25 '19 at 08:54
  • @ChenA.: THanks for the suggestion, but I wanted to check if multiple files are present in my local before copying. Atlesat one file should be present. – Rookie1999 Sep 25 '19 at 09:14

1 Answers1

6

You can use the stat command to check whether a file exists or not. If exists, then copy it to the remote server :

---
- 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
Pacifist
  • 3,025
  • 2
  • 12
  • 20
  • Thanks a lot, just a small clarification though. debug: msg="File {{ item.item }} exist" - why item.item instead of only item ? – Rookie1999 Sep 25 '19 at 11:48
  • @Rookie1999 item.item is used to get the actual item that was used to loop on in the first task. In this case its the file names. See here: https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html#registering-variables-with-a-loop – watney Apr 23 '22 at 17:03