0
votes

I'm fairly new to ansible, and am currently trying to have a play that reads a log file, register directories in that file, and then deletes the directories

Right now I can get the correct output, and register the path, however the delete portion is throwing me

  • name: Get the names of directories that need to be deleted shell: grep "Errno 21" /path/error.log | awk '{ print $7 }' | tr -d \' register: Errno21

  • debug: msg: "{{Errno21.stdout}}"

  • name: Remove Directories file: path={{Errno21.stdout}} state=absent

TASK [remove files] ******************************* ok:

But the "directories" are not deleted.

How can I tell ansible that these are directories? I was thinking " file_type: directory" but that failed. Is it the module?

1

1 Answers

0
votes

Is this the code that you're lookg for?

- name: Remove Directories
  file:
    path: "{{ item }}"
    state: absent
  loop: "{{ Errno21.stdout_lines }}"

To answer your question:

How can I tell ansible that these are directories?

There is no reason to explicitly specify the items are directories. Ansible will find out on its own. If you want to delete the directories only state: absent is needed. The file module is clear about it

If absent, directories will be recursively deleted, ...