diff --git a/lib/ansible/runner/__init__.py b/lib/ansible/runner/__init__.py index c8db18a4c9..27314ed8ca 100644 --- a/lib/ansible/runner/__init__.py +++ b/lib/ansible/runner/__init__.py @@ -711,10 +711,18 @@ class Runner(object): actual_transport = inject.get('ansible_connection', self.transport) actual_private_key_file = inject.get('ansible_ssh_private_key_file', self.private_key_file) actual_private_key_file = template.template(self.basedir, actual_private_key_file, inject, fail_on_undefined=True) + self.sudo = utils.boolean(inject.get('ansible_sudo', self.sudo)) + self.sudo_user = inject.get('ansible_sudo_user', self.sudo_user) self.sudo_pass = inject.get('ansible_sudo_pass', self.sudo_pass) self.su = inject.get('ansible_su', self.su) self.su_pass = inject.get('ansible_su_pass', self.su_pass) + # select default root user in case self.sudo requested + # but no user specified; happens e.g. in host vars when + # just ansible_sudo=True is specified + if self.sudo and self.sudo_user is None: + self.sudo_user = 'root' + if actual_private_key_file is not None: actual_private_key_file = os.path.expanduser(actual_private_key_file)