From aa4d53ccdfe9fb8f5a97e058703286adfbc91d08 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Mon, 9 Nov 2020 16:21:17 -0500 Subject: [PATCH] ensure local exposes correct user (#72543) * ensure local exposes correct user avoid corner case in which delegation relied on playcontext fallback which was removed fixes #72541 --- changelogs/fragments/ensure_local_user_correctness.yml | 2 ++ lib/ansible/plugins/connection/local.py | 3 ++- .../targets/delegate_to/delegate_local_from_root.yml | 10 ++++++++++ test/integration/targets/delegate_to/files/testfile | 1 + test/integration/targets/delegate_to/runme.sh | 3 ++- 5 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/ensure_local_user_correctness.yml create mode 100644 test/integration/targets/delegate_to/delegate_local_from_root.yml create mode 100644 test/integration/targets/delegate_to/files/testfile diff --git a/changelogs/fragments/ensure_local_user_correctness.yml b/changelogs/fragments/ensure_local_user_correctness.yml new file mode 100644 index 00000000000..913b1095e21 --- /dev/null +++ b/changelogs/fragments/ensure_local_user_correctness.yml @@ -0,0 +1,2 @@ +bugfixes: + - ensure 'local' connection always has the correct default user for actions to consume. diff --git a/lib/ansible/plugins/connection/local.py b/lib/ansible/plugins/connection/local.py index c3e7683a66c..2ababb061af 100644 --- a/lib/ansible/plugins/connection/local.py +++ b/lib/ansible/plugins/connection/local.py @@ -44,13 +44,14 @@ class Connection(ConnectionBase): super(Connection, self).__init__(*args, **kwargs) self.cwd = None + self.default_user = getpass.getuser() def _connect(self): ''' connect to the local host; nothing to do here ''' # Because we haven't made any remote connection we're running as # the local user, rather than as whatever is configured in remote_user. - self._play_context.remote_user = getpass.getuser() + self._play_context.remote_user = self.default_user if not self._connected: display.vvv(u"ESTABLISH LOCAL CONNECTION FOR USER: {0}".format(self._play_context.remote_user), host=self._play_context.remote_addr) diff --git a/test/integration/targets/delegate_to/delegate_local_from_root.yml b/test/integration/targets/delegate_to/delegate_local_from_root.yml new file mode 100644 index 00000000000..c9be4ff2337 --- /dev/null +++ b/test/integration/targets/delegate_to/delegate_local_from_root.yml @@ -0,0 +1,10 @@ +- name: handle case from issue 72541 + hosts: testhost + gather_facts: false + remote_user: root + tasks: + - name: ensure we copy w/o errors due to remote user not being overriden + copy: + src: testfile + dest: "{{ playbook_dir }}" + delegate_to: localhost diff --git a/test/integration/targets/delegate_to/files/testfile b/test/integration/targets/delegate_to/files/testfile new file mode 100644 index 00000000000..492bafce648 --- /dev/null +++ b/test/integration/targets/delegate_to/files/testfile @@ -0,0 +1 @@ +nothing special diff --git a/test/integration/targets/delegate_to/runme.sh b/test/integration/targets/delegate_to/runme.sh index 72d3215318c..0ec570b44cd 100755 --- a/test/integration/targets/delegate_to/runme.sh +++ b/test/integration/targets/delegate_to/runme.sh @@ -72,4 +72,5 @@ ln -s python secondpython ansible-playbook verify_interpreter.yml -i inventory_interpreters -v "$@" ansible-playbook discovery_applied.yml -i inventory -v "$@" ansible-playbook resolve_vars.yml -i inventory -v "$@" -ansible-playbook test_delegate_to_lookup_context.yml -i inventory -v "$@" \ No newline at end of file +ansible-playbook test_delegate_to_lookup_context.yml -i inventory -v "$@" +ansible-playbook delegate_local_from_root.yml -i inventory -v "$@" -e 'ansible_user=root'