如何使用Ansible的apt模块执行相当于sudo apt-get update && sudo apt-get upgrade -y的工作?

时间:2019-08-29 16:12:54

标签: ubuntu ansible apt ubuntu-server

我在配置Ansible的apt模块时遇到困难,因此它无法完全等效于以下命令行:

sudo apt-get update && sudo apt-get upgrade -y

到目前为止,我最大的尝试是:

- name: update and upgrade packages
  apt:
    force_apt_get: yes
    name: "*"
    only_upgrade: yes
    state: latest
    update_cache: yes

但是,这将继续安装我以前使用sudo apt-get remove [name]删除的软件包。

我如何告诉Ansible仅升级那些已经安装的软件包?

我正在将Ansible 2.8.4与Python 3.7.4结合使用。远程是Ubuntu Server 18.4.3。手动运行apt-get命令时,一切正常。

1 个答案:

答案 0 :(得分:0)

我必须建议您不要在剧本中使用force_apt_get: yes。由于Ansible具有为此目的而设计的apt模块。我已经使用下面的apt完成了此操作。

在剧本中,您可以像这样进行更新和升级:

- name: Update and upgrade apt packages
  become: true
  apt:
    upgrade: yes
    update_cache: yes

此外,您还可以删除不再需要的依赖项。

- name: Remove useless packages from the cache
  apt:
     autoclean: yes

- name: Remove dependencies that are no longer required
  apt:
     autoremove: yes