kpabijanskas
6/3/2015 - 11:02 AM

When a certain package is not provided in a repository, and you need install/update it using deb packages, it can be difficult to install/up

When a certain package is not provided in a repository, and you need install/update it using deb packages, it can be difficult to install/upgrade from the same playbook. Here's an example of how you can do this in debian, using PowerDNS Recursor. PowerDNS do not provide their own repositories, so if you need official packages, heres how you can manage it indepodently in one playbook. Please note, this fully supports downgrading too

hosts: localhost
sudo: yes
vars:
    - pdns_version: '3.7.2-1'

tasks:
  - name: 'Check if PowerDNS Recursor is installed'
    shell: "dpkg-query -W pdns-recursor"
    failed_when: no
    changed_when: no
    register: 'powerdns_package_installed'

  - name: 'If we need to install or upgrade, since we can't install from a url directly, get the package'
    get_url:
      url: "https://downloads.powerdns.com/releases/deb/pdns-recursor_{{ pdns_version }}_amd64.deb".
      dest: '/root/pdns.deb'
      force: 'yes' # Overwrite the previous file
    when: ( powerdns_package_installed.rc != 0 ) or ( powerdns_package_installed.stdout.find(pdnsrecursor['latest_version']) != 0 )

  - name: 'Remove old package before installing the update, since apt module from a deb file does not support latest parameter. We need to do this using dpkg rather than apt itself'
    shell: 'dpkg -P pdns-recursor'
    when: ( powerdns_package_installed.rc == 0 ) or ( powerdns_package_installed.stdout.find(pdnsrecursor['latest_version']) == -1 )

  - name: 'Install or upgrade PowerDNS Recursor'
    apt:
      deb: '/root/pdns.deb'
      state: 'present'
    when: ( powerdns_package_installed.rc == 0 ) or ( powerdns_package_installed.stdout.find(pdnsrecursor['latest_version']) == -1 )
    
# Then you can do your usual configure-start/enable service cycle. Even though we are purging the dpkg, since you are configuring resolver.conf via ansible, when you are upgrading it will not delete it, since that file was not created by dpkg itself