# Action failed\_when not working with condition

**URL:** <https://forum.ansible.com/t/action-failed-when-not-working-with-condition/36311>\
**Category:** Ansible Project\
**Created:** [February 9, 2022, 4:51pm UTC](https://forum.ansible.com/t/action-failed-when-not-working-with-condition/36311 "2022-02-09T16:51:47Z")\
**Posts on this page:** 6\
**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:** [February 9, 2022, 4:51pm UTC](https://forum.ansible.com/t/action-failed-when-not-working-with-condition/36311/1 "2022-02-09T16:51:47Z")

</div>

Hello, I am creating a playbook, in the first task I am checking if a database exists which is previously created with another task.

I need to finish the playbook if the database does not exist, for this to assemble the next playbook

- name: Check if database exists  
shell: |  
mysql -hhost -uuser -ppassword -e “show databases” | egrep db  
register: mysql\_exist  
failed\_when: “‘FAILED’ in mysql\_exist.stderr”

- name:  
debug:  
msg: “{{ mysql\_exist.stdout }}”

but the playbook keeps going and I need it to end the playbook if the database doesn’t exist.

the next task called “Check Queue in RabbitMQ” should not be executed because the database does not exist.

changed: [server01-stage]  
TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [server01-stage] =\> {  
“msg”: “”  
}  
TASK [Check Queue in RabbitMQ] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [schedule01-stage]: FAILED! =\> {“changed”: true, “cmd”: “rabbitmqadmin --host=host --port=port2 --username=user --password=usert -f long list queues | egrep "AWPP"\n”, “delta”: “0:00:00.389108”, “end”: “2022-02-09 16:42:13.394477”, “msg”: “non-zero return code”, “rc”: 1, “start”: “2022-02-09 16:42:13.005369”, “stderr”: “”, “stderr\_lines”: , “stdout”: “”, “stdout\_lines”: }  
PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
schedule01-stage : ok=2 changed=1 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0

any helps??

Regards,

---

<div class="post-metadata">

**Author:** ![Rilindo\_Foster1](https://avatars.discourse-cdn.com/v4/letter/r/8e8cbc/32.png) [@Rilindo\_Foster1](https://forum.ansible.com/u/Rilindo_Foster1)\
**Post date:** [February 9, 2022, 5:01pm UTC](https://forum.ansible.com/t/action-failed-when-not-working-with-condition/36311/2 "2022-02-09T17:01:40Z")

</div>

Not an answer, but a thought - instead of using the shell module, have you thought about using the mysql\_info module?

[https://docs.ansible.com/ansible/latest/collections/community/mysql/mysql\_info\_module.html#ansible-collections-community-mysql-mysql-info-module](https://docs.ansible.com/ansible/latest/collections/community/mysql/mysql_info_module.html#ansible-collections-community-mysql-mysql-info-module)

Or any other of the mySQL modules:

[https://docs.ansible.com/ansible/latest/collections/community/mysql/index.html](https://docs.ansible.com/ansible/latest/collections/community/mysql/index.html)

---

<div class="post-metadata">

**Author:** ![Rilindo\_Foster1](https://avatars.discourse-cdn.com/v4/letter/r/8e8cbc/32.png) [@Rilindo\_Foster1](https://forum.ansible.com/u/Rilindo_Foster1)\
**Post date:** [February 9, 2022, 5:01pm UTC](https://forum.ansible.com/t/action-failed-when-not-working-with-condition/36311/3 "2022-02-09T17:01:41Z")

</div>

Not an answer, but a thought - instead of using the shell module, have you thought about using the mysql\_info module?

[https://docs.ansible.com/ansible/latest/collections/community/mysql/mysql\_info\_module.html#ansible-collections-community-mysql-mysql-info-module](https://docs.ansible.com/ansible/latest/collections/community/mysql/mysql_info_module.html#ansible-collections-community-mysql-mysql-info-module)

Or any other of the mySQL modules:

[https://docs.ansible.com/ansible/latest/collections/community/mysql/index.html](https://docs.ansible.com/ansible/latest/collections/community/mysql/index.html)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [February 9, 2022, 5:41pm UTC](https://forum.ansible.com/t/action-failed-when-not-working-with-condition/36311/4 "2022-02-09T17:41:39Z")

</div>

You don't show the output/result of the mysql task so it is very hard  
to figure out what happend with it, but the debug shows stdout is  
empty, but not stderr which is what you have in the failed\_when

---

<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:** [February 9, 2022, 7:06pm UTC](https://forum.ansible.com/t/action-failed-when-not-working-with-condition/36311/5 "2022-02-09T19:06:31Z")

</div>

Hi, here debug:

stdout is empty, why??

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [schedule01-stage] =\> {  
“mysql\_exist”: {  
“ansible\_facts”: {  
“discovered\_interpreter\_python”: “/usr/bin/python”  
},  
“changed”: true,  
“cmd”: “mysql -hhost uuser -ppass -e "show databases" | egrep db\_app\n”,  
“delta”: “0:00:00.019964”,  
“end”: “2022-02-09 18:45:39.611461”,  
“failed”: false,  
“rc”: 0,  
“start”: “2022-02-09 18:45:39.591497”,  
“stderr”: “”,  
“stderr\_lines”: ,  
“stdout”: “0”,  
“stdout\_lines”: [  
“0”  
],  
“warnings”: [  
“Platform linux on host schedule01-stage is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python interpreter could change this. See [https://docs.ansible.com/ansible/2.9/reference\_appendices/interpreter\_discovery.html](https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html) for more information.”  
]  
}  
}

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [February 9, 2022, 9:34pm UTC](https://forum.ansible.com/t/action-failed-when-not-working-with-condition/36311/6 "2022-02-09T21:34:07Z")

</div>

the 'failed\_when' seems to be working fine, stdout does not have  
'FAILED' in it, rc is 0 .. so the issue is that you are not getting  
the failure you expect.
