# conditional error in playbook

**URL:** <https://forum.ansible.com/t/conditional-error-in-playbook/36983>\
**Category:** Ansible Project\
**Created:** [August 12, 2022, 2:29pm UTC](https://forum.ansible.com/t/conditional-error-in-playbook/36983 "2022-08-12T14:29:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Emanuel\_Gonzalez](https://avatars.discourse-cdn.com/v4/letter/e/b3f665/32.png) [@Emanuel\_Gonzalez](https://forum.ansible.com/u/Emanuel_Gonzalez)\
**Post date:** [August 12, 2022, 2:29pm UTC](https://forum.ansible.com/t/conditional-error-in-playbook/36983/1 "2022-08-12T14:29:11Z")

</div>

Hi, i create a playbook to remove a yum package:

---

<div class="post-metadata">

**Author:** ![Paul\_Manno](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/paul_manno/32/2804_2.png) [@Paul\_Manno](https://forum.ansible.com/u/Paul_Manno)\
**Post date:** [August 12, 2022, 2:35pm UTC](https://forum.ansible.com/t/conditional-error-in-playbook/36983/2 "2022-08-12T14:35:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Paul\_Manno](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/paul_manno/32/2804_2.png) [@Paul\_Manno](https://forum.ansible.com/u/Paul_Manno)\
**Post date:** [August 12, 2022, 2:37pm UTC](https://forum.ansible.com/t/conditional-error-in-playbook/36983/3 "2022-08-12T14:37:58Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![farrukh\_ahmed](https://avatars.discourse-cdn.com/v4/letter/f/a9adbd/32.png) [@farrukh\_ahmed](https://forum.ansible.com/u/farrukh_ahmed)\
**Post date:** [August 12, 2022, 6:02pm UTC](https://forum.ansible.com/t/conditional-error-in-playbook/36983/4 "2022-08-12T18:02:13Z")

</div>

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 }}”

---

<div class="post-metadata">

**Author:** ![farrukh\_ahmed](https://avatars.discourse-cdn.com/v4/letter/f/a9adbd/32.png) [@farrukh\_ahmed](https://forum.ansible.com/u/farrukh_ahmed)\
**Post date:** [August 12, 2022, 6:06pm UTC](https://forum.ansible.com/t/conditional-error-in-playbook/36983/5 "2022-08-12T18:06:19Z")

</div>

So, your overall playbook should be like.

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [August 12, 2022, 6:11pm UTC](https://forum.ansible.com/t/conditional-error-in-playbook/36983/6 "2022-08-12T18:11:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [August 12, 2022, 6:36pm UTC](https://forum.ansible.com/t/conditional-error-in-playbook/36983/7 "2022-08-12T18:36:13Z")

</div>

Your entire playbook could look like this:
