Re: [ansible-project] Delete all files under a specific directory

706 views
Skip to first unread message
Message has been deleted

Michael Mullay

unread,
Nov 1, 2018, 12:47:10 AM11/1/18
to ansible...@googlegroups.com
Per the documentation (in bold red below), the file module is working as designed:

If directory, all intermediate subdirectories will be created if they do not exist. Since Ansible 1.7 they will be created with the supplied permissions. If file, the file will NOT be created if it does not exist; see the touchvalue or the copy or template module if you want that behavior. If link, the symbolic link will be created or changed. Use hard for hardlinks. If absent, directories will be recursively deleted, and files or symlinks will be unlinked. Note that absent will not cause file to fail if the path does not exist as the state did not change. If touch (new in 1.4), an empty file will be created if the path does not exist, while an existing file or directory will receive updated file access and modification times (similar to the way `touch` works from the command line).


So without testing it, it would look something like (run in 'test mode' (-C) first!):

- hosts: all
  tasks:
    - name: remove web dir contents
      file:
        path: "{{ item }}"
        state: absent
    with_fileglob:
      - "/tmp/xyz/*"



On Wed, Oct 31, 2018 at 8:50 PM <anushake...@gmail.com> wrote:
Hi,

I am trying to delete all files under a specific directory (xyz) but it is deleting the whole directory. Could someone please help.

---
- hosts: all
  tasks:
    - name: remove web dir contents
      file:
        path: '/tmp/xyz/'
        state: absent

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/7d82ba48-9c06-49e7-8c37-66c5fbe68b07%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Kai Stian Olstad

unread,
Nov 1, 2018, 3:21:23 AM11/1/18
to ansible...@googlegroups.com
On 01.11.2018 05:46, Michael Mullay wrote:
> You probably want to use
> https://docs.ansible.com/ansible/latest/plugins/lookup/fileglob.html?highlight=with_fileglob
>
>
> So without testing it, it would look something like (run in 'test mode'
> (-C) first!):
>
> - hosts: all
> tasks:
> - name: remove web dir contents
> file:
> path: "{{ item }}"
> state: absent
> with_fileglob:
> - "/tmp/xyz/*"

lookup plugins only work on Ansible controller aka localhost, not on
remote hosts.


> On Wed, Oct 31, 2018 at 8:50 PM <anushake...@gmail.com> wrote:
>
>> Hi,
>>
>> I am trying to delete all files under a specific directory (xyz) but
>> it is
>> deleting the whole directory. Could someone please help.
>>
>> ---
>> - hosts: all
>> tasks:
>> - name: remove web dir contents
>> file:
>> path: '/tmp/xyz/'
>> state: absent
>>

Just recreate the directory in the next task, it that is not an option
just use commad or shell module and run a command to do it.


--
Kai Stian Olstad

Michael Mullay

unread,
Nov 1, 2018, 6:41:14 PM11/1/18
to ansible...@googlegroups.com
My bad, thanks for pointing that out.


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages