onknows
onknows

Reputation: 6691

Raise an error based on results of other task

I am using the postgresql_db module that is part of Ansible. For example with something like

- name: Database
  postgresql_db:
    name: "{{ vars[item + '_database_name_version']  }}"
    login_host: "{{ vars[item + '_database_host'] }}"
    login_password: "{{ vars[item + '_database_admin_password'] }}"
    login_user: "{{ vars[item + '_database_admin_username'] }}"
    port: "{{ vars[item + '_database_port'] }}"
    state: restore
    target: "{{ backup_restore[item]['db_tar'] }}"
  when: backup_restore[item]['db_tar'] is defined
  with_items: '{{ backup_restore }}'
  register: db_restore

When I debug output db_restore I see

TASK [backup : db_restore] *****************************************************
ok: [myapp] => {
    "db_restore": {
        "changed": true,
        "msg": "All items completed",
        "results": [
            {
                "ansible_loop_var": "item",
                "changed": true,
                "cmd": "cmd: ****",
                "failed": false,
                "invocation": {
                    "module_args": {
                        "ca_cert": null,
                        "conn_limit": "",
                        "db": "myapp_0_1_0",
                        "encoding": "",
                        "lc_collate": "",
                        "lc_ctype": "",
                        "login_host": "1.1.1.2",
                        "login_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
                        "login_unix_socket": "",
                        "login_user": "ansible",
                        "maintenance_db": "postgres",
                        "name": "myapp_0_1_0",
                        "owner": "",
                        "port": 5432,
                        "session_role": null,
                        "ssl_mode": "prefer",
                        "state": "restore",
                        "target": "/backup/tmp/myapp-myapp/myapp_daily/databases/PostgreSQL.sql.gz",
                        "target_opts": "",
                        "template": ""
                    }
                },
                "item": "myapp",
                "msg": "",
                "rc": 0,
                "stderr": "ERROR:  relation \"myapp_table\" already exists\n",
                "stderr_lines": [
                    "ERROR:  relation \"myapp_table\" already exists"
                ]
            }
        ]
    }
}

Although there is an error during execution of this module as visible in stderr, the Ansible postgresql_db module also returns "failed": false. So it looks like it is ignoring any errors that might occur while running commands to restore the database.

Now I want to add a task to check db_restore for stderr attribute and if present, raise an error so that the user is made aware of the problems.

How can I raise an error? Is there an error module?

Upvotes: 1

Views: 455

Answers (2)

Patrick Pötz
Patrick Pötz

Reputation: 369

I strongly agree with @Stefan Wegener's answer, but want to add another possibility. I'm curious if your problem only occurs because of the loop you wrapped around your task.

Another possible solution would be to add the any_errors_fatal: true clause on play level like described in the Ansible documentation:

- hosts: somehosts
  any_errors_fatal: true
  roles:
    - myrole

See https://docs.ansible.com/ansible/latest/user_guide/playbooks_error_handling.html#aborting-the-play for reference.

Upvotes: 0

Stefan Wegener
Stefan Wegener

Reputation: 746

Yes, there is a module to raise errors which is called fail (https://docs.ansible.com/ansible/latest/modules/fail_module.html)

# Example playbook using fail and when together
- fail:
    msg: The system may not be provisioned according to the CMDB status.
  when: cmdb_status != "to-be-staged"

Another possible way is to use failed_when.

- name: Fail task when the command error output prints FAILED
  command: /usr/bin/example-command -x -y -z
  register: command_result
  failed_when: "'FAILED' in command_result.stderr"

I would recommend you to read the ansible page on error handling (https://docs.ansible.com/ansible/latest/user_guide/playbooks_error_handling.html)

Upvotes: 2

Related Questions