Search code examples
ansible

How to access ansible controller hostname or ip?


I need to know the hostname or IP of the machine where ansible was invoked, not the ones from the inventory.

What is the easiest way to access this? Please note that I need to use this variable in tasks that executed on various hosts.

Note that all these are not valid answers: - ansible_hostname - inventory_hostname


Solution

  • Use lookups, they are always executed on localhost.

    For example: {{lookup("pipe","hostname")}}

    If you use this value extensively, better to do set_fact first, otherwise lookup command will be executed every time it is referenced.