From 94a81f7b44e53fde4d477bf7ce14741a2ac4f102 Mon Sep 17 00:00:00 2001 From: David Shrewsbury Date: Fri, 17 Jul 2020 15:46:33 -0400 Subject: [PATCH] Make sure ansible_become treated as a boolean (#70484) (#70526) * Make sure ansible_become treated as a boolean (cherry picked from commit 8aca464b8bbd4ecd606cdb14f1a5b9f19f093552) --- changelogs/fragments/70484-bool-ansible-become.yaml | 3 +++ lib/ansible/executor/task_executor.py | 3 ++- test/integration/targets/inventory_ini/aliases | 1 + test/integration/targets/inventory_ini/inventory.ini | 5 +++++ test/integration/targets/inventory_ini/runme.sh | 5 +++++ .../targets/inventory_ini/test_ansible_become.yml | 11 +++++++++++ 6 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/70484-bool-ansible-become.yaml create mode 100644 test/integration/targets/inventory_ini/aliases create mode 100644 test/integration/targets/inventory_ini/inventory.ini create mode 100755 test/integration/targets/inventory_ini/runme.sh create mode 100644 test/integration/targets/inventory_ini/test_ansible_become.yml diff --git a/changelogs/fragments/70484-bool-ansible-become.yaml b/changelogs/fragments/70484-bool-ansible-become.yaml new file mode 100644 index 00000000000..ffe879dae1e --- /dev/null +++ b/changelogs/fragments/70484-bool-ansible-become.yaml @@ -0,0 +1,3 @@ +bugfixes: + - The `ansible_become` value was not being treated as a boolean value when set in an INI format + inventory file (fixes bug https://github.com/ansible/ansible/issues/70476). diff --git a/lib/ansible/executor/task_executor.py b/lib/ansible/executor/task_executor.py index dec4b978643..dc4616a5c8a 100644 --- a/lib/ansible/executor/task_executor.py +++ b/lib/ansible/executor/task_executor.py @@ -19,6 +19,7 @@ from ansible import constants as C from ansible.errors import AnsibleError, AnsibleParserError, AnsibleUndefinedVariable, AnsibleConnectionFailure, AnsibleActionFail, AnsibleActionSkip from ansible.executor.task_result import TaskResult from ansible.executor.module_common import get_action_args_with_defaults +from ansible.module_utils.parsing.convert_bool import boolean from ansible.module_utils.six import iteritems, string_types, binary_type from ansible.module_utils.six.moves import xrange from ansible.module_utils._text import to_text, to_native @@ -909,7 +910,7 @@ class TaskExecutor: raise AnsibleError("the connection plugin '%s' was not found" % conn_type) # load become plugin if needed - if cvars.get('ansible_become', self._task.become): + if boolean(cvars.get('ansible_become', self._task.become)): become_plugin = self._get_become(cvars.get('ansible_become_method', self._task.become_method)) try: diff --git a/test/integration/targets/inventory_ini/aliases b/test/integration/targets/inventory_ini/aliases new file mode 100644 index 00000000000..70a7b7a9f32 --- /dev/null +++ b/test/integration/targets/inventory_ini/aliases @@ -0,0 +1 @@ +shippable/posix/group5 diff --git a/test/integration/targets/inventory_ini/inventory.ini b/test/integration/targets/inventory_ini/inventory.ini new file mode 100644 index 00000000000..a0c99adee06 --- /dev/null +++ b/test/integration/targets/inventory_ini/inventory.ini @@ -0,0 +1,5 @@ +[local] +testhost ansible_connection=local ansible_become=no ansible_become_user=ansibletest1 + +[all:vars] +ansible_python_interpreter="{{ ansible_playbook_python }}" diff --git a/test/integration/targets/inventory_ini/runme.sh b/test/integration/targets/inventory_ini/runme.sh new file mode 100755 index 00000000000..81bf1475904 --- /dev/null +++ b/test/integration/targets/inventory_ini/runme.sh @@ -0,0 +1,5 @@ +#!/usr/bin/env bash + +set -eux + +ansible-playbook -v -i inventory.ini test_ansible_become.yml diff --git a/test/integration/targets/inventory_ini/test_ansible_become.yml b/test/integration/targets/inventory_ini/test_ansible_become.yml new file mode 100644 index 00000000000..55bbe7da3fe --- /dev/null +++ b/test/integration/targets/inventory_ini/test_ansible_become.yml @@ -0,0 +1,11 @@ +- hosts: testhost + gather_facts: no + tasks: + - name: Test proper bool evaluation of ansible_become (issue #70476) + shell: whoami + register: output + + - name: Assert we are NOT the become user specified + assert: + that: + - "output.stdout != 'ansibletest1'"