How do I continue a handler task on failure?

I currently have a playbook (not completed) that installs some packages and sets up MySQL. I have one task that notifies several handlers if that task is changed. However, one of the handler tasks, which uses the ‘expect’ module, fails. The task first installs mysqld if necessary. If it installs it, it notifies six different handlers to run. One of them is " - Secure MySQL Installation". This specific task handler is the one that requires the ‘expect’ module because it prompts you for a series of questions to secure MySQL.

Here is where it gets tricky. One of the questions always fails when I answer yes. The question is: “Remove test database and access to it?” When the expect module sees this, I have it responding with a “y” for yes. It then continues to the last question and answers that one as well. The issue is that the “Remove test database and access to it?” question always throws the following error because the “test” database doesn’t exist:

Dropping test database… ERROR 1008 (HY000) at line 1: Can’t drop database ‘test’; database doesn’t exist … Failed! Not critical, keep moving…

  • Removing privileges on test database… … Success!

However, the second part of that same question succeeds, which is “removing privileges on test database”

If I run it manually outside of Ansible, the result is the same. So, how do I handle this error so that the next task handler in my playbook continues even though this one failed or it thinks it failed? I want to know of my failure but I also want it to continue without halting the playbook. I have tried adding the following:

register: result failed_when: "'Can't drop database' in result.stdout" #failed_when: "'Can't drop database' in result.stderr"

However, that fails with the following error when the playbook runs:

RUNNING HANDLER [Secure MySQL Installation] ************************************ fatal: [subdomain.test.com]: FAILED! => {“failed”: true, “msg”: “The conditional check ‘‘Can’t drop database’ in result.stdout’ failed. The error was: template error while templating string: expected token ‘end of statement block’, got ‘t’. String: {% if ‘Can’t drop database’ in result.stdout %} True {% else %} False {% endif %}”}

What am I doing wrong here?

This is a copy of my playbook:

`

I wanted to point out that I noticed my issue was a bug per https://github.com/ansible/ansible/issues/15572

However, I grabbed the latest version of Ansible, which is 2.2 and still have the same issue. The problem is that my handler fails and doesn’t complete the last question/response in expect so my privileges are not reloaded.

Anyone out there with any idea on this issue?

If I understand you correctly "ignore_errors: yes" is what you are looking for.
https://docs.ansible.com/ansible/playbooks_error_handling.html#ignoring-failed-commands

^^^^

Do not use a sinqle quote in there. Then your conditional could make
sense. I do not know if it works, though.

Johannes

I need to use single quotes here because ‘Can’t drop database’ is the failure I am filtering in result.stdout

So I already have ignore_errors: yes set. What I am trying to figure out is why my response on my handler is failing. So here is my handler:

`

  • name: Secure MySQL Installation
    register: result
    failed_when: “Can’t drop database in result.stdout”
    ignore_errors: yes
    expect:
    command: /bin/mysql_secure_installation
    responses:
    ‘Enter current password for root (enter for none)’: “”
    ‘Set root password? [Y/n]’: “y”
    ‘New password’: “{{ root }}”
    ‘Re-enter new password’: “{{ root }}”
    ‘Remove anonymous users? [Y/n]’: “y”
    ‘Disallow root login remotely? [Y/n]’: “y”
    ‘Remove test database and access to it? [Y/n]’: “y”
    ‘Reload privileges tables now? [Y/n]’: “y”
    `

So the response 'Remove test database and access to it? [Y/n]': “y” on that task keeps failing with the following error:

RUNNING HANDLER [Secure MySQL Installation] ************************************
fatal: [server1.domain.tld]: FAILED! => {“failed”: true, “msg”: “The conditional check ‘Can’t drop database in result.stdout’ failed. The error was: template error while templating string: unexpected char u"'" at 9. String: {% if Can’t drop database in result.stdout %} True {% else %} False {% endif %}”}
…ignoring

As you can see, it ignores the error because I have ignore_errors: yes but I have that option only because it continues to fail and I just want it to continue with my playbook. The long term solution is that I want it to work when it attempts to run that response so that the next response of ‘Reload privileges table now? [Y/n]’: “y” actually runs. Since the previous response fails, that next one just gets skipped and it moves on to the next task. I really need that response to run because I want that test database to actually be dropped.

I am talking about the single quote inside the Can't. It messes up the
quoting and results in the failure.

Johannes

You get this message because of the single quote in the message field.
Try this
   failed_when: result.stdout.find("Can\\'t drop database")