我接手了一个在 Ansible 上运行的项目,用于服务器配置和管理。我对 Ansible 还很陌生,但由于有很好的文档,我已经开始了解它了。 我仍然遇到错误,输出如下:
失败:[build](item = [u'software-properties-common',u'python-pycurl',u'openssh-server',u'ufw',u'unattended-upgrades',u'vim', u'curl', u'git', u'ntp']) => {"failed": true, "item": ["software-properties-common", "python-pycurl", "openssh-server", "ufw", "unattended-upgrades", "vim", "curl", "git", "ntp"], "msg": "无法锁定 apt 进行独占操作"}
剧本是用
sudo: yes
运行的,所以我不明白为什么会收到此错误(看起来像权限错误)。知道如何追踪这个吗?
- name: "Install very important packages"
apt: pkg={{ item }} update_cache=yes state=present
with_items:
- software-properties-common # for apt repository management
- python-pycurl # for apt repository management (Ansible support)
- openssh-server
- ufw
- unattended-upgrades
- vim
- curl
- git
- ntp
剧本:
- hosts: build.url.com
sudo: yes
roles:
- { role: postgresql, tags: postgresql }
- { role: ruby, tags: ruby }
- { role: build, tags: build }
我刚刚在新虚拟机上遇到了同样的问题。我尝试了很多方法,包括重试
apt
命令,但最终唯一的方法是删除无人值守的升级。
我在这里使用
raw
命令,因为此时虚拟机尚未安装 Python,所以我需要先安装它,但我需要一个 reliable apt
。
由于它是一个虚拟机,并且我通过将其重置为快照来测试剧本,系统日期已关闭,这迫使我使用
date -s
命令,以便在 apt
命令期间不会出现 SSL 证书问题。这date -s
触发了无人值守升级。
所以这个剧本的片段基本上是与在新系统中禁用无人值守升级相关的部分。它们是我在新系统上发出的第一个命令。
- name: Disable timers for unattended upgrade, so that none will be triggered by the `date -s` call.
raw: systemctl disable --now {{item}}
with_items:
- 'apt-daily.timer'
- 'apt-daily-upgrade.timer'
- name: Reload systemctl daemon to apply the new changes
raw: systemctl daemon-reload
# Syncing time is only relevant for testing, because of the VM's outdated date.
#- name: Sync time
# raw: date -s "{{ lookup('pipe', 'date') }}"
- name: Wait for any possibly running unattended upgrade to finish
raw: systemd-run --property="After=apt-daily.service apt-daily-upgrade.service" --wait /bin/true
- name: Purge unattended upgrades
raw: apt-get -y purge unattended-upgrades
- name: Update apt cache
raw: apt-get -y update
- name: If needed, install Python
raw: test -e /usr/bin/python || apt-get -y install python
任何其他情况都会导致
apt
命令因无人值守升级引起的锁定问题而随机失败。
这是配置 Ubuntu(也可能是其他一些发行版)时非常常见的情况。您尝试在后台运行自动更新时运行 Ansible(这是设置新机器后立即发生的情况)。由于 APT 使用信号量,Ansible 被踢出。
剧本没问题,最简单的验证方法是稍后运行它(自动更新过程完成后)。
为了永久解决问题,您可能需要:
我也遇到了这个错误,这解决了我的问题。
name: update apt package
become: yes
apt:
name: '*'
state: latest
update_cache: true
register: apt_get_status <<<<<<<<<<<<<<
until: apt_get_status is success <<<<<<<<
retries: 60
delay: 10
对于任何面临同样问题的人来说,这对我有用。
- name: wait until apt lock is released
shell: lsof -w /var/lib/apt/lists/lock | wc -l
register: lock
until: lock.stdout == "0"
retries: 10
delay: 10
- name: wait until apt lock_frontend is released
shell: lsof -w /var/lib/dpkg/lock-frontend | wc -l
register: lock_frontend
until: lock_frontend.stdout == "0"
retries: 10
delay: 10