Bootstrap FreeKB - Ansible - lookup pipe hostname (control node)
Ansible - lookup pipe hostname (control node)

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 pipe plugin, the Python pipe.py script must be in your Ansible plugins lookup directory, such as /usr/lib/python3.12/site-packages/ansible/plugins/lookup/pipe.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

 

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

---
- hosts: localhost
  tasks:
  - name: return control node hostname
    ansible.builtin.debug:
      msg: "{{ lookup('ansible.builtin.pipe', 'hostname') }}"
...

 

Running this playbook should return something like this. In this example, server1 is the hostname of the control node.

TASK [return control node hostname]
ok: [server1.example.com] => {
    "msg: "server1"
}

 




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