As an Ansible newbie, I'm installing some packages on multiple Ubuntu machines using a loop, here's my playbook:
---
- name: Install Software
hosts: all
become: true
tasks:
- name: Update package cache
package: # Use the package module
name: "{{ item }}"
state: present
with_items: # Loop over the package list
- vim
- tree
- figlet
ignore_errors: yes # Ignore errors if packages fail to install
retries: 3 # Number of retries if task fails
delay: 10 # Delay between retries in seconds
At the first run the playbook fails on one of the hosts, here's the error message:
failed: [serv01] (item=tree) => {"ansible_loop_var": "item", "cache_update_time": 1716373387, "cache_updated": false, "changed": false, "item": "tree", "msg": "'/usr/bin/apt-get -y -o "Dpkg::Options::=--force-confdef" -o "Dpkg::Options::=--force-confold" install 'tree=2.0.2-1'' failed: E: Could not get lock /var/lib/dpkg/lock-frontend. It is held by process 1567 (apt-get)\nE: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), is another process using it?\n", "rc": 100, "stderr": "E: Could not get lock /var/lib/dpkg/lock-frontend. It is held by process 1567 (apt-get)\nE: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), is another process using it?\n", "stderr_lines": ["E: Could not get lock /var/lib/dpkg/lock-frontend. It is held by process 1567 (apt-get)", "E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), is another process using it?"], "stdout": "", "stdout_lines": []} failed: [serv01] (item=figlet) => {"ansible_loop_var": "item", "cache_update_time": 1716373387, "cache_updated": false, "changed": false, "item": "figlet", "msg": "'/usr/bin/apt-get -y -o "Dpkg::Options::=--force-confdef" -o "Dpkg::Options::=--force-confold" install 'figlet=2.2.5-3'' failed: E: Could not get lock /var/lib/dpkg/lock-frontend. It is held by process 1567 (apt-get)\nE: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), is another process using it?\n", "rc": 100, "stderr": "E: Could not get lock /var/lib/dpkg/lock-frontend. It is held by process 1567 (apt-get)\nE: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), is another process using it?\n", "stderr_lines": ["E: Could not get lock /var/lib/dpkg/lock-frontend. It is held by process 1567 (apt-get)", "E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), is another process using it?"], "stdout": "", "stdout_lines": []} ...ignoring
When I run the playbook again, it is successful on all hosts.
How do I tell Ansible to wait til the lock is released? Or is it possible to install packages concurrently?
killall apt apt-getto kill all processes from apt and apt get. Most of the time this fixes it