Solved: How to wait for droplet agent to be installed to avoid apt lock issues

It took me several hours to figure out that the new droplet-agent is installed by apt on the first boot, which caused my Ansible playbook to fail due to not being able to get an apt lock.

The trick seems to be to wait until /etc/systemd/system/droplet-agent.service exists before trying to do anything with apt. The cloud install script grabs apt several times, so merely waiting to be able to use apt (as the apt-upgrade below will do) just results in a race condition where the cloud install script and mine were both working at the same time.

    - name: Wait until cloud service is installed before continuing
        path: /etc/systemd/system/droplet-agent.service

Here’s this too, though as mentioned above, it’s not sufficient

    - name: do an apt upgrade
        name: "*"
        state: latest
        update_cache: yes
        force_apt_get: True
        cache_valid_time: 3600
      become: yes
      register: apt_action
      retries: 100
      until: apt_action is success or ('Failed to lock apt for exclusive operation' not in apt_action.msg and '/var/lib/dpkg/lock' not in apt_action.msg)
      when: ansible_distribution == 'Ubuntu' or ansible_distribution == 'Ubuntu'

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

Sorry if that was unclear.

As I tried to explain, the stanza that waits until apt is available (my first attempt at solving this problem) is insufficient since the droplet-agent install script makes multiple apt calls. I am fairly certain that my “Wait until cloud service is installed before continuing” stanza is sufficient, but I included the other one anyway since I’m still using it in my playbook and didn’t test that it would work without it. And since race conditions may be involved, it might be hard to know for sure.

I already lost half a day on figuring out why scripts that had worked for years were now broken and didn’t have time for further testing.

Hi @pfaffman,

I can see you’ve marked the question as SOLVED. I can see you’ve found some solutions but mentioned them as not sufficient. Can you let us know what else you found so that other people that find your question know what to do.