Hi, i create a playbook to remove a yum package:
You need to move this out of the block
- name: “Verificamos si el paquete esta instalado”
shell:
cmd: |
rpm -q “{{ item }}”
with_items: “{{ package_names }}”
register: package_check
Additionally, you’re looping over package_names. I think you may need to instead pull this out to another task_list and include_tasks while looping instead.
Firstly, you have defined the play that is verifying the packages, which needs to be outside the block:
- name: “Verificamos si el paquete esta instalado”
shell:
cmd: |
rpm -q “{{ item }}”
with_items: “{{ package_names }}”
register: package_check
So, it should execute before the block, save the output to the register variable that is package_check. And then check the condition for the block to execute when: package_check is succeeded
Second, Inside the block on this play, you need to define the with_items on this play, to tell it which packages to remove.
- name: “Borramos el paquete”
yum:
name: “{{ item }}”
state: absent
with_items: “{{ package_names }}”
So, your overall playbook should be like.
If you want to ensure a list of packages is absent, make a task that does only that.
- name: “Borramos el paquete”
package:
name: “{{ package_names }}”
state: absent
Each Ansible task describes a desired state when the task is completed. You don’t need to check whether the packages are installed. You only need to say you want them absent. If any (or all) of the packages are present, they will be removed, the task will report “changed”, and the state will be changed. If none of the packages are present, the task will report “ok” and the state will be unchanged.
Your entire playbook could look like this: