I am trying to find files with .ign extension from a directory and copy it to another directory. Tried using the 'find' and 'copy' module as follows:
- 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 is set as /root/openstack-upi. And I am running this as a non-root(cloud-user) user with the command--
ansible-playbook -i inventory -e @install_vars.yaml playbooks/install.yaml --become
However, after running this I get an error as below:
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
Running a debug on variable find_result gives the following-
"msg": "/root/openstack-upi/*.ign was skipped as it does not seem to be a valid directory or it cannot be accessed\n"
Am I missing anything here? Can anybody tell me the exact command for the ansible-playbook for the above scenario?
CodePudding user response:
The following solution will does not use the with_items option. It will recursively find all the files which ends with the 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 }}"
CodePudding user response:
An explanation regarding your given find file loop
with_items:
- "{{ workdir }}/*.ign"
register: find_result
and the given error message
msg": "/root/openstack-upi/*.ign was skipped as it does not seem to be a valid directory or it cannot be accessed
It will be necessary to lookup the files matching a pattern before. This can be done with the with_fileglob lookup plugin.
with_fileglob:
- "{{ workdir }}/*.ign"
register: find_result
or better and as explained in the accepted answer here in this thread.
The error message just sayed the path and filename were skipped as .../*.ign wasn't a valid path and filename. It means it hasn't looked up any files by resolving the fileglob.
