Repository: ansible/ansible

Issue:
sudo_user in task should accept variables.
This isn't working for me on the latest HEAD:

> <staging> EXEC sudo -k && sudo -p "[sudo via ansible, key=x] password: " -u ${project_name} /bin/sh

```

---
- hosts: servers
  vars:
    - project_name: stavros
  gather_facts: false
  sudo: true

  tasks:
  - name: Create the SSH directory.
    file: state=directory path=${project_root}/home/.ssh/
    sudo_user: ${project_name}
```



Discussion:
Explain what "it" is

-- Michael

On Nov 23, 2012, at 10:17 AM, Stavros Korokithakis notifications@github.com wrote:

> This isn't working for me on the latest HEAD:
> 
> EXEC sudo -k && sudo -p "[sudo via ansible, key=x] password: " -u ${project_name} /bin/sh
> 
> ---
> - hosts: servers
>   vars:
>   - project_name: stavros
>     gather_facts: false
>     sudo: true
>   
>   tasks:
>   - name: Create the SSH directory.
>     file: state=directory path=${project_root}/home/.ssh/
>     sudo_user: ${project_name}
>     It didn't work when I tried putting it at the top either.
> 
> —
> Reply to this email directly or view it on GitHub.

Nm, I see your subject.  It is always good to mention both expected and actual behavior though...

-- Michael

On Nov 23, 2012, at 10:17 AM, Stavros Korokithakis notifications@github.com wrote:

> This isn't working for me on the latest HEAD:
> 
> EXEC sudo -k && sudo -p "[sudo via ansible, key=x] password: " -u ${project_name} /bin/sh
> 
> ---
> - hosts: servers
>   vars:
>   - project_name: stavros
>     gather_facts: false
>     sudo: true
>   
>   tasks:
>   - name: Create the SSH directory.
>     file: state=directory path=${project_root}/home/.ssh/
>     sudo_user: ${project_name}
>     It didn't work when I tried putting it at the top either.
> 
> —
> Reply to this email directly or view it on GitHub.

Nothing, it was a mispaste. I've edited it out.

ok, I've also edited your description to indicate it's a problem with the sudo_user in the task.

Run into this wanting to use sudo_user with with_items - same problem. 

I'll see if I can make any progress on resolving this. 

Hmm, wanting to run the same task from different sudo_users with sudo_user: $item set on a task?

Yeah, it's not really intended to work that way.   with_item only templates out the action line.

I fear this could be used for too many different crazy things if we are not careful, but if the patch ends up being clean I'll take it :)

We're running into this as well:

```
---

- name: install autoref application packages
  hosts:
    - dev
  user: root

  vars_files:
    - vars/shared_vars.yml
    - vars/${ENV_NAME}_vars.yml

  sudo: yes
  sudo_user: ${APP_USER}

  tasks:
     - name: create ${APP_USER} user
       user: name=${APP_USER} system=yes state=present
```

`<127.0.0.1> EXEC /bin/sh -c 'sudo -k && sudo -H -S -p "[sudo via ansible, key=notsureifthiskeyissecret] password: " -u ${APP_USER} /bin/sh -c '"'"' /usr/bin/python /tmp/ansible-1362095408.15-165816866783206/setup; rm -rf /tmp/ansible-1362095408.15-165816866783206/ >/dev/null 2>&1'"'"''`

APP_USER is defined in vars/${ENV_NAME}_vars.yml . I would expect that variable to be interpolated in sudo_user, but it is not. The first task works just fine, so the variable is defined.

It works as expected when the var is passed in using --extra-vars , as per fix in issue #998 

I wanted to write here to share our config because there seems to be some confusion on the use case.

At the very least, `sudo_user` should respect variables defined in the inventory.

Absolutely.   This part is in queue and is pretty easy to do.   Somewhat of a busy week for obvious reasons :)


Resolving patch:
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
@@ -94,7 +94,7 @@ def __init__(self, play, ds, module_vars=None, additional_conditions=None):
         self.args         = ds.get('args', {})
 
         if self.sudo:
-            self.sudo_user    = ds.get('sudo_user', play.sudo_user)
+            self.sudo_user    = utils.template(play.basedir, ds.get('sudo_user', play.sudo_user), play.vars)
             self.sudo_pass    = ds.get('sudo_pass', play.playbook.sudo_pass)
         else:
             self.sudo_user    = None


Test patch: