Action failed_when not working with condition

71 views
Skip to first unread message

SysAdmin EM

unread,
Feb 9, 2022, 11:52:27 AM2/9/22
to ansible...@googlegroups.com
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,

Rilindo Foster

unread,
Feb 9, 2022, 12:02:07 PM2/9/22
to ansible...@googlegroups.com
Not an answer, but a thought - instead of using the shell module, have you thought about using the mysql_info module?


Or any other of the mySQL modules:


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAGUDtnm3JqG4VQAPMi0RzhFOp-NUCBr5LQWLcof85VEbv03Hfg%40mail.gmail.com.

Rilindo Foster

unread,
Feb 9, 2022, 12:02:21 PM2/9/22
to ansible...@googlegroups.com
Not an answer, but a thought - instead of using the shell module, have you thought about using the mysql_info module?


Or any other of the mySQL modules:

On Feb 9, 2022, at 10:51 AM, SysAdmin EM <ema...@gmail.com> wrote:

Brian Coca

unread,
Feb 9, 2022, 12:42:38 PM2/9/22
to Ansible Project
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
--
----------
Brian Coca

SysAdmin EM

unread,
Feb 9, 2022, 2:07:08 PM2/9/22
to ansible...@googlegroups.com
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."
]
}
}



--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.

Brian Coca

unread,
Feb 9, 2022, 4:35:04 PM2/9/22
to Ansible Project
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.

-
----------
Brian Coca

Reply all
Reply to author
Forward
0 new messages