I maintain a flock of EC2 servers with ansible. The servers are regularly updates and upgraded using the apt module.
When I manually tried to upgrade a server, I received the following message:
$ 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.
Is there a way to run sudo apt-get autoremove
with ansible?
Support for the
apt-get
option--auto-remove
is now built into Ansible'sapt
(optionautoremove
) as of version 2.1 Official documentation is at http://docs.ansible.com/ansible/apt_module.htmlThe merge happened here.
Note that
autoclean
is also available as of 2.4This simplified method requires one task only
You can do it with
command
(untested):However, I think it could be risky to run
autoremove
automatically. Because of system administration errors that you've made in the past (these could be in your ansible code), it's possible that a package that is needed can at some point be falsely detected as autoremovable, and this could stop the server from working. On the other hand, it's no big deal to leave unused packages on the system, and it's not very common unless you make a major change in the server's setup.Therefore, I would stay away from autoremoving packages without confirmation from a human.
This is a variation on the solution Antonis Christofides provided. It is tested and works for me. I avoided using ignore_errors in the check command. Otherwise it generally takes the same approach.
A variation that highlights the change in packages (first task will be appropriately colored green or yellow):
I like this simplified method, and I add some check and print message for me.
Thank you for cortopy and Dave James Miller.