如何使用 ansible 升级单个包?

如何使用 ansible 升级单个包?

我正在尝试编写一个 ansible playbook,它只升级软件包linux-generic,但找不到让它真正起作用的魔法咒语。通过这个剧本,我得到了一个错误parameters are mutually exclusive: deb|package|upgrade,谷歌建议这是由于upgrade: full它不适用于单个软件包:

  - 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

移除upgrade: full正在播放...

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

但是,当我登录到机器并运行时,aptitude full-upgrade它显示 ansible 没有执行任何操作:

# 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}

好的,所以我想我只需将command:模块扔给它并使用裸apt命令,但是命令字符串存在一些问题,我没有正确转义。

  - 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

这会产生以下错误:

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

我尝试过转义: =字符,以及替换双引号和单引号。有人知道如何实现吗?

答案1

有什么具体的原因导致你不考虑 ansible 包模块,例如

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

就 ansible 错误而言,您必须将命令括在双引号中,因为您在里面使用单引号:

 - 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

相关内容