ansible/ansible — pull request #3820 by amenonsen (2013-08-11 15:01:02+00:00)
Title: Add a plugin to check if apt would install the given packages

I want to install a /usr/sbin/policy-rc.d file that does "exit 101" to suppress auto-starting daemons while installing some Debian packages using the apt module, and to remove the file afterwards. But obviously I want to do these things only if I'm actually going to be installing any packages. Removing the file is easy with a handler, but I couldn't find any sensible way to know beforehand whether to install policy-rc.d or not.

This module allows me to do something like this:

- apt_wouldinstall: pkg=$item
with_items:
- git
- vim
register: wanted
- name: Install policy-rc.d
copy: src=policy-rc.d dest=/usr/sbin/policy-rc.d mode=0755
when: wanted.changed

(Note that a shell: equivalent to the above test would be rather tedious to write. The dpkg/apt* CLIs do not make a simple ".is_installed" available without jumping through hoops, at least as far as I could tell.)

There are several open questions:

1. Should this functionality be added to the apt module somehow? (I tend to think not, since it would mean forcing two rather different usages into one module; on the other hand, you're always going to want to call it with the same list of packages)
2. If not, then should the module be called something else?
3. Should we support different target states, so that one could check if a list of modules were all installed or removed or purged etc.? (I don't have any need for this myself, but it's not exactly difficult to support)

I'm open to suggestions about any of this (especially the code itself). I guess this is more of a review request than a pull request, but then the code does work nicely for me, and I could find no better way of doing this (I did ask on IRC before writing the module).

(The correct thing to do would probably be to discuss it on the mailing list. Unfortunately, Google won't allow me to subscribe to the list with a non-primary address. If someone could send a list invite to ams@toroid.org, I would be grateful.)