Reputation: 67
I'm having a problem with one of my Ansible tasks constantly failing. However, the problem is not that the task is failing (I'm expecting it to fail) but what I want to do is show it as successful even though it failed.
The reason why I want to do this is because the task is running a Powershell script on a Windows server using WinRM and the Powershell script is enabling certificate authentication for WinRM. This causes the WinRM connection to the server to be aborted when the WinRM service is reset on the Windows server. Thus, the task will fail when these changes to WinRM is applied.
The fact that the task fails is fine since the next task in the playbook is checking if the changes for WinRM was applied successfully. Thus, the task is run with the parameter "ignore_errors: yes". However, I would like to show the task as successful, even though it fails. I have tried to set the parameter "failed_when: false" (task should always be successful) and "no_log: true", but this does not seem to have had any affect on the task and it still gives the following output:
fatal: [X.X.X.X]: FAILED! => {"censored": "the output has been hidden due to the fact that 'no_log: true' was specified for this result"}
The complete task looks like this:
- name: Activate certificate authentication for WinRM on server
ansible.windows.win_powershell:
script: |
{{ remote_working_dir }}\ConfigureWinRMCertificateAuthentication.ps1
ignore_errors: yes
failed_when: false
no_log: true
The Powershell script "ConfigureWinRMCertificateAuthentication.ps1" is uploaded to the server in {{ remote_working_dir }} before the execution of this task. The Powershell script simply configures CA certificates, user certificates and server certificates to be used by the WinRM service, creates a new WSMan instance with the new certificate and then enables certificate based authentication for WinRM.
The error when the task is run without "no_log: true" is:
requests.exceptions.ConnectionError: ('Connection aborted.', OSError("(104, 'ECONNRESET')"))
fatal: [172.27.25.37]: FAILED! => {
"msg": "Unexpected failure during module execution.",
"stdout": ""
TL;DR: The question I want to ask is therefor, is there any way to show a task with a connection aborted during task execution as successful?
Edit:
Adding the output of the registered result of the task:
ok: [X.X.X.X] => {
"changed": false,
"msg": {
"exception": "Traceback (most recent call last):\n File \"/usr/lib/python3/dist-packages/urllib3/connectionpool.py\", line 665, in urlopen\n httplib_response = self._make_request(\n File \"/usr/lib/python3/dist-packages/urllib3/connectionpool.py\", line 421, in _make_request\n six.raise_from(e, None)\n File \"<string>\", line 3, in raise_from\n File \"/usr/lib/python3/dist-packages/urllib3/connectionpool.py\", line 416, in _make_request\n httplib_response = conn.getresponse()\n File \"/usr/lib/python3.8/http/client.py\", line 1348, in getresponse\n response.begin()\n File \"/usr/lib/python3.8/http/client.py\", line 316, in begin\n version, status, reason = self._read_status()\n File \"/usr/lib/python3.8/http/client.py\", line 277, in _read_status\n line = str(self.fp.readline(_MAXLINE + 1), \"iso-8859-1\")\n File \"/usr/lib/python3.8/socket.py\", line 669, in readinto\n return self._sock.recv_into(b)\n File \"/usr/lib/python3/dist-packages/urllib3/contrib/pyopenssl.py\", line 328, in recv_into\n return self.recv_into(*args, **kwargs)\n File \"/usr/lib/python3/dist-packages/urllib3/contrib/pyopenssl.py\", line 318, in recv_into\n raise SocketError(str(e))\nOSError: (104, 'ECONNRESET')\n\nDuring handling of the above exception, another exception occurred:\n\nTraceback (most recent call last):\n File \"/usr/lib/python3/dist-packages/requests/adapters.py\", line 439, in send\n resp = conn.urlopen(\n File \"/usr/lib/python3/dist-packages/urllib3/connectionpool.py\", line 719, in urlopen\n retries = retries.increment(\n File \"/usr/lib/python3/dist-packages/urllib3/util/retry.py\", line 400, in increment\n raise six.reraise(type(error), error, _stacktrace)\n File \"/usr/lib/python3/dist-packages/six.py\", line 702, in reraise\n raise value.with_traceback(tb)\n File \"/usr/lib/python3/dist-packages/urllib3/connectionpool.py\", line 665, in urlopen\n httplib_response = self._make_request(\n File \"/usr/lib/python3/dist-packages/urllib3/connectionpool.py\", line 421, in _make_request\n six.raise_from(e, None)\n File \"<string>\", line 3, in raise_from\n File \"/usr/lib/python3/dist-packages/urllib3/connectionpool.py\", line 416, in _make_request\n httplib_response = conn.getresponse()\n File \"/usr/lib/python3.8/http/client.py\", line 1348, in getresponse\n response.begin()\n File \"/usr/lib/python3.8/http/client.py\", line 316, in begin\n version, status, reason = self._read_status()\n File \"/usr/lib/python3.8/http/client.py\", line 277, in _read_status\n line = str(self.fp.readline(_MAXLINE + 1), \"iso-8859-1\")\n File \"/usr/lib/python3.8/socket.py\", line 669, in readinto\n return self._sock.recv_into(b)\n File \"/usr/lib/python3/dist-packages/urllib3/contrib/pyopenssl.py\", line 328, in recv_into\n return self.recv_into(*args, **kwargs)\n File \"/usr/lib/python3/dist-packages/urllib3/contrib/pyopenssl.py\", line 318, in recv_into\n raise SocketError(str(e))\nurllib3.exceptions.ProtocolError: ('Connection aborted.', OSError(\"(104, 'ECONNRESET')\"))\n\nDuring handling of the above exception, another exception occurred:\n\nTraceback (most recent call last):\n File \"/usr/lib/python3/dist-packages/ansible/executor/task_executor.py\", line 146, in run\n res = self._execute()\n File \"/usr/lib/python3/dist-packages/ansible/executor/task_executor.py\", line 645, in _execute\n result = self._handler.run(task_vars=variables)\n File \"/usr/lib/python3/dist-packages/ansible/plugins/action/normal.py\", line 46, in run\n result = merge_hash(result, self._execute_module(task_vars=task_vars, wrap_async=wrap_async))\n File \"/usr/lib/python3/dist-packages/ansible/plugins/action/__init__.py\", line 923, in _execute_module\n res = self._low_level_execute_command(cmd, sudoable=sudoable, in_data=in_data)\n File \"/usr/lib/python3/dist-packages/ansible/plugins/action/__init__.py\", line 1071, in _low_level_execute_command\n rc, stdout, stderr = self._connection.exec_command(cmd, in_data=in_data, sudoable=sudoable)\n File \"/usr/lib/python3/dist-packages/ansible/plugins/connection/winrm.py\", line 548, in exec_command\n result = self._winrm_exec(cmd_parts[0], cmd_parts[1:], from_exec=True, stdin_iterator=stdin_iterator)\n File \"/usr/lib/python3/dist-packages/ansible/plugins/connection/winrm.py\", line 475, in _winrm_exec\n resptuple = self.protocol.get_command_output(self.shell_id, command_id)\n File \"/usr/local/lib/python3.8/dist-packages/winrm/protocol.py\", line 449, in get_command_output\n self._raw_get_command_output(shell_id, command_id)\n File \"/usr/local/lib/python3.8/dist-packages/winrm/protocol.py\", line 468, in _raw_get_command_output\n res = self.send_message(xmltodict.unparse(req))\n File \"/usr/local/lib/python3.8/dist-packages/winrm/protocol.py\", line 243, in send_message\n resp = self.transport.send_message(message)\n File \"/usr/local/lib/python3.8/dist-packages/winrm/transport.py\", line 323, in send_message\n response = self._send_message_request(prepared_request, message)\n File \"/usr/local/lib/python3.8/dist-packages/winrm/transport.py\", line 328, in _send_message_request\n response = self.session.send(prepared_request, timeout=self.read_timeout_sec)\n File \"/usr/lib/python3/dist-packages/requests/sessions.py\", line 646, in send\n r = adapter.send(request, **kwargs)\n File \"/usr/lib/python3/dist-packages/requests/adapters.py\", line 498, in send\n raise ConnectionError(err, request=request)\nrequests.exceptions.ConnectionError: ('Connection aborted.', OSError(\"(104, 'ECONNRESET')\"))\n",
"failed": true,
"msg": "Unexpected failure during module execution.",
"stdout": ""
}
}
Edit 2:
Running the task with the following configuration:
- name: Activate certificate authentication for WinRM on server
ansible.windows.win_powershell:
script: |
{{ remote_working_dir }}\ConfigureWinRMCertificateAuthentication.ps1
register: test
ignore_errors: yes
failed_when: rc != 0
no_log: true
- name: Dump results
ansible.builtin.debug:
msg: "{{ test }}"
Produces the following debug output (and a failed task):
TASK [Dump results] *********************************************************************************************************************************************************************************************************************************************
ok: [172.27.25.37] => {
"changed": false,
"msg": {
"exception": "Traceback (most recent call last):\n File \"/usr/lib/python3/dist-packages/urllib3/connectionpool.py\", line 665, in urlopen\n httplib_response = self._make_request(\n File \"/usr/lib/python3/dist-packages/urllib3/connectionpool.py\", line 421, in _make_request\n six.raise_from(e, None)\n File \"<string>\", line 3, in raise_from\n File \"/usr/lib/python3/dist-packages/urllib3/connectionpool.py\", line 416, in _make_request\n httplib_response = conn.getresponse()\n File \"/usr/lib/python3.8/http/client.py\", line 1348, in getresponse\n response.begin()\n File \"/usr/lib/python3.8/http/client.py\", line 316, in begin\n version, status, reason = self._read_status()\n File \"/usr/lib/python3.8/http/client.py\", line 277, in _read_status\n line = str(self.fp.readline(_MAXLINE + 1), \"iso-8859-1\")\n File \"/usr/lib/python3.8/socket.py\", line 669, in readinto\n return self._sock.recv_into(b)\n File \"/usr/lib/python3/dist-packages/urllib3/contrib/pyopenssl.py\", line 328, in recv_into\n return self.recv_into(*args, **kwargs)\n File \"/usr/lib/python3/dist-packages/urllib3/contrib/pyopenssl.py\", line 318, in recv_into\n raise SocketError(str(e))\nOSError: (104, 'ECONNRESET')\n\nDuring handling of the above exception, another exception occurred:\n\nTraceback (most recent call last):\n File \"/usr/lib/python3/dist-packages/requests/adapters.py\", line 439, in send\n resp = conn.urlopen(\n File \"/usr/lib/python3/dist-packages/urllib3/connectionpool.py\", line 719, in urlopen\n retries = retries.increment(\n File \"/usr/lib/python3/dist-packages/urllib3/util/retry.py\", line 400, in increment\n raise six.reraise(type(error), error, _stacktrace)\n File \"/usr/lib/python3/dist-packages/six.py\", line 702, in reraise\n raise value.with_traceback(tb)\n File \"/usr/lib/python3/dist-packages/urllib3/connectionpool.py\", line 665, in urlopen\n httplib_response = self._make_request(\n File \"/usr/lib/python3/dist-packages/urllib3/connectionpool.py\", line 421, in _make_request\n six.raise_from(e, None)\n File \"<string>\", line 3, in raise_from\n File \"/usr/lib/python3/dist-packages/urllib3/connectionpool.py\", line 416, in _make_request\n httplib_response = conn.getresponse()\n File \"/usr/lib/python3.8/http/client.py\", line 1348, in getresponse\n response.begin()\n File \"/usr/lib/python3.8/http/client.py\", line 316, in begin\n version, status, reason = self._read_status()\n File \"/usr/lib/python3.8/http/client.py\", line 277, in _read_status\n line = str(self.fp.readline(_MAXLINE + 1), \"iso-8859-1\")\n File \"/usr/lib/python3.8/socket.py\", line 669, in readinto\n return self._sock.recv_into(b)\n File \"/usr/lib/python3/dist-packages/urllib3/contrib/pyopenssl.py\", line 328, in recv_into\n return self.recv_into(*args, **kwargs)\n File \"/usr/lib/python3/dist-packages/urllib3/contrib/pyopenssl.py\", line 318, in recv_into\n raise SocketError(str(e))\nurllib3.exceptions.ProtocolError: ('Connection aborted.', OSError(\"(104, 'ECONNRESET')\"))\n\nDuring handling of the above exception, another exception occurred:\n\nTraceback (most recent call last):\n File \"/usr/lib/python3/dist-packages/ansible/executor/task_executor.py\", line 146, in run\n res = self._execute()\n File \"/usr/lib/python3/dist-packages/ansible/executor/task_executor.py\", line 645, in _execute\n result = self._handler.run(task_vars=variables)\n File \"/usr/lib/python3/dist-packages/ansible/plugins/action/normal.py\", line 46, in run\n result = merge_hash(result, self._execute_module(task_vars=task_vars, wrap_async=wrap_async))\n File \"/usr/lib/python3/dist-packages/ansible/plugins/action/__init__.py\", line 923, in _execute_module\n res = self._low_level_execute_command(cmd, sudoable=sudoable, in_data=in_data)\n File \"/usr/lib/python3/dist-packages/ansible/plugins/action/__init__.py\", line 1071, in _low_level_execute_command\n rc, stdout, stderr = self._connection.exec_command(cmd, in_data=in_data, sudoable=sudoable)\n File \"/usr/lib/python3/dist-packages/ansible/plugins/connection/winrm.py\", line 548, in exec_command\n result = self._winrm_exec(cmd_parts[0], cmd_parts[1:], from_exec=True, stdin_iterator=stdin_iterator)\n File \"/usr/lib/python3/dist-packages/ansible/plugins/connection/winrm.py\", line 475, in _winrm_exec\n resptuple = self.protocol.get_command_output(self.shell_id, command_id)\n File \"/usr/local/lib/python3.8/dist-packages/winrm/protocol.py\", line 449, in get_command_output\n self._raw_get_command_output(shell_id, command_id)\n File \"/usr/local/lib/python3.8/dist-packages/winrm/protocol.py\", line 468, in _raw_get_command_output\n res = self.send_message(xmltodict.unparse(req))\n File \"/usr/local/lib/python3.8/dist-packages/winrm/protocol.py\", line 243, in send_message\n resp = self.transport.send_message(message)\n File \"/usr/local/lib/python3.8/dist-packages/winrm/transport.py\", line 323, in send_message\n response = self._send_message_request(prepared_request, message)\n File \"/usr/local/lib/python3.8/dist-packages/winrm/transport.py\", line 328, in _send_message_request\n response = self.session.send(prepared_request, timeout=self.read_timeout_sec)\n File \"/usr/lib/python3/dist-packages/requests/sessions.py\", line 646, in send\n r = adapter.send(request, **kwargs)\n File \"/usr/lib/python3/dist-packages/requests/adapters.py\", line 498, in send\n raise ConnectionError(err, request=request)\nrequests.exceptions.ConnectionError: ('Connection aborted.', OSError(\"(104, 'ECONNRESET')\"))\n",
"failed": true,
"msg": "Unexpected failure during module execution.",
"stdout": ""
}
}
And then running the task with the following configuration:
- name: Activate certificate authentication for WinRM on server
ansible.windows.win_powershell:
script: |
{{ remote_working_dir }}\ConfigureWinRMCertificateAuthentication.ps1
register: test
ignore_errors: yes
failed_when: rc == 0
no_log: true
- name: Dump results
ansible.builtin.debug:
msg: "{{ test }}"
Produces the following debug output (and also a failed task):
TASK [Dump results] *********************************************************************************************************************************************************************************************************************************************
ok: [172.27.25.37] => {
"changed": false,
"msg": {
"exception": "Traceback (most recent call last):\n File \"/usr/lib/python3/dist-packages/urllib3/connectionpool.py\", line 665, in urlopen\n httplib_response = self._make_request(\n File \"/usr/lib/python3/dist-packages/urllib3/connectionpool.py\", line 421, in _make_request\n six.raise_from(e, None)\n File \"<string>\", line 3, in raise_from\n File \"/usr/lib/python3/dist-packages/urllib3/connectionpool.py\", line 416, in _make_request\n httplib_response = conn.getresponse()\n File \"/usr/lib/python3.8/http/client.py\", line 1348, in getresponse\n response.begin()\n File \"/usr/lib/python3.8/http/client.py\", line 316, in begin\n version, status, reason = self._read_status()\n File \"/usr/lib/python3.8/http/client.py\", line 277, in _read_status\n line = str(self.fp.readline(_MAXLINE + 1), \"iso-8859-1\")\n File \"/usr/lib/python3.8/socket.py\", line 669, in readinto\n return self._sock.recv_into(b)\n File \"/usr/lib/python3/dist-packages/urllib3/contrib/pyopenssl.py\", line 328, in recv_into\n return self.recv_into(*args, **kwargs)\n File \"/usr/lib/python3/dist-packages/urllib3/contrib/pyopenssl.py\", line 318, in recv_into\n raise SocketError(str(e))\nOSError: (104, 'ECONNRESET')\n\nDuring handling of the above exception, another exception occurred:\n\nTraceback (most recent call last):\n File \"/usr/lib/python3/dist-packages/requests/adapters.py\", line 439, in send\n resp = conn.urlopen(\n File \"/usr/lib/python3/dist-packages/urllib3/connectionpool.py\", line 719, in urlopen\n retries = retries.increment(\n File \"/usr/lib/python3/dist-packages/urllib3/util/retry.py\", line 400, in increment\n raise six.reraise(type(error), error, _stacktrace)\n File \"/usr/lib/python3/dist-packages/six.py\", line 702, in reraise\n raise value.with_traceback(tb)\n File \"/usr/lib/python3/dist-packages/urllib3/connectionpool.py\", line 665, in urlopen\n httplib_response = self._make_request(\n File \"/usr/lib/python3/dist-packages/urllib3/connectionpool.py\", line 421, in _make_request\n six.raise_from(e, None)\n File \"<string>\", line 3, in raise_from\n File \"/usr/lib/python3/dist-packages/urllib3/connectionpool.py\", line 416, in _make_request\n httplib_response = conn.getresponse()\n File \"/usr/lib/python3.8/http/client.py\", line 1348, in getresponse\n response.begin()\n File \"/usr/lib/python3.8/http/client.py\", line 316, in begin\n version, status, reason = self._read_status()\n File \"/usr/lib/python3.8/http/client.py\", line 277, in _read_status\n line = str(self.fp.readline(_MAXLINE + 1), \"iso-8859-1\")\n File \"/usr/lib/python3.8/socket.py\", line 669, in readinto\n return self._sock.recv_into(b)\n File \"/usr/lib/python3/dist-packages/urllib3/contrib/pyopenssl.py\", line 328, in recv_into\n return self.recv_into(*args, **kwargs)\n File \"/usr/lib/python3/dist-packages/urllib3/contrib/pyopenssl.py\", line 318, in recv_into\n raise SocketError(str(e))\nurllib3.exceptions.ProtocolError: ('Connection aborted.', OSError(\"(104, 'ECONNRESET')\"))\n\nDuring handling of the above exception, another exception occurred:\n\nTraceback (most recent call last):\n File \"/usr/lib/python3/dist-packages/ansible/executor/task_executor.py\", line 146, in run\n res = self._execute()\n File \"/usr/lib/python3/dist-packages/ansible/executor/task_executor.py\", line 645, in _execute\n result = self._handler.run(task_vars=variables)\n File \"/usr/lib/python3/dist-packages/ansible/plugins/action/normal.py\", line 46, in run\n result = merge_hash(result, self._execute_module(task_vars=task_vars, wrap_async=wrap_async))\n File \"/usr/lib/python3/dist-packages/ansible/plugins/action/__init__.py\", line 923, in _execute_module\n res = self._low_level_execute_command(cmd, sudoable=sudoable, in_data=in_data)\n File \"/usr/lib/python3/dist-packages/ansible/plugins/action/__init__.py\", line 1071, in _low_level_execute_command\n rc, stdout, stderr = self._connection.exec_command(cmd, in_data=in_data, sudoable=sudoable)\n File \"/usr/lib/python3/dist-packages/ansible/plugins/connection/winrm.py\", line 548, in exec_command\n result = self._winrm_exec(cmd_parts[0], cmd_parts[1:], from_exec=True, stdin_iterator=stdin_iterator)\n File \"/usr/lib/python3/dist-packages/ansible/plugins/connection/winrm.py\", line 475, in _winrm_exec\n resptuple = self.protocol.get_command_output(self.shell_id, command_id)\n File \"/usr/local/lib/python3.8/dist-packages/winrm/protocol.py\", line 449, in get_command_output\n self._raw_get_command_output(shell_id, command_id)\n File \"/usr/local/lib/python3.8/dist-packages/winrm/protocol.py\", line 468, in _raw_get_command_output\n res = self.send_message(xmltodict.unparse(req))\n File \"/usr/local/lib/python3.8/dist-packages/winrm/protocol.py\", line 243, in send_message\n resp = self.transport.send_message(message)\n File \"/usr/local/lib/python3.8/dist-packages/winrm/transport.py\", line 323, in send_message\n response = self._send_message_request(prepared_request, message)\n File \"/usr/local/lib/python3.8/dist-packages/winrm/transport.py\", line 328, in _send_message_request\n response = self.session.send(prepared_request, timeout=self.read_timeout_sec)\n File \"/usr/lib/python3/dist-packages/requests/sessions.py\", line 646, in send\n r = adapter.send(request, **kwargs)\n File \"/usr/lib/python3/dist-packages/requests/adapters.py\", line 498, in send\n raise ConnectionError(err, request=request)\nrequests.exceptions.ConnectionError: ('Connection aborted.', OSError(\"(104, 'ECONNRESET')\"))\n",
"failed": true,
"msg": "Unexpected failure during module execution.",
"stdout": ""
}
}
Thus, changing the failed_when
parameter (no matter what I set it to) seems to have no effect on the result of the task.
Upvotes: 0
Views: 2261
Reputation: 2939
The normal workaround for executing tasks that interrupt the connection is to use async
in fire-and-forget mode. This pattern used to be used a lot for server reboots, before the reboot
action was implemented.
- name: Activate certificate authentication for WinRM on server
ansible.windows.win_powershell:
script: |
Start-Sleep -s 2
{{ remote_working_dir }}\ConfigureWinRMCertificateAuthentication.ps1
async: 60
poll: 0
The added sleep at the beginning of the script ensures that there is time for the initial async status to be returned to the Ansible controller before the connection breaks; async: 60
runs the task asynchronously with a 60 second timeout, and poll: 0
tells Ansible to immediately move on to the next task.
This is often followed by something like
- name: Wait for the server to come back
wait_for_connection:
delay: 30
timeout: 300
to gracefully retry connections until the server is reachable again.
Upvotes: 3
Reputation: 5625
Use a register and ".failed
". Here's one of my own cases where I am testing a kubernetes deployment fails to deploy as expected:
- name: Test webhook rejects with no label
kubernetes.core.k8s:
state: present
namespace: "{{ webhook_namespace }}"
definition: "{{ lookup('template', 'roles/gke-setup-tasks/files/nginx-test-webhook-no-label.yaml') | from_yaml }}"
register: test
failed_when: not test.failed
So I declare the task to have been considered "failed" if it not failed. In other words, if the task succeeded (the deployment deployed), then I consider the task as failed.
To respond to a few more queries in the comments here's another example where you can fail based on rc (or return code -- on Windows, I believe this is also known as the errorlevel in batch scripting)
We start with a simple task that will fail:
- name: Do something that fails
shell:
cmd: /usr/bin/false
That gives us the expected error:
TASK [appinstalls : Do something that fails] ***********************************
fatal: [localhost]: FAILED! => {"changed": true, "cmd": "/usr/bin/false", "delta": "0:00:00.012978", "end": "2022-01-20 14:45:51.400921", "msg": "non-zero return code", "rc": 1, "start": "2022-01-20 14:45:51.387943", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}
Now lets add a register and dump the contents. We have to add ignore_errors
so the execution continues
- name: Do something that fails
shell:
cmd: /usr/bin/false
register: test
ignore_errors: true
- debug:
msg: "{{test}}"
TASK [appinstalls : Do something that fails] *************************************************************************************************************************
fatal: [localhost]: FAILED! => {"changed": true, "cmd": "/usr/bin/false", "delta": "0:00:00.010801", "end": "2022-01-20 14:48:35.689258", "msg": "non-zero return code", "rc": 1, "start": "2022-01-20 14:48:35.678457", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}
...ignoring
TASK [appinstalls : debug] *******************************************************************************************************************************************
ok: [localhost] => {
"msg": {
"changed": true,
"cmd": "/usr/bin/false",
"delta": "0:00:00.010801",
"end": "2022-01-20 14:48:35.689258",
"failed": true,
"msg": "non-zero return code",
"rc": 1,
"start": "2022-01-20 14:48:35.678457",
"stderr": "",
"stderr_lines": [],
"stdout": "",
"stdout_lines": []
}
}
Now notice how rc
here is 1
? Let's change the task so that we fail the task only if the rc
is 0
(i.e. if the rc
, or return code is zero, we consider it a failure). We also remove the ignore_errors
now:
- name: Do something that fails
shell:
cmd: /usr/bin/false
register: test
failed_when: test.rc == 0
- debug:
msg: "{{test}}"
Now when we run it, it does not fail, even though the command in the task actually failed.
TASK [appinstalls : Do something that fails] *************************************************************************************************************************
changed: [localhost]
TASK [appinstalls : debug] *******************************************************************************************************************************************
ok: [localhost] => {
"msg": {
"changed": true,
"cmd": "/usr/bin/false",
"delta": "0:00:00.006484",
"end": "2022-01-20 14:52:01.010044",
"failed": false,
"failed_when_result": false,
"msg": "non-zero return code",
"rc": 1,
"start": "2022-01-20 14:52:01.003560",
"stderr": "",
"stderr_lines": [],
"stdout": "",
"stdout_lines": []
}
}
So what happens if we do the same thing, but with a command that actually succeeds:
- name: Do something that succeeds
shell:
cmd: echo "This is a success"
register: test
failed_when: test.rc == 0
- debug:
msg: "{{test}}"
TASK [appinstalls : Do something that succeeds] **********************************************************************************************************************
fatal: [localhost]: FAILED! => {"changed": true, "cmd": "echo \"This is a success\"", "delta": "0:00:00.005851", "end": "2022-01-20 14:54:46.082148", "failed_when_result": true, "msg": "", "rc": 0, "start": "2022-01-20 14:54:46.076297", "stderr": "", "stderr_lines": [], "stdout": "This is a success", "stdout_lines": ["This is a success"]}
So using the rc
or the failed
attribute will let you override or change what Ansible evaluates a task result as.
Upvotes: -1