diff --git a/tasks/ec2/inithost.yml b/tasks/ec2/inithost.yml index 46ea5fbeaa8db4e4b507d4ae7c7b8aaef15896e9..624bcdadd820bc1242c74c99b06f734091464df7 100644 --- a/tasks/ec2/inithost.yml +++ b/tasks/ec2/inithost.yml @@ -26,18 +26,22 @@ state: running wait: yes -- name: "Add new instance(s) to the inventory" - add_host: - hostname="{{ host }}" - static_ipv4="{{ item.public_ip }}" - groups="{{ initgroups }}" - with_items: '{{ ec2.instances }}' +- block: + + - name: "Associate an elastic IP with the new instance" + ec2_eip: + device_id: "{{ item }}" + public_ip: '{{ inithostip }}' + region: "{{ ec2_region }}" + state: present + with_items: '{{ ec2.instance_ids }}' + + - include: inithostip.yml hostip='{{ inithostip }}' + + when: inithostip is defined -- name: "Waiting for SSH service becoming available" - wait_for: - host="{{ item.public_ip }}" - port=22 - delay=10 - timeout=120 - state=present +- include: inithostip.yml hostip='{{ ec2instance.public_ip }}' with_items: '{{ ec2.instances }}' + loop_control: + loop_var: ec2instance + when: inithostip is not defined diff --git a/tasks/ec2/inithostip.yml b/tasks/ec2/inithostip.yml new file mode 100644 index 0000000000000000000000000000000000000000..7e57143f63979f98cc6de73d949ea4003bcf7c61 --- /dev/null +++ b/tasks/ec2/inithostip.yml @@ -0,0 +1,16 @@ +--- +# file: ec2/inithostip.yml + +- name: "Add new instance(s) to the inventory" + add_host: + hostname="{{ host }}" + static_ipv4="{{ hostip }}" + groups="{{ initgroups }}" + +- name: "Waiting for SSH service becoming available" + wait_for: + host="{{ hostip }}" + port=22 + delay=10 + timeout=120 + state=present