Chrome repository setup failing on Linux Mint

Hello,
I have this code to add the Chrome repository:

- name: Add Chrome repository
  become: true
  ansible.builtin.apt_repository:
    repo: deb [arch=amd64 signed-by=/etc/apt/keyrings/linux_signing_key.pub] http://dl.google.com/linux/chrome/deb/ stable main
    state: present
    filename: google-chrome

It works fine on Debian 12 Bookworm but on a fresh Linux Mint install it gives this error:

fatal [localhost]: FAILED! = {“changed”: false, “msg”: “Failed to lock directory /var/lib/apt/lists/: E: Could not get lock /var/lib/apt/lists/lock. It is held by process 42190 (python3)”

The error message is self-explanatory, however, I want to mention that this is a fresh Mint installation with only Ansible installed. Encountering such an error is concerning because the user who will execute the process may lack the expertise to troubleshoot Linux. I want the automation to ensure that everything is configured correctly immediately after installation. Is there a way to address the lock issue directly through Ansible?

Thank you.

Is it locked because another instance of Ansible is currently using the apt process? If so, you could do something similar to this Stack Exchange Solution.

Excerpt

- command: /usr/bin/false
  retries: 3
  delay: 3
  register: result
  until: result.rc == 0

Basically, run the command, register the result, and loop till the result code is 0. Or as the comments in the link suggest until: result is not failed.

- name: Add Chrome repository
  become: true
  ansible.builtin.apt_repository:
    repo: deb [arch=amd64 signed-by=/etc/apt/keyrings/linux_signing_key.pub] http://dl.google.com/linux/chrome/deb/ stable main
    state: present
    filename: google-chrome
  retries: 3
  delay: 30
  register: result
  until: result is not failed

That will retry 3 times, waiting 30 seconds between each try. That would give other processes up to 2 minutes to finish and free up the lock.

1 Like