In playbook interate over a hash to rm files

46 views
Skip to first unread message

Kenton Brede

unread,
Apr 23, 2014, 3:55:53 PM4/23/14
to ansible...@googlegroups.com
Just starting out with Ansible.  Currently I've got:

---
- hosts: myhost.example.com
  remote_user
: user1
  sudo
: yes
  tasks
:
   
- name: remove select files in root
      command
: /bin/rm /root/{{ item }}
      with_items
:
       
- testing
       
- testing2



This works fine but for each directory I have to create a new task.

What I'd like to do is something like:

tasks:
 
- name: remove files
    command
: /bin/rm {{ dir/files }}
    dirs_files
:
      dir
: '/root'
        files
:
         
- testing
         
- testing2
      dir
: '/home/user'
        files
:
         
- testfile
         
- testfile2

Obviously that's not going to work but hopefully it illustrates what I'd like to do.  I just don't want to have to write different tasks for different directories.

Any help appreciated.
Thanks,
Kent


Strahinja Kustudić

unread,
Apr 23, 2014, 5:09:47 PM4/23/14
to ansible...@googlegroups.com

Adam Morris

unread,
Apr 23, 2014, 5:51:03 PM4/23/14
to ansible...@googlegroups.com
This could get unwieldy quickly but is there some reason why you can't use...

- name: remove files
  file: path={{ item }} state=absent
  with_items:
    - '/root/testing'
    - '/root/testing2'
    - '/home/user/testfile'
    - '/home/user/testfile2'
 
This has the big advantage over the commands version that it is actually idempotent.  If it has to remove the files it will, but if they are already gone it will report that the task was 'OK'.  Your version might well report failed if the file no longer exists.

Adam
Reply all
Reply to author
Forward
0 new messages