I'm getting mixed results here provisioning new droplets with some new code. It seems like maybe Ansible is not waiting properly for the server to get an IP address. I have made several attempts to fix it with no luck and putting in some arbitrary wait time will not scale. Can anyone see if I made a mistake in the code that is causing this weird behavior. Right now, the playbook is designed to take the hostnames from the PROVISION group, spin up VMs, reply back with their IP addresses, and then append the new addresses to the DROPLETS group. Usually only part of the IP addresses make it into the DROPLETS group.
Here is the hosts file:
[DIGITALOCEAN]
localhost ansible_connection=local
[PROVISION]
TEST1
TEST2
TEST3
[DROPLETS]
TEST1 ansible_ssh_host=x.x.x.x
TEST2 ansible_ssh_host=y.y.y.y
See, this one didn't make it in... Not always this one... and they get unordered sometimes.
Here is the playbook:
---
- hosts: PROVISION
gather_facts: no
vars:
do_token: TOKEN
tasks:
- name: Create new droplet
digital_ocean: >
state=present
command=droplet
name={{ inventory_hostname }}
unique_name=yes
size_id=512mb
region_id=nyc3
image_id=ubuntu-18-04-x64
ssh_key_ids=KEY
api_token={{ do_token }}
wait=yes
wait_timeout=500
register: hostname
delegate_to: localhost
- name: Add host to Ansible config
lineinfile:
dest: "/etc/ansible/hosts"
insertafter: '^\[DROPLETS\]'
state: present
line: "{{ hostname.droplet.name }} ansible_ssh_host={{ hostname.droplet.ip_address }}"
delegate_to: localhost