I’d like to perform the following:
- Ensure 3 results file do not exist on targets
- Run a script that produces them on targets
- Copy the 3 resulting files back.
- Delete the 3 resulting file from targets.
Seems like I can’t do something like (for the removal process):
name: remove 3 results file from target:
file: dest={{ res_file1 }} state=absent
file: dest={{ res_file2 }} state=absent
file: dest={{ res_file3 }} state=absent
But I need to perform:
name: remove results file 1 from target
file: dest={{ res_file1 }} state=absent
name: remove results file 2 from target
file: dest={{ res_file2 }} state=absent
name: remove results file 3 from target
file: dest={{ res_file3 }} state=absent
Am I expected to use ‘with’ ? Any better way (this is quite tedious) ?
ansible-1.2.3-2.el6.noarch on CentOS 6.4.
TIA,
Y.
Yes.
Please read the documentation about with_items, as follows:
- file: dest={{ item }} state=absent
with_items:
- res_file1
- res_file2
- res_file3
quick update, you will want to template them if you have variables defined.
with_items:
- “{{ res_file1 }}”
- “{{ res_file2 }}”
of course, but can just also do this:
with_items: list_of_files_to_delete
where this allows you define that list in inventory, vars_files, roles, or other places.
I recommend being on 1.3.X though to make sure you have the latest updates.
‘with_items’ is a keyword? I was puzzled a bit about the ‘item’ vs. ‘items’ in the documentation.
If there’s an RPM of 1.3, I’d be happy to take it. Otherwise, it’ll take bit more work to bring it to 1.3, but I’ll try.
Thanks!
Y.
Hi Yaniv,
“with_items” is covered in the looping sections of the playbook docs – I’d link to them, but it’s currently under a refactor so this link will change tomorrow
“item” is the value being looped over in each iteration of the with items part.