Ansible:迭代结果返回值yum模块 [英] Ansible: iterate over a results return value yum module

查看:328
本文介绍了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

会产生 结果

我想做的是仅在yum:时才更新软件包:列表将软件包视为已安装,并且可以从上述结果中进行升级.

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 循环文档的部分register循环.

The Ansible loops documentation has a section about using register in a loop.

看一下debug任务的输出,您可以看到packages变量具有一个名为results的键,该键包含第一个任务中with_items循环的结果.大型结构如下所示:

Taking a look at the output of your debug task, you can see that your packages variable has a key named results that contains the results of your with_items loop in the first task. The large structure looks like this:

{  
   "packages":{  
      "changed":false,
      "msg":"All items completed",
      "results":[  
         {  
            "item":"...",
            "results":[  

            ]
         },
         {  
            "item":"...",
            "results":[  

            ]
         }
      ]
   }
}

每个结果都有一个键item和一个results键,其中item包含该结果的循环迭代器的值,该键包含list选项返回给yum模块.

Each individual result has a key item that contains the value of the loop iterator for that result, and a results key that contains the list of packages (possible empty) returned by the list option to the yum module.

考虑到这一点,您可以像这样遍历结果:

With that in mind, you could loop over the results like this:

- debug:
    msg: "{{ item.item }}"
  with_items: "{{ packages.results }}"
  when: item.results

when条件仅与list操作返回非空结果的结果匹配.

The when condition matches only those results for which the list operation returned a non-empty result.

要升级匹配的软件包,请执行以下操作:

To upgrade matching packages:

- yum:
    name: "{{ item.item }}"
    state: latest
  with_items: "{{ packages.results }}"
  when: item.results

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆