Petr
Petr

Reputation: 14455

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.

Upvotes: 7

Views: 21799

Answers (3)

Guillaume
Guillaume

Reputation: 94

From what I've see the best way the the following:

    - name: List files matching '*rhel6*'
      find:
        paths: "/etc/yum.repos.d"
        patterns: '*rhel6*'
      register: to_remove
    - name: Remove EPEL repo files
      file:
        path: "{{ item.path }}"
        state: absent
      with_items: "{{ to_remove.files }}"
      when: to_remove.matched > 0
  • First list the file with find
  • Second remove them with file & absent

Upvotes: 0

nyumerics
nyumerics

Reputation: 6547

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

Upvotes: 20

Luc Demeester
Luc Demeester

Reputation: 345

I tried this and it works with ansible 1.9.2:

- name: Ensure absence of old freeze files
  file:
    path: /etc/yum.repos.d/*rhel6*.repo
    state: absent

Of course be sure you won't delete too many repo files.

Upvotes: -2

Related Questions