0

I have the following files in my tmp directory

root@ansible:/tmp/test$ ls /tmp/test/
file1  file2  file3

I also have the following playbook which is modeled after this

vars:
  exclude_files: file1
tasks:
 - name: check files
   shell: ls -l /tmp/test
   register: capture

- name: remove files
  file: path=/tmp/test/{{item}} state=absent
  with_items: capture.stdout_lines
  when: item not in exclude_files

- name: debug variable
  debug: msg={{exclude_files}}

Unfortunately the second task isn't removing file2,file3. Instead it's treating the registered variable as a file.

ok: [172.16.2.3] => (item=capture.stdout_lines) => {
"changed": false,
"invocation": {
    "module_args": {
        "attributes": null,
        "backup": null,
        "content": null,
        "delimiter": null,
        "diff_peek": null,
        "directory_mode": null,
        "follow": false,
        "force": false,
        "group": null,
        "mode": null,
        "original_basename": null,
        "owner": null,
        "path": "/tmp/test/capture.stdout_lines",
        "recurse": false,
        "regexp": null,
        "remote_src": null,
        "selevel": null,
        "serole": null,
        "setype": null,
        "seuser": null,
        "src": null,
        "state": "absent",
        "unsafe_writes": null,
        "validate": null
    }
},
"item": "capture.stdout_lines",
"path": "/tmp/test/capture.stdout_lines",
"state": "absent"

Any idea why this code isn't working? I am using ansible 2.3.

crusadecoder
  • 651
  • 1
  • 11
  • 26
  • This is not a duplicate, since it also hints at how to copy all files except some. It was very useful, thanks crusadecoder. – Ilja Zverev Feb 28 '19 at 07:33

1 Answers1

1

with_items: capture.stdout_lines should be with_items: "{{capture.stdout_lines}}"

Bare variables in with_... are deprecated long ago.

Konstantin Suvorov
  • 65,183
  • 9
  • 162
  • 193