Ansible Script dpkg lock on aws launch ubuntu 16.04


I have a launch script (user data) that runs on startup in aws with an ubuntu 16.04 image, and the issue I’m having is that when it gets to the part where it runs an ansible playbook the playbook fails saying this basic error message Could not get lock /var/lib/dpkg/lock. Now when I log in and try to run the ansible script manually it works, but if I run it from the aws user data, it fails with the error.

This is the full error


I ran into the same lock issue. I found that ubuntu was installing some packages on first boot which cloud-init did not wait for.

I use the following script to check that the lock file is available for at least 15 seconds prior to trying to install anything.

The reason I prefer this vs sleep 5m because in an autoscale group the instance may be removed before it’s even provisioned.

Leave a Reply