# Failed to lock apt for exclusive operation

**URL:** <https://forum.ansible.com/t/failed-to-lock-apt-for-exclusive-operation/32725>\
**Category:** Ansible Project\
**Created:** [April 18, 2020, 12:05pm UTC](https://forum.ansible.com/t/failed-to-lock-apt-for-exclusive-operation/32725 "2020-04-18T12:05:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![George\_James](https://avatars.discourse-cdn.com/v4/letter/g/5e9695/32.png) [@George\_James](https://forum.ansible.com/u/George_James)\
**Post date:** [April 18, 2020, 12:05pm UTC](https://forum.ansible.com/t/failed-to-lock-apt-for-exclusive-operation/32725/1 "2020-04-18T12:05:02Z")

</div>

I create a new user using ansible called `nginx`, `nginx` is part of the sudo group a, but when I try to do an apt install with this user I get the below error

```auto
: FAILED! => {"changed": false, "msg": "Failed to lock apt for exclusive operation"}

```

What is the cause of the error and how can it be fixed?

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [April 18, 2020, 12:41pm UTC](https://forum.ansible.com/t/failed-to-lock-apt-for-exclusive-operation/32725/2 "2020-04-18T12:41:48Z")

</div>

> I create a new user using ansible called `nginx`, `nginx` is part of the sudo group a, but when I try to do an apt  
> install with this user I get the below error
> 
> ```auto
> : FAILED! => {"changed": false, "msg": "Failed to lock apt for exclusive operation"}
> 
> ```
> 
> What is the cause of the error and how can it be fixed?

Please show your playbook.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![George\_James](https://avatars.discourse-cdn.com/v4/letter/g/5e9695/32.png) [@George\_James](https://forum.ansible.com/u/George_James)\
**Post date:** [April 18, 2020, 1:13pm UTC](https://forum.ansible.com/t/failed-to-lock-apt-for-exclusive-operation/32725/3 "2020-04-18T13:13:20Z")

</div>

Hi Stefan, I have attached my playbook below

`

- name: Install Nginx Ubuntu  
hosts: web  
remote\_user: “{{ NGINX\_USER }}”  
become: yes  
become\_method: sudo  
become\_user: “{{ NGINX\_USER }}”  
connection: ssh  
gather\_facts: no  
vars:  
NGINX\_VERSION: nginx-1.17.10  
NGINX\_SBIN\_PATH: /usr/sbin/  
NGINX\_ERROR\_LOG\_PATH: /var/log/nginx/error.log  
NGINX\_HTTP\_LOG\_PATH: /var/log/nginx/access.log  
NGINX\_PID\_PATH: /var/run/nginx.pid  
vars\_files:
- …/vars/global.yaml  
tasks:
- name: Check if Nginx Exists  
stat: path=/etc/init.d/nginx  
register: nginx\_status  
become: yes
- name: Stop nginx Service  
service: name=nginx state=stopped  
when: nginx\_status.stat.exists  
register: service\_stopped
- name: Make sure a systemd is not running  
systemd:  
state: stopped  
name: nginx
- name: Install aptitude using apt  
apt:  
name: aptitude  
state: latest  
update\_cache: yes  
force\_apt\_get: yes
- name: Update apt repo  
apt:  
update\_cache: yes  
cache\_valid\_time: 3600
- name: Install required system packages  
apt: name={{ item }} state=latest update\_cache=yes  
loop:  
[  
“build-essential”,  
“libpcre3”,  
“libpcre3-dev”,  
“zlib1g”,  
“zlib1g-dev”,  
“libssl-dev”,  
]

`

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [April 18, 2020, 1:24pm UTC](https://forum.ansible.com/t/failed-to-lock-apt-for-exclusive-operation/32725/4 "2020-04-18T13:24:36Z")

</div>

> Hi Stefan, I have attached my playbook below

Hello George,

you need to be superuser to install APT packages, but your become\_user is "{{ NGINX\_USER }}" which  
is probably not root.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![George\_James](https://avatars.discourse-cdn.com/v4/letter/g/5e9695/32.png) [@George\_James](https://forum.ansible.com/u/George_James)\
**Post date:** [April 18, 2020, 1:41pm UTC](https://forum.ansible.com/t/failed-to-lock-apt-for-exclusive-operation/32725/5 "2020-04-18T13:41:35Z")

</div>

Okay, sorry I am new to Ansible and all this is a bit confusing.

How do I set the super password, because now I change it to root, and even though I use the flag --ask-pass and pass in the password I now get the error `Missing sudo password`

`

- name: Install Nginx Ubuntu  
hosts: web  
remote\_user: “{{ NGINX\_USER }}”  
become: yes  
become\_method: sudo  
become\_user: root  
connection: ssh  
gather\_facts: no  
vars:  
NGINX\_VERSION: nginx-1.17.10  
NGINX\_SBIN\_PATH: /usr/sbin/  
NGINX\_ERROR\_LOG\_PATH: /var/log/nginx/error.log  
NGINX\_HTTP\_LOG\_PATH: /var/log/nginx/access.log  
NGINX\_PID\_PATH: /var/run/nginx.pid  
vars\_files:
- …/vars/global.yaml  
tasks:
- name: Check if Nginx Exists  
stat: path=/etc/init.d/nginx  
register: nginx\_status  
become: yes
- name: Stop nginx Service  
service: name=nginx state=stopped  
when: nginx\_status.stat.exists  
register: service\_stopped
- name: Make sure a systemd is not running  
systemd:  
state: stopped  
name: nginx
- name: Install aptitude using apt  
apt:  
name: aptitude  
state: latest  
update\_cache: yes  
force\_apt\_get: yes  
`

And I run with this playbook with the command

`ansible-playbook nginx_ubuntu_18_04/playbook.yaml -i hosts --ask-pass`

But I now get the error

`

TASK [Check if Nginx Exists] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [142.93.15.195]: FAILED! =\> {“msg”: “Missing sudo password”}

`

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [April 18, 2020, 1:59pm UTC](https://forum.ansible.com/t/failed-to-lock-apt-for-exclusive-operation/32725/6 "2020-04-18T13:59:13Z")

</div>

> Okay, sorry I am new to Ansible and all this is a bit confusing.
> 
> How do I set the super password, because now I change it to root, and even though I use the flag --ask-pass and pass in  
> the password I now get the error `Missing sudo password`

Hello George,

--ask-pass is for the SSH connection, --ask-become-pass for Sudo password.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [April 18, 2020, 2:56pm UTC](https://forum.ansible.com/t/failed-to-lock-apt-for-exclusive-operation/32725/7 "2020-04-18T14:56:00Z")

</div>

Not sure what a "super password" is - but it indicates that you might  
not have read the dedicated web page on Privilege escalation.  
I would suggest doing that though:  
[https://docs.ansible.com/ansible/latest/user\_guide/become.html](https://docs.ansible.com/ansible/latest/user_guide/become.html).
