TL;DR: When I create a instance the ec2_* variables are undefined.
Hello,
I’m using ansible to create instances and using the ec2_tag_Name to set up the machine’s hostname. The problem is, on the first run the playbook crashes saying that the ‘ec2_tag_Name’ variable is undefinded, which is odd, since it should be automagicly defined by the ec2.py script. I’ve tried to run [$ ./ec2.py --refresh-cache]after the machine’s creation but with no luck.
My current workaround is to simply run the playbook twice, the variable is defined on the second run.
Here is a example playbook with this behaviour.
Thank you!
For starters, you have a typo in your add_host task: publid_dns_name should be public_dns_name.
When you run your playbook the first time, there is no inventory. If you want to be able to access your new instances from the same playbook that creates them, you’ll need to reference them by either the registered variable (ec2) or the group you’re adding them to (my_cool_instance_group).
Dynamic inventory isn’t dynamically modified as you create instances. It simply populates your inventory when the playbook starts. That’s the inventory you have to work with until the playbook is complete and you refresh the cache.
I get it now, I need to use the dynamic inventory in order to use it’s variables.
Thanks for the reply!