Apt

使用 ansible 執行 apt-get autoremove

  • July 6, 2017

我用 ansible 維護了一群 EC2 伺服器。伺服器使用apt 模組定期更新和升級。

當我手動嘗試升級伺服器時,我收到以下消息:

$ sudo apt-get upgrade
Reading package lists... Done
Building dependency tree
Reading state information... Done
Calculating upgrade... Done
The following packages were automatically installed and are no longer required:
 linux-headers-3.13.0-29 linux-headers-3.13.0-29-generic
 linux-headers-3.13.0-32 linux-headers-3.13.0-32-generic
 linux-image-3.13.0-29-generic linux-image-3.13.0-32-generic
Use 'apt-get autoremove' to remove them.
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

有沒有辦法sudo apt-get autoremove用ansible執行?

apt-get自 2.1 版起,對選項的支持--auto-remove現已內置到 Ansible 的apt(選項autoremove)中 官方文件位於http://docs.ansible.com/ansible/apt_module.html

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

合併發生在這裡

請注意,autoclean從 2.4 開始也可以使用

這種簡化的方法只需要一項任務

 - name: Autoremove unused packages
   command: apt-get -y autoremove
   register: autoremove_output
   changed_when: "'The following packages will be REMOVED' in autoremove_output.stdout"

引用自:https://serverfault.com/questions/644082