How to wait for user_data script to run when starting EC2 instances with ansible?
Solution 1:
Here is the answer to your question "How to wait for user data script to run when starting EC2 instances with ansible?":
- raw: test -f /var/lib/cloud/instance/boot-finished
retries: 20
register: cmd_res
changed_when: false
until: cmd_res | success
This will connect via ssh and succeed when boot-finished
is present, which is created after cloud-init
module on the host completes all modules (including user_data script).
But I'd suggest to create AMI with Python preinstalled, or use raw
or script
Ansible module to check/install Python (these modules don't require Python).
Solution 2:
It turns out you can use cloud-init
command for that, namely:
cloud-init status --wait
So you should add the following snipped to your playbook:
- name: Wait for cloud-init / user-data to finish
command: cloud-init status --wait
changed_when: false
It looks a bit cleaner compared to the first answer.