Bootstrap FreeKB - Ansible - lookup env home (users home directory)
Ansible - lookup env home (users home directory)

Updated:   |  Ansible articles

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/python3.12/site-packages/ansible/plugins/lookup/env.py.

The env or printenv command will display certain global variables, something like this.

[john.doe@server1 ~]$ env
HOSTNAME=server1
SHELL=/bin/bash
USER=john.doe
HOME=/home/john.doe

 

The lookup plugin can be used to get the value of the global variables. For example, to display the value of the HOME global variable.

 

debug:
  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.

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

 

Or the set_fact module can be used.

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

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


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