How to display ansible_host in console output even if an alias is defined at inventory level

Hi everyone!

We currently use ansible_host variable and define an alias for the hosts we use as follows:

Inventory file

[my_jboss_servers]
jboss_server1 ansible_host=myhostnameABC
jboss_server2 ansible_host=myhostnameEFG
jboss_server3 ansible_host=myhostnameHIJ
jboss_server4 ansible_host=myhostnameKLM

The reason for this is because hosts might change from time to time and since we have host-specific variables and files, it’s just easier to keep using the same alias so we don’t have to change any file names in our repository however this also has a disadvantage as it might get confusing if we start removing and adding hosts since the ansible-playbook console output displays only the alias, not the actual host, see:

$ ansible-playbook myplaybook.yml -i inventory/my_inventory

PLAY [all] *********************************************************************

TASK [Gathering Facts] *********************************************************
ok: [jboss_server1]
ok: [jboss_server2]
ok: [jboss_server3]
ok: [jboss_server4]

TASK [Task 1 : Create home directory if it does not exist] *******************
ok: [jboss_server1]
ok: [jboss_server2]
ok: [jboss_server3]
ok: [jboss_server4]

My questions are:

  1. Is there any ansible best-practice on how to handle this scenario?
  2. Is there any way I can display the actual hostname, not the alias in the console output?

Thanks in advance for your help / guidance on this!

I think it's best to reserve ansible_host as the IP address as that remains constant. You can declare friendly names first that way:

jboss_1 ansible_host=10.0.0.10

Going by your method, your inventory will require a lot of upkeep because if the hostname changes, the inventory will break.

Thanks, Varun!

Got it, but even so, imagine that we suddenly remove one of the so the inventory would initially look like this:

Inventory file

[my_jboss_servers]
jboss_server1 ansible_host=IP1
jboss_server3 ansible_host=IP3
jboss_server4 ansible_host=IP4

And if I run the playbook, I’ll see something like this:

$ ansible-playbook myplaybook.yml -i inventory/my_inventory

PLAY [all] *********************************************************************

TASK [Gathering Facts] *********************************************************
ok: [jboss_server1]
ok: [jboss_server3]
ok: [jboss_server4]

TASK [Task 1 : Create home directory if it does not exist] *******************
ok: [jboss_server1]
ok: [jboss_server3]
ok: [jboss_server4]

Which I think might be confusing, initially as the first question would be: “what abour jboss_server2”? so I guess I’m trying to find a way to balance both, maintenance and clarity

Regards.

Hey

Why not put a debug statement at the beginning of your play that outputs the ansible_hostname? That way you can a one-to-one mapping in your output and you can reference that to figure out what system each refers to? Just a thought.