Ansible: How to delete files and folders inside a directory?

Abbas picture Abbas · Jul 5, 2016 · Viewed 322.3k times · Source

The below code only deletes the first file it gets inside the web dir. I want to remove all the files and folders inside the web directory and retain the web directory. How can I do that?

  - name: remove web dir contents
     file: path='/home/mydata/web/{{ item }}' state=absent
     with_fileglob:
       - /home/mydata/web/*

Note: I've tried rm -rf using command and shell, but they don't work. Perhaps I am using them wrongly.

Any help in the right direction will be appreciated.

I am using ansible 2.1.0.0

Answer

Mohan Kumar P picture Mohan Kumar P · Jul 5, 2016

Below code will delete the entire contents of artifact_path

- name: Clean artifact path
  file:
    state: absent
    path: "{{ artifact_path }}/"

Note: this will delete the directory too.