Ansible - lookup env home (users home directory)

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 script must be in your Ansible plugins lookup directory, such as /usr/lib/python2.7/site-packages/ansible/plugins/lookup/

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

  msg: "{{ lookup('env', 'HOME') }}"


Running this play should return the following.

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


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

  home: "{{ lookup('env', 'HOME') }}"


Or the set_fact module can be used.

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


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 ca795 in the box below so that we can be sure you are a human.


Web design by yours truely - me, myself, and I   |   |