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:
- hostname_variable only supports tags (not formatting or the ec2_* vars)
- 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!
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.
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.
https://github.com/ansible/ansible/issues/18359#issuecomment-267540204