How to Upgrade a Single Package with Ansible

ansibledebian

I'm trying to write an ansible playbook which only upgrades the linux-generic package but cannot find the magic incantation to get it to actually work. With this play, I get the error parameters are mutually exclusive: deb|package|upgrade which google suggests is due to upgrade: full is not meant for a single package:

  - name: Check if linux-generic is installed
    command: dpkg -l linux-generic
    register: pkg_chk

  - name: update kernel if installed
    when: pkg_chk.rc == 0
    apt:
      update_cache: yes
      autoremove  : yes
      autoclean   : no
      upgrade     : full
      dpkg_options: 'force-confold,force-confdef'
      name        : linux-generic

Removing upgrade: full runs the play…

TASK [update kernel if installed] **********************************************************
ok: [vivaldi.fammed.wisc.edu]

But, when I login to the machine and run aptitude full-upgrade it shows ansible didn't do anything:

# aptitude full-upgrade
The following NEW packages will be installed:
  linux-headers-4.4.0-203{a} linux-headers-4.4.0-203-generic{a}
  linux-image-4.4.0-203-generic{a} linux-modules-4.4.0-203-generic{a}
  linux-modules-extra-4.4.0-203-generic{a}

Ok, so I thought I'd just throw the command: module at it and use bare apt commands but there is some issue with the command string that I have not escaped properly.

  - name: upgrade linux-generic
    when: pkg_chk.rc == 0
      command: apt-get install -y --only-upgrade -o Dpkg::Options::='force-confdef' -o Dpkg::Options::='force-confold' linux-generic
      register: install_chk

  - name: autoremove linux-generic
    when: install_chk.rc == 0
      command: apt-get -y autoremove -o Dpkg::Options::='force-confdef' -o Dpkg::Options::='force-confold'
      register: auto_chk

which yields this error:

Syntax Error while loading YAML.
  mapping values are not allowed in this context
    The error appears to be in '/usr/src/ansible/tests/upgrade-kernel-only.yml': line 24, column 12, but may
    be elsewhere in the file depending on the exact syntax problem.
    
    The offending line appears to be:
    
        when: pkg_chk.rc == 0
          command: apt-get install -y --only-upgrade -o Dpkg::Options::='force-confdef' -o Dpkg::Options::='force-confold' linux-generic
               ^ here

I've tried escaping the : = characters, as well as replacing double-quotes and single quotes. Anyone know how to get this working?

Best Answer

Is there any specific reason why would you not consider the ansible package module e.g.

- name: Install linux-generic
  package:
    name: linux-generic
    state: present

As far as the ansible error is concerned, you must enclose your command in double quotes since you are using single quotes inside:

 - name: upgrade linux-generic
    when: pkg_chk.rc == 0
      command: "apt-get install -y --only-upgrade -o Dpkg::Options::='force-confdef' -o Dpkg::Options::='force-confold' linux-generic"
      register: install_chk

  - name: autoremove linux-generic
    when: install_chk.rc == 0
      command: "apt-get -y autoremove -o Dpkg::Options::='force-confdef' -o Dpkg::Options::='force-confold'"
      register: auto_chk
Related Topic