MrDuk
MrDuk

Reputation: 18322

Way to get hostname in ansible while only executing on localhost?

Is there a built-in that allows for you to grab the hostname of the host you're on, excluding the inventory_hostname variable? What I want to do is execute plays on multiple hosts, but they have to be manually ran on each host (security policy, don't worry about it). However, as part of this, we use template variables that require the actual hostname of the localhost. Is it possible to acquire this with something other than inventory_hostname, since I assume that will just return localhost?

Upvotes: 1

Views: 4767

Answers (1)

nitzmahone
nitzmahone

Reputation: 13950

Sure- lots of ways to do that, but this one is friendly to local or remote running:

- command: hostname
  register: hostname

- set_fact: 
    actual_hostname: hostname.stdout

# now you can use {{ actual_hostname }} wherever and have 
# the value returned from running hostname on each target host.

Upvotes: 7

Related Questions