The answer below still works, but newer ansible versions have the apt module. Mariusz Sawicki answer is the preferred one now. I've marked it as the accepted answer.
It'll work just with Ansible version 1.3, when changed_when
parameter was added. It is a little clumsy, maybe someone can improve the solution. I didn't find the documentation of this "register" object.
- name: Install old python
command: dpkg --skip-same-version -i {{ temp_dir }}/{{ item }}
register: dpkg_result
changed_when: "dpkg_result.stdout.startswith('Selecting')"
with_items:
- python2.4-minimal_2.4.6-6+precise1_i386.deb
- python2.4_2.4.6-6+{{ ubuntu_release }}1_i386.deb
- libpython2.4_2.4.6-6+{{ ubuntu_release }}1_i386.deb
- python2.4-dev_2.4.6-6+{{ ubuntu_release }}1_i386.deb
Here you can run the same task and it will just install the first time. After the first time, the packages won't be installed.
There were two modifications. One is the parameter --skip-same-version
for preventing dpkg to reinstall the software. The other is the register and changed_when attributes. The first time dpkg runs, it prints to stdout a string starting with 'Selecting' and a change is notified. Later it will have a different output. I've tried a more readable condition, but couldn't make it work with a more sofisticated condition that uses "not" or searches for a substring.