如何防止 dpkg 安装任务在第二次运行时通知更改的状态


没有

直接安装.deb软件包的模块。当您必须将 dpkg 作为命令运行时,它始终将安装任务标记为已更改的任务。我在正确配置它时会遇到一些麻烦,所以我在这里作为公共笔记本发布。

以下是使用 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

上传到另一个任务中的 {{temp_dir}} 的文件。

下面的答案仍然有效,但较新的 ansible 版本具有 apt 模块。Mariusz Sawicki的答案是现在的首选。我已将其标记为可接受的答案。

它仅适用于 Ansible 版本 1.3,当时添加了changed_when参数。有点笨拙,也许有人可以改进解决方案。我没有找到这个"寄存器"对象的文档。

- 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

在这里,您可以运行相同的任务,它只会在第一次安装。首次安装后,将不会安装包。

有两个修改。一个是防止 dpkg 重新安装软件的参数--skip-same-version。另一个是寄存器和changed_when属性。dpkg 第一次运行时,它会打印一个以"选择"开头的字符串,并通知更改。稍后它将具有不同的输出。我尝试了一个更易读的条件,但无法让它与使用"not"或搜索子字符串的更复杂的条件一起工作。

在 Ansible 1.6(及更高版本(中,apt 模块有一个 deb 选项:

- apt: deb=/tmp/mypackage.deb

您可以将 apt 模块与dpkg_options参数一起使用:

- name: Install old python 
  apt: deb={{ temp_dir }}/{{ item }} dpkg_options="skip-same-version"
  register: dpkg_result
  changed_when: dpkg_result.stderr.find("already installed") == -1
  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

最新更新