Using ansible_user in task with delegate_to: localhost

26 views
Skip to first unread message

Simon Liddicott

unread,
Oct 14, 2020, 10:41:13 AM10/14/20
to Ansible Project
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.

---
- name: debug vars
  hosts: all
  gather_facts: no
  tasks:
  - debug: var=ansible_user
    delegate_to: localhost

  - debug: var=ansible_user

Simon Liddicott

unread,
Oct 14, 2020, 11:59:01 AM10/14/20
to Ansible Project
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 }}'

Reply all
Reply to author
Forward
0 new messages