Ansible - lookup env user (whoami)

The lookup plugin is always executed on the control node (that's your Ansible server), which is to say that the lookup plugin does not do something on your managed nodes (the target systems).

To use the env plugin, the Python env.py script must be in your Ansible plugins lookup directory, such as /usr/lib/python2.7/site-packages/ansible/plugins/lookup/env.py.

The lookup plugin can be used to identify the user that is invoking Ansible. The debug module can be used to output the results. Notice the double curley braces {{ ... }}. Jinja2 uses double curley braces for variables.

debug:
  msg: "{{ lookup('env', 'USER') }}"

 

Running this play should return the following.

TASK [debug]
ok: [localhost] => {
    "msg": "john.doe"
}

 

Or the vars plugin is used to store the users home directory in the "whoami" variable.

vars:
  whoami: "{{ lookup('env', 'USER') }}"

 

Or the set_fact module can be used.

- set_fact:
    home: "{{ lookup('env', 'USER') }}"


Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.





Please enter ac8b1 in the box below so that we can be sure you are a human.




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |