Repository: ansible/ansible

Issue:
Cannot set become_flags globally since 2.4
<!---
Verify first that your issue/request is not already reported on GitHub.
Also test if the latest release, and master branch are affected too.
-->

##### ISSUE TYPE
<!--- Pick one below and delete the rest: -->
 - Bug Report

##### COMPONENT NAME
<!--- Name of the module/plugin/task/feature -->
become

##### ANSIBLE VERSION
<!--- Paste verbatim output from "ansible --version" between quotes below -->
Both stable:
```
ansible 2.4.0.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/home/pipanczy/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.9 (default, Jun 29 2016, 13:08:31) [GCC 4.9.2]
```
and devel:
```
ansible 2.5.0 (devel d74c871559) last updated 2017/09/20 15:19:48 (GMT +200)
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/home/piotrp/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /home/piotrp/github/fresh/ansible/lib/ansible
  executable location = /home/piotrp/github/fresh/ansible/bin/ansible
  python version = 2.7.12 (default, Nov 19 2016, 06:48:10) [GCC 5.4.0 20160609]
```

##### CONFIGURATION
```
$ ansible-config dump --only-changed
ANSIBLE_SSH_ARGS(/etc/ansible/ansible.cfg) = -o ControlMaster=auto -o ControlPersist=2h
ANSIBLE_SSH_CONTROL_PATH(/etc/ansible/ansible.cfg) = ~/.ssh/ansible-ssh-%%h-%%p-%%r
CACHE_PLUGIN(/etc/ansible/ansible.cfg) = memory
DEFAULT_ASK_PASS(/etc/ansible/ansible.cfg) = True
DEFAULT_BECOME(/etc/ansible/ansible.cfg) = True
DEFAULT_BECOME_ASK_PASS(/etc/ansible/ansible.cfg) = True
DEFAULT_BECOME_FLAGS(/etc/ansible/ansible.cfg) = -H -S -n -i
DEFAULT_FORKS(/etc/ansible/ansible.cfg) = 24
DEFAULT_GATHER_TIMEOUT(/etc/ansible/ansible.cfg) = 120
DEFAULT_LOG_PATH(/etc/ansible/ansible.cfg) = /var/log/ansible.log
DEFAULT_REMOTE_TMP(/etc/ansible/ansible.cfg) = /tmp/$USER/ansible/
DEFAULT_TIMEOUT(/etc/ansible/ansible.cfg) = 90
DISPLAY_SKIPPED_HOSTS(/etc/ansible/ansible.cfg) = False
HOST_KEY_CHECKING(/etc/ansible/ansible.cfg) = False
PERSISTENT_CONNECT_TIMEOUT(/etc/ansible/ansible.cfg) = 30
```
<!---
If using Ansible 2.4 or above, paste the results of "ansible-config dump --only-changed"

Otherwise, mention any settings you have changed/added/removed in ansible.cfg
(or using the ANSIBLE_* environment variables).

-->

##### OS / ENVIRONMENT
N/A
<!---
Mention the OS you are running Ansible from, and the OS you are
managing, or say "N/A" for anything that is not platform-specific.
Also mention the specific version of what you are trying to control,
e.g. if this is a network bug the version of firmware on the network device.
-->

##### SUMMARY
<!--- Explain the problem briefly -->
Since Ansible 2.4, it is impossible to set become_flags globally, both by ansible.cfg and by environment variable, as described in the [docs](http://docs.ansible.com/ansible/latest/config.html#default-become-flags).
The option sudo_flags still works, but displays a warning that this feature will be removed in version 2.8.

##### STEPS TO REPRODUCE
<!---
For bugs, show exactly how to reproduce the problem, using a minimal test-case.
For new features, show how the feature would be used.
-->
Set become_flags to non-default value in ansible.cfg or by the environment variable and run anything anywhere with high verbosity (-vvv).

<!--- Paste example playbooks or commands between quotes below -->
```
$ ANSIBLE_BECOME_FLAGS='-H -S -n -i' ansible -a true localhost -vvv
```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->
In 2.3, the output of the above command used to be:
```
[...]
<127.0.0.1> EXEC /bin/sh -c 'sudo -H -S  -i -p "[sudo via ansible, key=...
[...]
```

##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->
Since 2.4 it is:
<!--- Paste verbatim command output between quotes below -->
```
[...]
<127.0.0.1> EXEC /bin/sh -c 'sudo -H -S  -p "[sudo via ansible, key=...
[...]
```



Discussion:

Resolving patch:
diff --git a/lib/ansible/playbook/play_context.py b/lib/ansible/playbook/play_context.py
--- a/lib/ansible/playbook/play_context.py
+++ b/lib/ansible/playbook/play_context.py
@@ -528,19 +528,13 @@ def make_become_cmd(self, cmd, executable=None):
                 command = success_cmd
 
             # set executable to use for the privilege escalation method, with various overrides
-            exe = self.become_method
-            for myexe in (getattr(self, '%s_exe' % self.become_method, None), self.become_exe):
-                if myexe:
-                    exe = myexe
-                    break
+            exe = self.become_exe or getattr(self, '%s_exe' % self.become_method, self.become_method)
 
             # set flags to use for the privilege escalation method, with various overrides
-            flags = ''
-            for myflag in (getattr(self, '%s_flags' % self.become_method, None), self.become_flags):
-                if myflag is not None:
-                    flags = myflag
-                    break
+            flags = self.become_flags or getattr(self, '%s_flags' % self.become_method, '')
 
+            print(exe)
+            print(flags)
             if self.become_method == 'sudo':
                 # If we have a password, we run sudo with a randomly-generated
                 # prompt set using -p. Otherwise we run it with default -n, which makes


Test patch: