I am running into a silly issue when i try to delete some folders with win_file
first i copy some folders on the remote itself from one dir to another
- name: copy folders first
win_copy:
src: '{{ item }}'
dest: 'C:\folders\to\copy'
remote_src: yes
loop: '{{ paths_to_copy }}'
register: copied_folders
then i filter only the 'path' of those folders to be deleted later in the play after executng some other tasks.
- name: filter paths to be deleted after some tasks
set_fact:
paths_to_delete: "{{ copied_folders | json_query('results[*].dest') }}"
i get this results:
ok: [<computer>] => {
"ansible_facts": {
"paths_to_delete": [
"C:\\folders\\to\\copy\\1",
"C:\\folders\\to\\copy\\2",
"C:\\folders\\to\\copy\\3",
"C:\\folders\\to\\copy\\4"
]
},
"changed": false
}
all seems good but the playbook is failing when i loop over 'paths_to_delete' because it returns with all those 4 paths as ONE path.
- name: clean up temporary copied directories
win_file:
path: '{{ item }}'
state: absent
loop:
- '{{ paths_to_delete }}'
"msg": "Get-AnsibleParam: Parameter 'path' has an invalid path '['C:\\\\folders\\\\to\\\\copy\\\\1','C:\\\\folders\\\\to\\\\copy\\\\2','C:\\\\folders\\\\to\\\\copy\\\\3','C:\\\\folders\\\\to\\\\copy\\\\4'] specified."
why it is not looping over this list and deletes them one by one? i am using the same mechanism in the first copy task, looping over a list and it DOES copy the folder one by one without any issue.
Any help would be much appreciated.
CodePudding user response:
Your loop syntax is incorrect.
loop:
- '{{ paths_to_delete }}'
This nests the list inside another list with a single element. What you want to do is loop over the original list:
loop: '{{ paths_to_delete }}'