本文介绍了Ansible:迭代结果返回值 yum 模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:我有很多节点需要更新包.有些节点安装了这些软件包,有些则没有.目标是1. 使用 yum 模块检查是否安装了软件包.2. 如果安装了包并且有更新可用,则运行 yum update

Problem: I have many nodes that need package updates. Some of the nodes have these packages installed and some do not. The goal is to 1. check if a package is installed using the yum module.2. if package is installed and update is available then run yum update

我知道这很容易通过命令或 shell 完成,但效率很低.

I know this is easily done via command or shell but very inefficient.

  tasks:
  - name: check if packages are installed
    yum: list="{{ item }}"
    with_items:
      - acpid
      - c-ares
      - automake
   register: packages


  - debug:
      var: packages

会产生结果

我想要 ansible 做的是仅当 yum: list 看到该软件包已安装并且从上述结果中可以进行升级时才更新该软件包.

What i want ansible to do is to update the package only when yum: list sees the package as installed and an upgrade available from the above results.

我不确定使用 yum 模块是否可行.

I'm not sure if that is possible using the yum module.

快速简便的方法就是使用命令:

The quick and easy way would just to use command:

  tasks:
  - name: check if packages are installed
    command: yum update -y {{ item }}
    with_items:
      - acpid
      - c-ares 
      - automake

因为 yum 更新包只会更新一个安装了的包.

since yum update package will only update a package if it is installed.

推荐答案

ansible.builtin.yum: 模块仅在安装了软件包时才会更新.您可以使用 loop: 指令循环遍历项目列表,或者如果它是一个短列表,则在任务块中声明变量并使用 yum 模块对列表进行操作的能力.喜欢快速和肮脏的版本.

The ansible.builtin.yum: module already updates only if a package is installed. You can loop over a list of items using the loop: directive, or if it's a short list, declare the variable within the task block and use the yum module's ability to operate over a list. Like the quick and dirty version.

- name: update a list of packages
  yum:
    name: "{{ packagelist }}"
    state: latest
  vars:
    packagelist:
      - acpid
      - c-ares 
      - automake

或者,更简单:

- name: update a list of packages
  yum:
    name:
      - acpid
      - c-ares 
      - automake
    state: latest

还有更多示例可用,所有参数都在此处定义:关于 yum 的 Ansible 文档文章

Many more examples are available and all the parameters are defined here:Ansible Docs article about yum

这篇关于Ansible:迭代结果返回值 yum 模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 13:14