...

Failed to lock apt for exclusive operation: Easy Fix in 2 Ways (Ansible)

Rate this post

Issue Description

Encountering the “Fatal error: Failed to lock apt for exclusive operation” in Ansible often occurs due to simultaneous attempts to manage packages using APT by multiple processes. This issue arises when a package manager like APT is already in use or if another process holds the lock. Resolving this error involves identifying and terminating the process of holding the lock or waiting until the lock is released.

Understanding the causes behind this error and employing appropriate strategies to release the lock ensures a smooth and uninterrupted package management process using Ansible, preventing conflicts and enabling successful operations.


The complete fatal log for the above issue is as below:-
fatal: [vm-instance-01]: FAILED! => {“changed”: false, “msg”: “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 (13: Permission denied)”}

Solution for Failed to lock apt for exclusive operation

Solution for Failed to lock apt for exclusive operation
Solution for Failed to lock apt for exclusive operation
  1. Check for Ongoing Operations: Verify if any other package manager or process is using APT. Wait for it to finish, or manually terminate conflicting processes.
  2. Wait and Retry: Sometimes, the lock is temporary. Retry the Ansible playbook or command after a short interval.
  3. Manually Release the Lock: If the lock persists, check and remove the lock files using the following command
sudo rm /var/lib/apt/lists/lock
sudo rm /var/cache/apt/archives/lock
sudo rm /var/lib/dpkg/lock

Description: Whenever we want to update the apt cache(sudo apt update in Ubuntu or Debian) as below:-

---
- name: Install Redis
  hosts: redis_hosts
  become: true
  
  tasks:
    - name: Update package cache first
       apt:
         update_cache: yes
#####other tasks#######

or install a package with update_cache: yes

---
- name: Install Redis
  hosts: redis_hosts
  become: true
  
  tasks:
    - name: Install Redis
      apt:
        name: redis-server
        state: present
        update_cache: yes


sometimes, we face the above issue.

2 Reasons for “Failed to lock apt for exclusive operation”

  • apt lock is acquired by another process (doing some installation). It means apt is already running (can be due to manual execution by somebody or through ansible by somebody). This needs to be resolved (resolution already provided earlier, wait and retry, manually release the lock)
  • Ansible users do not have the required permissions(sudo). It means ansible_user or remote_user is not in the sudo group. Learn more about different types of users in Ansible

Diagnosis of the issue “Failed to lock apt for exclusive operation” through Ansible

Rerun your playbook with an increased verbosity level (-vv, -vvv, -vvvv) to get more details on the error. Once the issue is validated then you can follow the below solutions.

Solution: install packages as root user(become_user: root) and become: true in the playbook as below:-

---
- name: Install Redis
  hosts: redis_hosts
  become: true
  become_user: root

  tasks:
    - name: Update package cache first
       apt:
         update_cache: yes
#####other tasks#######

Or if you want to install a package with update_cache: true, then as below:-

---
- name: Install Redis
  hosts: redis_hosts
  become: true
  become_user: root
  
  tasks:
    - name: Install Redis
      apt:
        name: redis-server
        state: present
        update_cache: yes
#####other tasks#######

Conclusion

In conclusion, resolving the “Fatal error: Failed to lock apt for exclusive operation” issue in Ansible requires meticulous handling of package management conflicts. Addressing this error involves identifying and rectifying concurrent processes accessing the APT package manager or waiting until the lock is released.

By implementing the appropriate measures to release the lock, such as terminating conflicting processes or allowing the completion of ongoing operations, users ensure uninterrupted package management operations within Ansible. This proactive approach prevents conflicts and fosters smoother and more efficient execution of Ansible tasks, ensuring successful and error-free package management on the system.

Enjoy the post!!

You may find Getting Started with Ansible interesting, Please go through it.

Spread the love

Leave a Comment

Seraphinite AcceleratorOptimized by Seraphinite Accelerator
Turns on site high speed to be attractive for people and search engines.