Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you prevent a dpkg installation task to notify a changed state when it runs for the second time?

Tags:

ansible

dpkg

deb

There isn't a module for installing .deb packages directly. When you have to run dpkg as a command, it always mark the installation task as one that has changed. I'd some trouble configuring it correctly, so I'm posting here as a public notebook.

Here is the task to install with dpkg:

- name: Install old python 
  command: dpkg -i {{ temp_dir }}/{{ item }}
  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

The files where uploaded to {{temp_dir}} in another task.

like image 379
neves Avatar asked Oct 01 '13 23:10

neves


2 Answers

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.

like image 123
neves Avatar answered Nov 18 '22 17:11

neves


In Ansible 1.6 (and newer), the apt module has a deb option:

- apt: deb=/tmp/mypackage.deb
like image 8
grahamrhay Avatar answered Nov 18 '22 15:11

grahamrhay