firasKoubaa
firasKoubaa

Reputation: 6877

Ansible: find files and copy them

I'm using Ansible 2.3 to find files with specific patterns and copy them according to their paths.

So my search task looks like this :

- name: find onDemand scripts of APIs
  find:
    paths: "{{WORKSPACE}}/dollaru-scripts/Docker/scripts_on_demand/"
    patterns:
    - 'run_api*.ksh'
    - '*_all_containers.ksh'
    - '*_docker_engine.ksh'
  register: scripts_on_demand_api
  when:
    - ansible_host not in groups['remoteHosts']

Then, to display the result I used a debug task and I obtain such result, after running under Jenkins:

ok: [localhost] => {
17:51:36     "changed": false, 
17:51:36     "msg": {
17:51:36         "changed": false, 
17:51:36         "examined": 13, 
17:51:36         "files": [
17:51:36             {
17:51:36                 "atime": 1518791011.147691, 
17:51:36                 "ctime": 1518791009.4396753, 
17:51:36                 "dev": 64782, 
17:51:36                 "gid": 15001, 
17:51:36                 "inode": 301837, 
17:51:36                 "isblk": false, 
17:51:36                 "ischr": false, 
17:51:36                 "isdir": false, 
17:51:36                 "isfifo": false, 
17:51:36                 "isgid": false, 
17:51:36                 "islnk": false, 
17:51:36                 "isreg": true, 
17:51:36                 "issock": false, 
17:51:36                 "isuid": false, 
17:51:36                 "mode": "0644", 
17:51:36                 "mtime": 1518791009.4396753, 
17:51:36                 "nlink": 1, 
17:51:36                 "path": "/opt/jenkins/workspace/ANSIBLE_DEPLOY_HP-ALL/dollaru-scripts/Docker/scripts_on_demand/run_api_backend.ksh", 
17:51:36                 "rgrp": true, 
17:51:36                 "roth": true, 
17:51:36                 "rusr": true, 
17:51:36                 "size": 470, 
17:51:36                 "uid": 30000, 
17:51:36                 "wgrp": false, 
17:51:36                 "woth": false, 
17:51:36                 "wusr": true, 
17:51:36                 "xgrp": false, 
17:51:36                 "xoth": false, 
17:51:36                 "xusr": false
17:51:36             }, 
                     {
17:51:36                 "atime": 1518791011.147691, 
17:51:36                 "ctime": 1518791009.4396753, 
17:51:36                 "dev": 64782, 
17:51:36                 "gid": 15001, 
17:51:36                 "inode": 301853, 
17:51:36                 "isblk": false, 
17:51:36                 "ischr": false, 
17:51:36                 "isdir": false, 
17:51:36                 "isfifo": false, 
17:51:36                 "isgid": false, 
17:51:36                 "islnk": false, 
17:51:36                 "isreg": true, 
17:51:36                 "issock": false, 
17:51:36                 "isuid": false, 
17:51:36                 "mode": "0644", 
17:51:36                 "mtime": 1518791009.4396753, 
17:51:36                 "nlink": 1, 
17:51:36                 "path": "/opt/jenkins/workspace/ANSIBLE_DEPLOY_HP-ALL/dollaru-scripts/Docker/scripts_on_demand/run_api_tracking.ksh", 
17:51:36                 "rgrp": true, 
17:51:36                 "roth": true, 
17:51:36                 "rusr": true, 
17:51:36                 "size": 476, 
17:51:36                 "uid": 30000, 
17:51:36                 "wgrp": false, 
17:51:36                 "woth": false, 
17:51:36                 "wusr": true, 
17:51:36                 "xgrp": false, 
17:51:36                 "xoth": false, 
17:51:36                 "xusr": false
17:51:36             }
17:51:36         ], 
17:51:36         "matched": 9, 
17:51:36         "msg": ""
17:51:36     }
17:51:36 }

Now i want to use paths of my founded files , in a copy task ; i did this way :

- name: Copy foundedfiles
  copy:
   src: "{{item.path}}"
   dest: "/opt/application/i99/sh/onDemand/"
   mode: 0755
  with_items:
   - "{{scripts_on_demand_api.files}}"
  when:
    - ansible_host in groups['api']

Strangely , an error saying that there is no konwnb "files" attribute !!!

17:51:37 fatal:FAILED! => {"failed": true, "msg": "'dict object' has no attribute 'files'"}

Suggestions ??

Upvotes: 1

Views: 1765

Answers (1)

techraf
techraf

Reputation: 68639

Hosts in your when conditions don't match, so you run the second task (copy) on a host that did not run the first task (find).

scripts_on_demand_api is registered on all hosts, files is registered only on the host not in groups['remoteHosts'].


To iterate over local files there is an easier method with a with_fileglob loop.

Upvotes: 1

Related Questions