Ansible way to delete all files in a remote directory

29,710 views
Skip to first unread message

Asad Khan

unread,
Mar 13, 2013, 4:37:20 PM3/13/13
to ansible...@googlegroups.com
I know I can easily do:
shell: rm -f /etc/whatever/*

or possibly via a register variable on 
shell: ls -1 /etc/whatever/*
register: dircontents

I had assumed I would be able to do this in Ansible trivially without templating, but am not finding it straightforward
- with_glob enumerates "local" files only
- file: doesn't take wildcards as far as I know 

Is there something obvious I'm missing, or are the above the available implementation-patterns for this?

Michael DeHaan

unread,
Mar 13, 2013, 7:21:51 PM3/13/13
to ansible...@googlegroups.com
Just call shell.
> --
> 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.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>



--
Michael DeHaan <mic...@ansibleworks.com>
CTO, AnsibleWorks, Inc.
http://www.ansibleworks.com/

Luke Tislow

unread,
Mar 14, 2013, 11:31:59 AM3/14/13
to ansible...@googlegroups.com
Would the file module work with state=absent?

Asad Khan

unread,
Mar 15, 2013, 9:19:38 AM3/15/13
to ansible...@googlegroups.com
The file module targets a specific file (unless used with with_items)
It can target a directory (recursively with recurse=yes in 1.1), but then the directory gets removed

Mike Biglan

unread,
Feb 17, 2015, 1:35:37 AM2/17/15
to ansible...@googlegroups.com
Just tried this and it can't both use state=absent AND recurse=yes. Only way I could make this happen is drop down to "command".
Reply all
Reply to author
Forward
0 new messages