After instance launched using ec2_asg , I wish to add newly created instance to ansible control machin’s host group.
I need to get the public ip and public dns of launched instances.
I wonder , if registering the ec2_asg output to a list , say asg_result and then getting the ip (item.public_ip) as asg_result.instances will work ?
Please let me know the best practices for the same .
Have you considered using the ec2 dynamic inventory plugin with tags?
Instead of adding the public ip's to the inventory, ansible can return a
list of them of the machines with a given tag. See [1] for the
documentation about it. It can also return the ip's based on other
information, such as security groups, etc.
My use case here, as soon as autoscale take place and new instance are UP…I have few more plays to start …like for an example …installing and configuring git …apache…other needed settings… so those role should identify the newly launched
instances. So need a mechanism for that.
Please note :All these play/roles is part of a same playbook.
If what you're executing in the servers has no side-effects and your
plays are properly configured to be indempotent, it shouldn't matter
if the machine is a fresh instance or an "old" one; running ansible in
each one of them should make them consistent between themselves (i.e.,
install everything needed in a fresh instance and keeping every other
instance unchanged).