Reputation: 14455
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
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
Upvotes: 0
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
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