Is it possible to set the "hostname" in Ansible's console output?

72 views
Skip to first unread message

Ryan Fisher

unread,
Nov 8, 2017, 8:38:32 PM11/8/17
to Ansible Project
I'm creating a dynamic inventory using ec2.py (which is working great, thanks!). One minor annoyance I'm running into is that the console output that shows changes is labeled with the instances private IPs (as set by hostname_variable or [vpc_]destination_variable). It would be nice to label these instances based on tags or the ec2 vars (or combination). From what I can tell, that isn't possible because:

  1. hostname_variable only supports tags (not formatting or the ec2_* vars)
  2. destination_format overrides vpc_destination_variable
I can modify ec2.py to add the functionality I'm looking for to hostname_variable, just wondering if there was a supported method for doing this already.

Thanks!

Ryan Fisher

unread,
Nov 9, 2017, 5:03:09 PM11/9/17
to Ansible Project
After further evaluation, I've found a way to achieve my desired result. However, look at the code makes me think that it's not exactly intended...

What I've done:

destination_variable = private_ip_address
vpc_destination_variable = private_ip_address
destination_format = {0}@{1}
destination_format_tags = Name,private_ip_address

This lets me set the ansible_hostname (or maybe it's inventory_hostname) to the label I want (instance_name@ip). However, ansible_ssh_host is then set to that same label, which isn't the correct SSH connection IP. So in my inventory, I reset ansible_ssh_host like so:

[all:vars]
ansible_ssh_host={{ ec2_private_ip_address }}

Willing to be this will break something at some point if for no reason but that when destination_format is used it doesn't run the name through the to_safe method in ec2.py.

Ryan Fisher

unread,
Nov 9, 2017, 5:40:29 PM11/9/17
to Ansible Project
Seems like this is actually a known issue for dynamic inventories. My workaround will break when the depreciated ansible_ssh_host var is no longer supported.

Note, it is possible to override ansible_host from the command line with --extra-vars which may end up being the better workaround in my case.



On Wednesday, November 8, 2017 at 5:38:32 PM UTC-8, Ryan Fisher wrote:

Ryan Fisher

unread,
Nov 10, 2017, 12:51:56 PM11/10/17
to Ansible Project
And I'm wrong...


Reply all
Reply to author
Forward
0 new messages