Search code examples
ansible

Deleting multiple files in most efficient way (ansible)


I want to delete couple of files, right now I do it this way:

- file: path=/etc/yum.repos.d/rhel6-6-hci-frozen.repo state=absent
  name: Ensure absence of old freeze files
- file: path=/etc/yum.repos.d/in-mrepo-rhel6.repo state=absent
  name: Ensure absence of old files

...

many other lines

The problem with this is that ansible seems to execute these one by one, instead of merging it into one task:

TASK [Ensure absence of old freeze files] **************************************
changed: [server]

TASK [Ensure absence of old files] *********************************************
ok: [server]

TASK [Ensure absence of actual files] ******************************************
ok: [server]

TASK [Ensure absence of old rhel6 freeze files] ********************************
ok: [server]

TASK [Ensure absence of epel stuff] ********************************************
ok: [server]

TASK [Ensure absence of epel testing] ******************************************
ok: [server]

Which takes a lot of time. Is there a way to execute this as one task? I know I could probably just execute a shell script, but I was hoping for ansible way to do this properly.


Solution

  • You can use the with_items key like so:

    - name: Ensure absence of old freeze files
      file:
        path: '{{ item }}'
        state: absent
      with_items:
        - /etc/yum.repos.d/rhel6-6-hci-frozen.repo
        - /etc/yum.repos.d/in-mrepo-rhel6.repo