Ansible APT任务:未能锁定用于独家操作的APT



我写了一本包括运行apt-get dist-upgrade的Ansible Playbook。我确保在剧本的顶部拥有become: truebecome_user: root,以便让Sudo访问升级。这在我的流VM上可以很好地工作,但是在我们的生产系统上运行(Ubuntu 16.04)时,我们会收到以下错误:

Failed to lock apt for exclusive operation

我们当前的解决方法是进入机器,然后手动运行sudo apt-get dist-upgrade。然后退出SSH会话,然后再次运行Ansible Playbook,并且可以正常运行。

我们的剧本中的其他任务需要Sudo访问并正常工作。失败只是APT命令。我们已经尝试重新启动机器,并用sudo: yes替换become: truebecome_user: root,无济于事。

关于如何解决这个问题的任何想法?我将在下面包含我们剧本的相关部分。

- become: true
  become_user: root
  name: Setup the mongo database servers
  hosts: sgmongo-{{ customer }}-ciadmin
  tasks:
    -
      name: Ensure OS is upgraded with all patches
      apt: upgrade=dist update_cache=yes

我通过称呼类似Ansible的shell脚本来做到这一点:

- script: ./files/bash_scripts/monitor_automatic_updates_status.sh

可讨论的脚本在这里:

#!/bin/bash  
#Debian automatically checks for updates on first boot. This ensures that has completed before continuing.
#If it hasn't finished in 10 minutes, the script will exit ungracefully.
timeout=$(($(date +%s) + 600))
while pgrep apt > /dev/null; do
    time=$(date +%s)
    if [[ $time -ge $timeout ]];
    then
        exit 1
    fi
    sleep 1
done;
exit 0

它只是不断检查apt是否在系统上运行。而且,当它最终完成运行时,可以允许继续进行。

似乎您已经中断了APT命令。尝试通过运行:

/var/lib/apt下删除APT锁定文件

sudo rm /var/lib/apt/lists/lock sudo rm /var/cache/apt/archives/lock sudo rm /var/lib/dpkg/lock

最新更新