Dorilds
Dorilds

Reputation: 448

Ansible tasks installing undesired packages

In my ansible playbook I have a few roles running tasks that install lots of different packages. Some of these packages seem to be installing:

modemmanager, wpasuppclient, snapd, and apache

I'm quite new to using the when: condition in ansible. Is there a way that I could add a when condition in my ansible tasks that prevent them from installing these packages when running my playbook?

pseudocode:

- name: installing packages when: if modemmanager, wpasuppclient, snapd, and apache are getting installed stop them from being installed? apt: pkg={{item}} state=latest with items: list of items

Another thought I had is that is there a way that I could use the state variable to do this?

Upvotes: 0

Views: 244

Answers (1)

ilias-sp
ilias-sp

Reputation: 6705

make a variable that is a list of the packages to be excluded, add it in the vars section of the playbook:

vars:
  package_exclusion_list: [modemmanager, wpasuppclient, snapd, apache]

update your task adding a when condition:

- name: installing packages
  apt: pkg={{item}} state=latest
  when: item not in package_exclusion_list
  with items:
       list of items

hope it helps

Side quest:

if you want to run a batch of "simple" tasks (as you explained in the comments), you can use the include_tasks.

example:

  - name: include tasks
    include_tasks: various_tasks.yml
    with_items: 
      - "{{ packages_to_install }}"

but pay attention, the various_tasks.yml should refer to each of the packages_to_install items with item.

various_tasks.yml example:

---
- name: print items name
  debug:
    var: item

- name: print items name #2
  debug:
    msg: "variable value: {{ item }}"

obviously, if you try to add in this various_tasks file some loop that will have to use its own items, then it will have a conflict.

Upvotes: 0

Related Questions