多个命令的 Ansible 寄存器结果 [英] Ansible register result of multiple commands

查看:24
本文介绍了多个命令的 Ansible 寄存器结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的任务是验证所有 Linux 服务器的一些路由条目,这是我使用 Ansible playbook 的方法

I was given a task to verify some routing entries for all Linux server and here is how I did it using an Ansible playbook

---
  - hosts: Linux
    serial: 1

    tasks:
      - name: Check first
        command: /sbin/ip route list xxx.xxx.xxx.xxx/24
        register: result
        changed_when: false

      - debug: msg="{{result.stdout}}"

      - name: Check second
        command: /sbin/ip route list xxx.xxx.xxx.xxx/24
        register: result
        changed_when: false

      - debug: msg="{{result.stdout}}"

您可以看到我必须为每个路由条目重复相同的任务,我相信我应该能够避免这种情况.我尝试使用 with_items 循环,但收到以下错误消息

You can see I have to repeat same task for each routing entry and I believe I should be able to avoid this. I tried use with_items loop but got following error message

One or more undefined variables: 'dict object' has no attribute 'stdout'

有没有办法为每个命令注册变量并一个接一个循环?

is there a way to register variable for each command and loop over them one by one ?

推荐答案

从 Ansible 1.6.1 开始,多个项目注册的结果作为数组存储在 result.results 中.所以你可以使用 result.results[0].stdout 等等.

Starting in Ansible 1.6.1, the results registered with multiple items are stored in result.results as an array. So you can use result.results[0].stdout and so on.

测试手册:

---
- hosts: localhost
  gather_facts: no
  tasks:
    - command: "echo {{item}}"
      register: result
      with_items: [1, 2]
    - debug:
        var: result

结果:

$ ansible-playbook -i localhost, test.yml

PLAY [localhost] ************************************************************** 

TASK: [command echo {{item}}] ************************************************* 
changed: [localhost] => (item=1)
changed: [localhost] => (item=2)

TASK: [debug ] **************************************************************** 
ok: [localhost] => {
    "var": {
        "result": {
            "changed": true, 
            "msg": "All items completed", 
            "results": [
                {
                    "changed": true, 
                    "cmd": [
                        "echo", 
                        "1"
                    ], 
                    "delta": "0:00:00.002502", 
                    "end": "2015-08-07 16:44:08.901313", 
                    "invocation": {
                        "module_args": "echo 1", 
                        "module_name": "command"
                    }, 
                    "item": 1, 
                    "rc": 0, 
                    "start": "2015-08-07 16:44:08.898811", 
                    "stderr": "", 
                    "stdout": "1", 
                    "stdout_lines": [
                        "1"
                    ], 
                    "warnings": []
                }, 
                {
                    "changed": true, 
                    "cmd": [
                        "echo", 
                        "2"
                    ], 
                    "delta": "0:00:00.002516", 
                    "end": "2015-08-07 16:44:09.038458", 
                    "invocation": {
                        "module_args": "echo 2", 
                        "module_name": "command"
                    }, 
                    "item": 2, 
                    "rc": 0, 
                    "start": "2015-08-07 16:44:09.035942", 
                    "stderr": "", 
                    "stdout": "2", 
                    "stdout_lines": [
                        "2"
                    ], 
                    "warnings": []
                }
            ]
        }
    }
}

PLAY RECAP ******************************************************************** 
localhost                  : ok=2    changed=1    unreachable=0    failed=0   

这篇关于多个命令的 Ansible 寄存器结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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