summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAbhijeet Kasurde <akasurde@redhat.com>2019-01-09 17:43:59 +0100
committerBrian Coca <bcoca@users.noreply.github.com>2019-01-09 17:43:59 +0100
commit17bb4f493212d15c80156c8417b9dcf5336ba69a (patch)
tree650e1b23df9e21a8ead81c313c1471374a002510
parentFix sanitizing config lines (#50553) (diff)
downloadansible-17bb4f493212d15c80156c8417b9dcf5336ba69a.tar.xz
ansible-17bb4f493212d15c80156c8417b9dcf5336ba69a.zip
default: callback plugin allow unreachable task to stderr (#50533)
Provide toggle flag to allow display of unreachable task to stderr using default callback plugin. Fixes: #48069 Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
-rw-r--r--changelogs/fragments/48069-default-callback-unreachable_stderr.yml2
-rw-r--r--lib/ansible/plugins/callback/default.py7
-rw-r--r--lib/ansible/utils/module_docs_fragments/default_callback.py4
-rw-r--r--test/integration/inventory3
-rwxr-xr-xtest/integration/targets/callback_default/runme.sh23
-rw-r--r--test/integration/targets/callback_default/test_2.yml6
6 files changed, 39 insertions, 6 deletions
diff --git a/changelogs/fragments/48069-default-callback-unreachable_stderr.yml b/changelogs/fragments/48069-default-callback-unreachable_stderr.yml
new file mode 100644
index 0000000000..5e24007a9d
--- /dev/null
+++ b/changelogs/fragments/48069-default-callback-unreachable_stderr.yml
@@ -0,0 +1,2 @@
+minor_changes:
+- Allow default callback plugin to send unreachable host/task to stderr using toggle flag.
diff --git a/lib/ansible/plugins/callback/default.py b/lib/ansible/plugins/callback/default.py
index 28acef3425..dd6d99f53c 100644
--- a/lib/ansible/plugins/callback/default.py
+++ b/lib/ansible/plugins/callback/default.py
@@ -158,11 +158,10 @@ class CallbackModule(CallbackBase):
delegated_vars = result._result.get('_ansible_delegated_vars', None)
if delegated_vars:
- self._display.display("fatal: [%s -> %s]: UNREACHABLE! => %s" % (result._host.get_name(), delegated_vars['ansible_host'],
- self._dump_results(result._result)),
- color=C.COLOR_UNREACHABLE)
+ msg = "fatal: [%s -> %s]: UNREACHABLE! => %s" % (result._host.get_name(), delegated_vars['ansible_host'], self._dump_results(result._result))
else:
- self._display.display("fatal: [%s]: UNREACHABLE! => %s" % (result._host.get_name(), self._dump_results(result._result)), color=C.COLOR_UNREACHABLE)
+ msg = "fatal: [%s]: UNREACHABLE! => %s" % (result._host.get_name(), self._dump_results(result._result))
+ self._display.display(msg, color=C.COLOR_UNREACHABLE, stderr=self.display_failed_stderr)
def v2_playbook_on_no_hosts_matched(self):
self._display.display("skipping: no hosts matched", color=C.COLOR_SKIP)
diff --git a/lib/ansible/utils/module_docs_fragments/default_callback.py b/lib/ansible/utils/module_docs_fragments/default_callback.py
index 4a343511cd..ec56a28850 100644
--- a/lib/ansible/utils/module_docs_fragments/default_callback.py
+++ b/lib/ansible/utils/module_docs_fragments/default_callback.py
@@ -28,8 +28,8 @@ class ModuleDocFragment(object):
type: boolean
version_added: '2.7'
display_failed_stderr:
- name: Use STDERR for failed tasks
- description: "Toggle to control whether failed tasks are displayed to STDERR (vs. STDOUT)"
+ name: Use STDERR for failed and unreachable tasks
+ description: "Toggle to control whether failed and unreachable tasks are displayed to STDERR (vs. STDOUT)"
default: False
env:
- name: ANSIBLE_DISPLAY_FAILED_STDERR
diff --git a/test/integration/inventory b/test/integration/inventory
index 11a709dee0..b92f861300 100644
--- a/test/integration/inventory
+++ b/test/integration/inventory
@@ -75,3 +75,6 @@ localhost ansible_ssh_host=127.0.0.1 ansible_connection=local
[azure]
localhost ansible_ssh_host=127.0.0.1 ansible_connection=local
+
+[nonexistent]
+testhost5 ansible_host=169.254.199.200
diff --git a/test/integration/targets/callback_default/runme.sh b/test/integration/targets/callback_default/runme.sh
index a10b18c905..40e7b0615a 100755
--- a/test/integration/targets/callback_default/runme.sh
+++ b/test/integration/targets/callback_default/runme.sh
@@ -36,6 +36,15 @@ cleanup() {
if [[ $INIT = 0 ]]; then
rm -rf "${OUTFILE}.*"
fi
+
+ if [[ -f "${BASEFILE}.unreachable.stdout" ]]; then
+ rm -rf "${BASEFILE}.unreachable.stdout"
+ fi
+
+ if [[ -f "${BASEFILE}.unreachable.stderr" ]]; then
+ rm -rf "${BASEFILE}.unreachable.stderr"
+ fi
+
# Restore TTY cols
if [[ -n ${TTY_COLS:-} ]]; then
stty cols "${TTY_COLS}"
@@ -105,3 +114,17 @@ export ANSIBLE_DISPLAY_OK_HOSTS=1
export ANSIBLE_DISPLAY_FAILED_STDERR=1
run_test failed_to_stderr
+
+# Default settings with unreachable tasks
+export DISPLAY_SKIPPED_HOSTS=1
+export ANSIBLE_DISPLAY_OK_HOSTS=1
+export ANSIBLE_DISPLAY_FAILED_STDERR=1
+
+# Check if UNREACHBLE is available in stderr
+set +e
+ansible-playbook -i ../../inventory test_2.yml > >(set +x; tee "${BASEFILE}.unreachable.stdout";) 2> >(set +x; tee "${BASEFILE}.unreachable.stderr" >&2) || true
+set -e
+if test "$(grep -c 'UNREACHABLE' "${BASEFILE}.unreachable.stderr")" -ne 1; then
+ echo "Test failed"
+ exit 1
+fi
diff --git a/test/integration/targets/callback_default/test_2.yml b/test/integration/targets/callback_default/test_2.yml
new file mode 100644
index 0000000000..2daded71e6
--- /dev/null
+++ b/test/integration/targets/callback_default/test_2.yml
@@ -0,0 +1,6 @@
+- hosts: nonexistent
+ gather_facts: no
+ tasks:
+ - name: Test task for unreachable host
+ command: echo foo
+ ignore_errors: True