运行apt-get autoremove with 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 with sudo apt-get autoremove

apt-get选项的支持--auto-remove现在已经内置到Ansible的apt (选项autoremove )中,版本为2.1。官方文档位于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" 

你可以用command (未经testing)来完成:

  - name: Check if anything needs autoremoving shell: apt-get -y --dry-run autoremove | grep -q "0 to remove" register: check_autoremove ignore_errors: True changed_when: False always_run: True - name: Autoremove unused packages command: apt-get -y autoremove when: "check_autoremove.rc != 0" 

不过,我认为自动运行autoremove可能会有风险。 由于您以前所做的系统pipe理错误(这些错误可能存在于您的代码中),因此可能会在某个时候将所需的软件包错误地检测为可自动移除,从而导致服务器无法正常工作。 另一方面,将未使用的软件包留在系统上并不是什么大事,除非您对服务器的设置进行重大更改,否则这种做法并不常见。

因此,如果没有人的确认,我会远离自动移除软件包。

这是Antonis Christofides提供的解决scheme的变体。 这是testing,为我工作。 我避免在check命令中使用ignore_errors。 否则它通常采用相同的方法。

 - name: Check if packages need to be autoremoved command: apt-get --dry-run autoremove register: check_autoremove changed_when: False - name: Autoremove unused packages command: apt-get -y autoremove when: "'packages will be REMOVED' in check_autoremove.stdout" 

一个变化,突出包装的变化(第一项任务将被适当的绿色或黄色):

  - name: check if packages need to be autoremoved shell: apt-get --dry-run autoremove | grep "to remove" | sed "s/^[0-9]\+ upgraded, [0-9]\+ newly installed, \([0-9]\+\) to remove and [0-9]\+ not upgraded\.$/\1/" register: check_autoremove changed_when: check_autoremove.stdout != "0" - name: autoremove unused packages command: apt-get -y autoremove when: check_autoremove.changed 

我喜欢这个简化的方法 ,并且为我添加一些检查和打印信息。

 #!/usr/bin/env ansible-playbook --- - name: Autoremove 'apt' package for Debian, Ubuntu hosts: all pre_tasks: - name: check storage space - before shell: df -h register: check_storage_space_before - name: print storage space debug: msg: "{{ check_storage_space_before.stdout_lines }}" - name: apt autoremove check command: apt-get -y --dry-run autoremove register: apt_autoremove_output - name: print apt autoremove packages debug: msg: "{{ apt_autoremove_output.stdout_lines }}" tasks: - name: autoremove unused packages become: yes command: apt-get -y autoremove changed_when: "'The following packages will be REMOVED' in apt_autoremove_output.stdout" post_tasks: - name: check storage space - after shell: df -h register: check_storage_space_after - name: print storage space debug: msg: "{{ check_storage_space_after.stdout_lines }}" # vim: ft=ansible : 

感谢您的cortopy和戴夫·詹姆斯·米勒 。