In ansbile playbook, the below style can be used to check the result of a script or command, and give a meaningful message if the task fails.
- name: check stack existance hosts: localhost any_errors_fatal: True tasks: - name: check stack existance command: openstack stack show {{ stack_name }} register: result ignore_errors: true - name: check stack existance result fail: msg="stack {{ stack_name }} exists" when: result is succeeded
In normal cases, the first task will fail and the second task will be skipped as below, when the playbook is run.
PLAY [check stack existance] **********************************************************
TASK [check stack existance] **********************************************************
fatal: [localhost]: FAILED! => {“changed”: true, “cmd”: [“openstack”, “stack”, “show”, “leonstack”], “delta”: “0:00:00.773096”, “end”: “2018-09-10 20:59:32.244563”, “msg”: “non-zero return code”, “rc”: 1, “start”: “2018-09-10 20:59:31.471467”, “stderr”: “Stack not found: leonstack”, “stderr_lines”: [“Stack not found: leonstack”], “stdout”: “”, “stdout_lines”: []}
…ignoringTASK [check stack existance result] ***************************************************
skipping: [localhost]PLAY RECAP ****************************************************************************
localhost : ok=1 changed=1 unreachable=0 failed=0
however, this error message sometimes may confuse customers because it is reporting failure. Our product tester also complained about this. After searching this ignoring issue, I found a good solution as below:
- name: check stack existance hosts: localhost any_errors_fatal: True tasks: - name: check stack existance command: openstack stack show {{ stack_name }} register: result failed_when: false - name: check stack existance result fail: msg="stack {{ stack_name }} exists" when: result.rc == 0
Using this style, the first task will always succeed and the second task will give a meaningful message if the result of the first task doesn’t meet the expect.
Below is a successful case.
PLAY [check stack existance] **********************************************************
TASK [check stack existance] **********************************************************
changed: [localhost]TASK [check stack existance result] ***************************************************
skipping: [localhost]PLAY RECAP ****************************************************************************
localhost : ok=1 changed=1 unreachable=0 failed=0
And below is a failure case.
PLAY [check stack existance] **********************************************************
TASK [check stack existance] **********************************************************
changed: [localhost]TASK [check stack existance result] ***************************************************
fatal: [localhost]: FAILED! => {“changed”: false, “msg”: “stack leonstack exists”}NO MORE HOSTS LEFT ********************************************************************
NO MORE HOSTS LEFT ********************************************************************
to retry, use: –limit @/home/leon/demo.retryPLAY RECAP ****************************************************************************
localhost : ok=1 changed=1 unreachable=0 failed=1
Ansible is really a good automation tool. Hope this post can help you.