From ed2b957a4f8682c8696b0e0417aad2b913832e2a Mon Sep 17 00:00:00 2001 From: Michele Baldessari Date: Mon, 20 Nov 2017 20:48:50 +0100 Subject: [PATCH] Fix all outputs|failed and outputs is defined The ansible "failed_when" filter that uses a registered output of a previous task piped to the '|failed' filter does not work as expected. Given the following playbook: - name: return code shell: | echo "fail 2" exit 2 failed_when: false log_when: false register: outputs - debug: msg: "rc: {{ outputs.rc }}" - debug: msg="Broken (does not fail as expected)" when: outputs is defined failed_when: outputs|failed - debug: msg="Working (fails as expected)" when: outputs is defined failed_when: outputs.rc != 0 We obtain the following output: TASK [return code] **** changed: [localhost] TASK [debug] ********** ok: [localhost] => { "msg": "rc: 2" } TASK [debug] ********** ok: [localhost] => { "failed_when_result": false, "msg": "Broken (does not fail as expected)" } TASK [debug] ********** fatal: [localhost]: FAILED! => { "failed_when_result": true, "msg": "Working (fails as expected)" } This means that the 'outputs|failed' just does not work at all. Let's move to a more explicit check on the rc code of the registered variable. We also need to fix all the "outputs is defined" checks, because when a task is skipped the registered outputs variable *is* actually defined as the following dictionary: {'skip_reason': u'Conditional result was False', 'skipped': True, 'changed': False} So we use "outputs.rc is defined" in order to make sure that the previous task did indeed run. Closes-Bug: #1733402 Change-Id: I6ef53dc3f9aede42f10c7f110d24722355481261 --- common/deploy-steps-tasks.yaml | 14 +++++++------- extraconfig/services/kubernetes-master.yaml | 4 ++-- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/common/deploy-steps-tasks.yaml b/common/deploy-steps-tasks.yaml index 7675b1a5dd..d045104e6c 100644 --- a/common/deploy-steps-tasks.yaml +++ b/common/deploy-steps-tasks.yaml @@ -25,7 +25,7 @@ no_log: true become: true - debug: var=(outputs.stderr|default('')).split('\n')|union(outputs.stdout_lines|default([])) - when: outputs is defined + when: outputs.rc is defined failed_when: outputs.rc not in [0, 2] ###################################### # Generate config via docker-puppet.py @@ -44,8 +44,8 @@ no_log: true become: true - debug: var=(outputs.stderr|default('')).split('\n')|union(outputs.stdout_lines|default([])) - when: outputs is defined - failed_when: outputs|failed + when: outputs.rc is defined + failed_when: outputs.rc != 0 ################################################## # Per step starting of the containers using paunch ################################################## @@ -70,8 +70,8 @@ no_log: true become: true - debug: var=(outputs.stderr|default('')).split('\n')|union(outputs.stdout_lines|default([])) - when: outputs is defined - failed_when: outputs|failed + when: outputs.rc is defined + failed_when: outputs.rc != 0 ######################################################## # Bootstrap tasks, only performed on bootstrap_server_id ######################################################## @@ -95,5 +95,5 @@ no_log: true become: true - debug: var=(outputs.stderr|default('')).split('\n')|union(outputs.stdout_lines|default([])) - when: outputs is defined - failed_when: outputs|failed + when: outputs.rc is defined + failed_when: outputs.rc != 0 diff --git a/extraconfig/services/kubernetes-master.yaml b/extraconfig/services/kubernetes-master.yaml index 189c084d71..16858cffb8 100644 --- a/extraconfig/services/kubernetes-master.yaml +++ b/extraconfig/services/kubernetes-master.yaml @@ -163,5 +163,5 @@ outputs: - name: print kubespray outputs debug: var: (outputs.stderr|default('')).split('\n')|union(outputs.stdout_lines|default([])) - failed_when: outputs|failed - when: outputs is defined + failed_when: outputs.rc != 0 + when: outputs.rc is defined