Reputation: 1698
I bumped into Failed to lock apt for exclusive operation
issue:
https://github.com/geerlingguy/ansible-role-apache/issues/50
I posted a lot of details in GitHub.
I googled a lot of "Failed to lock apt for exclusive operation" Ansible complaints, but no simple answer. Any help?
Upvotes: 30
Views: 49248
Reputation: 1678
In my case, the silly problem was that I had the same host in my inventory, but under two different DNS names.
So, apt was legitimately locked in my case, because I had multiple ansible connections to the same host doing the same thing at the same time. Hah! Doh!
Upvotes: 1
Reputation: 1282
adding become: yes
to the task is solved my problem.
- name: Install dependencies
apt:
name: "{{ packages }}"
state: present
update_cache: yes
vars:
packages:
- package1
- package2
- package3
become: yes
Upvotes: 0
Reputation: 55
I faced the same error while using ansible to set up a ceph cluster. Just running the ansible-playbook without sudo did the trick for me!
Upvotes: 0
Reputation: 195
In my case, ansible was trying to lock /var/lib/apt/lists/
which did not exist.
The error ansible gave me was Failed to lock apt for exclusive operation: Failed to lock directory /var/lib/apt/lists/: E:Could not open lock file /var/lib/apt/lists/lock - open (2: No such file or directory)
Adding the lists directory fixed the issues:
- name: packages | ensure apt list dir exists
file:
path: /var/lib/apt/lists/
state: directory
mode: 0755
Upvotes: 6
Reputation: 101
https://serverfault.com/questions/716260/ansible-sudo-error-while-building-on-atlas
Check
ps aux | grep apt
output for suspicious processes.
Upvotes: -1
Reputation: 41
On my hosts file, I have some hosts that use a different user for sudo
than the one Ansible uses for SSH so I had ansible_user
and ansible_become_user
specified for every host in the inventory.
In those hosts where ansible_user
== ansible_become_user
I got Failed to lock apt for exclusive operation
.
The solution for me was to remove ansible_become_user
line for those cases where the user is the same in both parameters.
Upvotes: 1
Reputation: 397
I had remote_user: root
in my playbook, and this happened a lot. I couldn't figure out why, it was happening only on some roles but not others (but always at the same place.) I removed remote_user: root
and it stopped happening. I just use --become
and --become-user=root
. (Which I was using, but it still was randomly failing to get a lock for some reason.)
Upvotes: 2
Reputation: 121
I know this question has been answered a long time ago, but for me, the solution was different. The problem was the update_cache step. I had this with every install step, and somehow that caused the "apt failed lock error". the solution was adding the update_cache as a seperate step, like so:
- tasks:
- name: update apt list
apt:
update_cache: yes
Upvotes: 12
Reputation: 1607
The answer from @guaka was entirely correct for me. It lacks only one thing-- where to put become: yes
.
In the following, there are three places the become
might go. Only one of them is correct:
- name: setup nginx web server
#1 become: yes
include_role:
name: nginx
#2 become: yes
vars:
ansible_user: "{{ devops_ssh_user }}"
#3 become: yes
In position #2 you will get an unexpected parameter error, because become
is not a parameter for the include_role
module.
In position #3 the play will run, but will not execute as sudo.
Only in position #1 does become: yes
do any good. become
is a parameter for the task, not for the module. It is not a variable like ansible_user
. Just to be clear, here's a working configuration:
- name: setup nginx web server
become: yes
include_role:
name: nginx
vars:
ansible_user: "{{ devops_ssh_user }}"
Upvotes: 4
Reputation: 99
Running the following commands in the same sequence as below should resolve this issue :
sudo rm -rf /var/lib/apt/lists/*
sudo apt-get clean
sudo apt-get update
Upvotes: 9
Reputation: 21939
I'm also getting this error, while setting up a couple of new boxes. I'm connecting as root
, so I didn't think it was necessary, but it is:
become: yes
Now everything works as intended.
Upvotes: 48
Reputation: 1698
Ok, so there is nothing wrong with Ansible, SSH or the role. It is just that apt in Debian can get really confused and lock itself out. As I was using a home-brewed Docker VM, I only had to recreate my image and container to get APT working again.
Upvotes: 0