# sudo\_user environment

**URL:** <https://forum.ansible.com/t/sudo-user-environment/13502>\
**Category:** Ansible Project\
**Created:** [September 18, 2013, 4:37pm UTC](https://forum.ansible.com/t/sudo-user-environment/13502 "2013-09-18T16:37:14Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Victor\_Castell](https://avatars.discourse-cdn.com/v4/letter/v/df705f/32.png) [@Victor\_Castell](https://forum.ansible.com/u/Victor_Castell)\
**Post date:** [September 18, 2013, 4:37pm UTC](https://forum.ansible.com/t/sudo-user-environment/13502/1 "2013-09-18T16:37:14Z")

</div>

Maybe a dumb question…

I detected sudo\_user is not loading the target user environment using the shell module.

When running it super verbose:

/bin/sh -c 'sudo -k && sudo -H -S -p “[sudo via ansible, key=wfaxytdcpsvwjtbbthcpmjnrnxmnxark] password: " -u deploy /bin/sh -c '”'“'/usr/bin/python /tmp/ansible-1379521443.11-262912739079441/command'”'"''

I can found this PR [https://github.com/ansible/ansible/commit/224b503b78a5e84cf9a28c03b3087f1ab3202768](https://github.com/ansible/ansible/commit/224b503b78a5e84cf9a28c03b3087f1ab3202768) but I don’t know if it’s something related.

Adding -i and running from the command line this outputs the correct env:

sudo -k && sudo -H -S -p "[sudo via ansible, key=wfaxytdcpsvwjtbbthcpmjnrnxmnxark] password: " -i -u deploy /bin/sh -c “env”

This is how it should work?

---

<div class="post-metadata">

**Author:** ![James\_Cammarata1](https://avatars.discourse-cdn.com/v4/letter/j/d26b3c/32.png) [@James\_Cammarata1](https://forum.ansible.com/u/James_Cammarata1)\
**Post date:** [September 18, 2013, 5:10pm UTC](https://forum.ansible.com/t/sudo-user-environment/13502/2 "2013-09-18T17:10:55Z")

</div>

Which version of ansible are you using, and could you show a bit of the playbook that you’re having a problem with? Also, what is the full command line you’re using?

---

<div class="post-metadata">

**Author:** ![Victor\_Castell](https://avatars.discourse-cdn.com/v4/letter/v/df705f/32.png) [@Victor\_Castell](https://forum.ansible.com/u/Victor_Castell)\
**Post date:** [September 18, 2013, 8:28pm UTC](https://forum.ansible.com/t/sudo-user-environment/13502/3 "2013-09-18T20:28:55Z")

</div>

I’m using 1.3 installed with pip.

A simple task like this:

- name: Precompile assets for {{ application }}  
shell: bundle exec rake assets:precompile chdir={{ release\_path }}

---

<div class="post-metadata">

**Author:** ![Victor\_Castell](https://avatars.discourse-cdn.com/v4/letter/v/df705f/32.png) [@Victor\_Castell](https://forum.ansible.com/u/Victor_Castell)\
**Post date:** [September 18, 2013, 8:34pm UTC](https://forum.ansible.com/t/sudo-user-environment/13502/4 "2013-09-18T20:34:41Z")

</div>

Sorry, clicked send too fast!

I’m using 1.3 installed with pip.

A simple task like this:

- name: Precompile assets for {{ application }}  
shell: bundle exec rake assets:precompile chdir={{ release\_path }}  
sudo\_user: {{ user }}

The full command is nothing more than:

sudo -k && sudo -H -S -p "[sudo via ansible, key=wfaxytdcpsvwjtbbthcpmjnrnxmnxark] password: " -i -u deploy /bin/sh -c “env”

Thanks

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [September 18, 2013, 8:38pm UTC](https://forum.ansible.com/t/sudo-user-environment/13502/5 "2013-09-18T20:38:37Z")

</div>

sudo\_user: "{{ user }}"

need to quote when starting a assignment with {

---

<div class="post-metadata">

**Author:** ![James\_Cammarata1](https://avatars.discourse-cdn.com/v4/letter/j/d26b3c/32.png) [@James\_Cammarata1](https://forum.ansible.com/u/James_Cammarata1)\
**Post date:** [September 18, 2013, 8:53pm UTC](https://forum.ansible.com/t/sudo-user-environment/13502/6 "2013-09-18T20:53:13Z")

</div>

Brian’s correct, is that the actual playbook or a sample, because I’d expect you to receive a YAML parsing error with what you have above.

Also, could you show the ansible command you’re running, and not the command as displayed from the verbose output? The -i flag for ansible commands is the inventory location, so I’m unclear as to why adding that would fix your issue unless your inventory files contain extra parameters. From your original output, it is running the command via sudo as the “deploy” user, it’s just doing it from within a second shell (the reason for all the extra quoting).

---

<div class="post-metadata">

**Author:** ![Victor\_Castell](https://avatars.discourse-cdn.com/v4/letter/v/df705f/32.png) [@Victor\_Castell](https://forum.ansible.com/u/Victor_Castell)\
**Post date:** [September 18, 2013, 9:23pm UTC](https://forum.ansible.com/t/sudo-user-environment/13502/7 "2013-09-18T21:23:18Z")

</div>

Sure it’s a sample I edited my current task for the message. I’m using a workaround just for the record:

vars:

- as\_deploy: ‘sudo -u {{ user }} -H bash -l -c’

tasks:

- shell: ‘{{ as\_deploy }} “cd {{ release\_path }} && bundle exec rake assets:precompile”’

---

<div class="post-metadata">

**Author:** ![James\_Cammarata1](https://avatars.discourse-cdn.com/v4/letter/j/d26b3c/32.png) [@James\_Cammarata1](https://forum.ansible.com/u/James_Cammarata1)\
**Post date:** [September 19, 2013, 10:51am UTC](https://forum.ansible.com/t/sudo-user-environment/13502/8 "2013-09-19T10:51:49Z")

</div>

Ahh, I see. Could you open a github issue for this? It may be that it is your PAM settings or something else that is requiring a full initial login via the sudo command, so it may be an option we need to enable.

Thanks!

---

<div class="post-metadata">

**Author:** ![Victor\_Castell](https://avatars.discourse-cdn.com/v4/letter/v/df705f/32.png) [@Victor\_Castell](https://forum.ansible.com/u/Victor_Castell)\
**Post date:** [September 19, 2013, 12:54pm UTC](https://forum.ansible.com/t/sudo-user-environment/13502/9 "2013-09-19T12:54:27Z")

</div>

Sure, thanks. Expect there all details.

El 19/09/2013, a les 12:51, James Cammarata \<[jcammarata@ansibleworks.com](mailto:jcammarata@ansibleworks.com)\> va escriure:
