# Using ansible\_user in task with delegate\_to: localhost

**URL:** <https://forum.ansible.com/t/using-ansible-user-in-task-with-delegate-to-localhost/34033>\
**Category:** Ansible Project\
**Created:** [October 14, 2020, 2:41pm UTC](https://forum.ansible.com/t/using-ansible-user-in-task-with-delegate-to-localhost/34033 "2020-10-14T14:41:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Simon\_Liddicott](https://avatars.discourse-cdn.com/v4/letter/s/76d3ee/32.png) [@Simon\_Liddicott](https://forum.ansible.com/u/Simon_Liddicott)\
**Post date:** [October 14, 2020, 2:41pm UTC](https://forum.ansible.com/t/using-ansible-user-in-task-with-delegate-to-localhost/34033/1 "2020-10-14T14:41:13Z")

</div>

Hi

I’d expect the following two tasks to return the same output, however the first task returns the user that I was running ansible-playbook as and the second task returns the user set with -u.

I can understand why this might happen, however I need to access the -u username in a task that is delegated to localhost.

Any ideas how I could do that?

Thanks.

---

<div class="post-metadata">

**Author:** ![Simon\_Liddicott](https://avatars.discourse-cdn.com/v4/letter/s/76d3ee/32.png) [@Simon\_Liddicott](https://forum.ansible.com/u/Simon_Liddicott)\
**Post date:** [October 14, 2020, 3:59pm UTC](https://forum.ansible.com/t/using-ansible-user-in-task-with-delegate-to-localhost/34033/2 "2020-10-14T15:59:01Z")

</div>

I’ve worked around it for now using a set fact, but I think it would be helpful if there was a magic variable that always contained the defined remote user even when delegated elsewhere.

- name: Set defined login name  
set\_fact:  
defined\_user: ‘{{ ansible\_user }}’
