# ansible\_user undefined when used in become\_user:

**URL:** <https://forum.ansible.com/t/ansible-user-undefined-when-used-in-become-user/25648>\
**Category:** Ansible Project\
**Created:** [September 30, 2017, 7:32pm UTC](https://forum.ansible.com/t/ansible-user-undefined-when-used-in-become-user/25648 "2017-09-30T19:32:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jerome\_Wagner](https://avatars.discourse-cdn.com/v4/letter/j/76d3ee/32.png) [@Jerome\_Wagner](https://forum.ansible.com/u/Jerome_Wagner)\
**Post date:** [September 30, 2017, 7:32pm UTC](https://forum.ansible.com/t/ansible-user-undefined-when-used-in-become-user/25648/1 "2017-09-30T19:32:08Z")

</div>

Hello,

I thought that when ansible-playbook was used with the “-u” option, then ansible\_user was always defined as the “-u” user.

when running the following playbook,

- hosts: mybox  
gather\_facts: no  
tasks:
- name: Echo ansible\_user  
debug:  
msg: “hello {{ ansible\_user }}”  
become: yes  
become\_user: “{{ansible\_user}}”

I get the following error :

The field ‘become\_user’ has an invalid value, which includes an undefined variable. The error was: ‘ansible\_user’ is undefined\nexception type: \<class ‘ansible.errors.AnsibleUndefinedVariable’\>\nexception: ‘ansible\_user’ is undefined

Why isn’t ansible\_user available when the become\_user template is rendered ?  
Is this a bug ?

Thank you for your help

---

<div class="post-metadata">

**Author:** ![Jerome\_Wagner](https://avatars.discourse-cdn.com/v4/letter/j/76d3ee/32.png) [@Jerome\_Wagner](https://forum.ansible.com/u/Jerome_Wagner)\
**Post date:** [October 2, 2017, 8:46am UTC](https://forum.ansible.com/t/ansible-user-undefined-when-used-in-become-user/25648/2 "2017-10-02T08:46:15Z")

</div>

Let me add that I encounter the “ansible\_user” is undefined issue with both stable-2.3 and stable-2.4 branches of the code.

---

<div class="post-metadata">

**Author:** ![Jerome\_Wagner](https://avatars.discourse-cdn.com/v4/letter/j/76d3ee/32.png) [@Jerome\_Wagner](https://forum.ansible.com/u/Jerome_Wagner)\
**Post date:** [October 2, 2017, 1:24pm UTC](https://forum.ansible.com/t/ansible-user-undefined-when-used-in-become-user/25648/3 "2017-10-02T13:24:55Z")

</div>

After a bit of research, I ended up understanding that the problem comes from the sequence that creates the context.

during task execution, “ansible\_user” is considered a magic variable, that is created out of “remote\_user”.  
but it is added to the task variables context only after the connection info is built, templated and checked for undefined values.

I suspect this is a bug in the context creation ordering (because this was working with ansible 1.8 with sudo\_user: {{ansible\_ssh\_user}} )

I pushed a PR fixing this bug - [https://github.com/ansible/ansible/pull/31179](https://github.com/ansible/ansible/pull/31179) , simply moving the “template” check for undefined values after magic variables were added. This makes them available during template rendering.

I would appreciate if someone in the ansible team has time to review it and tell me if this has any chance of being merged.

Thanks,  
Jérôme

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [October 2, 2017, 5:52pm UTC](https://forum.ansible.com/t/ansible-user-undefined-when-used-in-become-user/25648/4 "2017-10-02T17:52:00Z")

</div>

Just to note, that if it 'worked', become\_user: "{{ansible\_user}}"  
would be a noop as the become system would ignore this by default as  
you are already THAT user.

Also, debug does not get many of these things set as it has no  
connection, it always executes 'locally' and ignores connection  
settings.
