Action failed_when not working with condition

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,

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

Or any other of the mySQL modules:

https://docs.ansible.com/ansible/latest/collections/community/mysql/index.html

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

Or any other of the mySQL modules:

https://docs.ansible.com/ansible/latest/collections/community/mysql/index.html

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

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 for more information.”
]
}
}

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.