Repository: ansible/ansible

Issue:
Traceback from new elevation code in 1.9rc1
##### Issue Type:

Bug
##### Ansible Version:

Ansible
- devel 57aa404
- 1.9 rc1 +  f0bdf01 54 minutes ago Brian Coca fixed bad paren in connection plugin 
##### Environment:

n/a
##### Summary:

Traceback coming from privilege elevation patch
##### Steps To Reproduce:

```
$ cat play.yml 

---
- hosts: all
  gather_facts: false
  connection: local
  tasks: 
   - debug:
     sudo_user: serge
```
##### Results:

```
$ ansible-playbook play.yml 
Traceback (most recent call last):
  File "/usr/bin/ansible-playbook", line 323, in <module>
    sys.exit(main(sys.argv[1:]))
  File "/usr/bin/ansible-playbook", line 263, in main
    pb.run()
  File "/usr/lib/pymodules/python2.7/ansible/playbook/__init__.py", line 310, in run
    play = Play(self, play_ds, play_basedir, vault_password=self.vault_password)
  File "/usr/lib/pymodules/python2.7/ansible/playbook/play.py", line 187, in __init__
    self._tasks      = self._load_tasks(self._ds.get('tasks', []), load_vars)
  File "/usr/lib/pymodules/python2.7/ansible/playbook/play.py", line 588, in _load_tasks
    included_become_vars['become'] = x['sudo']
KeyError: 'sudo'
```



Discussion:

Resolving patch:
diff --git a/lib/ansible/playbook/play.py b/lib/ansible/playbook/play.py
--- a/lib/ansible/playbook/play.py
+++ b/lib/ansible/playbook/play.py
@@ -583,29 +583,6 @@ def _load_tasks(self, tasks, vars=None, role_params=None, default_vars=None, bec
                     included_become_vars[k] = become_vars[k]
                     x[k] = become_vars[k]
 
-            ## backwards compat with old sudo/su directives
-            if 'sudo' in x or 'sudo_user' in x:
-                included_become_vars['become'] = x['sudo']
-                x['become'] = x['sudo']
-                x['become_method'] = 'sudo'
-                del x['sudo']
-
-                if x.get('sudo_user', False):
-                    included_become_vars['become_user'] = x['sudo_user']
-                    x['become_user'] = x['sudo_user']
-                    del x['sudo_user']
-
-            elif 'su' in x or 'su_user' in x:
-                included_become_vars['become'] = x['su']
-                x['become'] = x['su']
-                x['become_method'] = 'su'
-                del x['su']
-
-                if x.get('su_user', False):
-                    included_become_vars['become_user'] = x['su_user']
-                    x['become_user'] = x['su_user']
-                    del x['su_user']
-
             if 'meta' in x:
                 if x['meta'] == 'flush_handlers':
                     results.append(Task(self, x))
diff --git a/lib/ansible/playbook/task.py b/lib/ansible/playbook/task.py
--- a/lib/ansible/playbook/task.py
+++ b/lib/ansible/playbook/task.py
@@ -173,19 +173,34 @@ def __init__(self, play, ds, module_vars=None, play_vars=None, play_file_vars=No
 
         # set only if passed in current task data
         if 'sudo' in ds or 'sudo_user' in ds:
-            self.become=ds['sudo']
             self.become_method='sudo'
+
+            if 'sudo' in ds:
+                self.become=ds['sudo']
+                del ds['sudo']
+            else:
+                self.become=True
             if 'sudo_user' in ds:
                 self.become_user = ds['sudo_user']
+                del ds['sudo_user']
             if 'sudo_pass' in ds:
                 self.become_pass = ds['sudo_pass']
-        if 'su' in ds or 'su_user' in ds:
-            self.become=ds['su']
+                del ds['sudo_pass']
+
+        elif 'su' in ds or 'su_user' in ds:
             self.become_method='su'
+
+            if 'su' in ds:
+                self.become=ds['su']
+            else:
+                self.become=True
+                del ds['su']
             if 'su_user' in ds:
                 self.become_user = ds['su_user']
+                del ds['su_user']
             if 'su_pass' in ds:
                 self.become_pass = ds['su_pass']
+                del ds['su_pass']
 
         # Both are defined
         if ('action' in ds) and ('local_action' in ds):
diff --git a/lib/ansible/runner/action_plugins/template.py b/lib/ansible/runner/action_plugins/template.py
--- a/lib/ansible/runner/action_plugins/template.py
+++ b/lib/ansible/runner/action_plugins/template.py
@@ -133,7 +133,7 @@ def run(self, conn, tmp, module_name, module_args, inject, complex_args=None, **
             xfered = self.runner._transfer_str(conn, tmp, 'source', resultant)
 
             # fix file permissions when the copy is done as a different user
-            if self.runner.become and self.runner.become_user != 'root' or self.runner.su and self.runner.su_user != 'root':
+            if self.runner.become and self.runner.become_user != 'root':
                 self.runner._remote_chmod(conn, 'a+r', xfered, tmp)
 
             # run the copy module


Test patch: