json - ansible 从寄存器变量访问项目

标签 json list variables ansible

我运行一项任务,捕获多个设备的接口(interface)状态,在某些情况下捕获多个接口(interface)的状态,但我一次只能检索一个 stdout_lines,但我需要检索它运行的主机的所有 stdout_lines命令打开。我有任务代码来访问列表中各自的 stdout_lines,但不是全部。

    - name: Execute IOS command to check port state up/down before configuration
      ios_command:
        commands:
          - "show ip int brief | in {{item}}"
      loop: "{{ports}}"
      register: PortUpDown

    - name: print port state up/down
      debug:
        #msg: "{{item.0 | to_nice_yaml}}"
        #msg: "{{item.1 | to_nice_yaml}}"
        msg: "{{item}}"
      loop:
        #- "{{PortUpDown.results.0.stdout_lines}}"
        #- "{{PortUpDown.results.1.stdout_lines}}"
         - "{{PortUpDown.results}}"
      register: PortUpDown

    - name: print var that capture port state up/down
      debug:
        var: PortUpDown

正如您所看到的,我已经评论了我在测试期间访问的项目。我最终想要的是能够访问 0,1,2,3 等而无需声明它,因为在脚本开始时我不知道它是否只是列表中的一个接口(interface)或多个接口(interface)我将获取端口状态。

输出是这样的。

    ok: [hub1] => {
    "PortUpDown": {
        "changed": false, 
        "msg": "All items completed", 
        "results": [
            {
                "ansible_loop_var": "item", 
                "changed": false, 
                "failed": false, 
                "item": [
                    {
                        "ansible_facts": {
                            "discovered_interpreter_python": "/usr/bin/python"
                        }, 
                        "ansible_loop_var": "item", 
                        "changed": false, 
                        "failed": false, 
                        "invocation": {
                            "module_args": {
                                "auth_pass": null, 
                                "authorize": null, 
                                "commands": [
                                    "show ip int brief | in Ethernet1/1"
                                ], 
                                "host": null, 
                                "interval": 1, 
                                "match": "all", 
                                "password": null, 
                                "port": null, 
                                "provider": null, 
                                "retries": 10, 
                                "ssh_keyfile": null, 
                                "timeout": null, 
                                "username": null, 
                                "wait_for": null
                            }
                        }, 
                        "item": "Ethernet1/1", 
                        "stdout": [
                            "Ethernet1/1                unassigned      YES NVRAM  up                    up"
                        ], 
                        "stdout_lines": [
                            [
                                "Ethernet1/1                unassigned      YES NVRAM  up                    up"
                            ]
                        ]
                    }, 
                    {
                        "ansible_loop_var": "item", 
                        "changed": false, 
                        "failed": false, 
                        "invocation": {
                            "module_args": {
                                "auth_pass": null, 
                                "authorize": null, 
                                "commands": [
                                    "show ip int brief | in Ethernet2/1"
                                ], 
                                "host": null, 
                                "interval": 1, 
                                "match": "all", 
                                "password": null, 
                                "port": null, 
                                "provider": null, 
                                "retries": 10, 
                                "ssh_keyfile": null, 
                                "timeout": null, 
                                "username": null, 
                                "wait_for": null
                            }
                        }, 
                        "item": "Ethernet2/1", 
                        "stdout": [
                            "Ethernet2/1                unassigned      YES NVRAM  administratively down down"
                        ], 
                        "stdout_lines": [
                            [
                                "Ethernet2/1                unassigned      YES NVRAM  administratively down down"
                            ]
                        ]
                    }
                ], 
                "msg": [
                    {
                        "ansible_facts": {
                            "discovered_interpreter_python": "/usr/bin/python"
                        }, 
                        "ansible_loop_var": "item", 
                        "changed": false, 
                        "failed": false, 
                        "invocation": {
                            "module_args": {
                                "auth_pass": null, 
                                "authorize": null, 
                                "commands": [
                                    "show ip int brief | in Ethernet1/1"
                                ], 
                                "host": null, 
                                "interval": 1, 
                                "match": "all", 
                                "password": null, 
                                "port": null, 
                                "provider": null, 
                                "retries": 10, 
                                "ssh_keyfile": null, 
                                "timeout": null, 
                                "username": null, 
                                "wait_for": null
                            }
                        }, 
                        "item": "Ethernet1/1", 
                        "stdout": [
                            "Ethernet1/1                unassigned      YES NVRAM  up                    up"
                        ], 
                        "stdout_lines": [
                            [
                                "Ethernet1/1                unassigned      YES NVRAM  up                    up"
                            ]
                        ]
                    }, 
                    {
                        "ansible_loop_var": "item", 
                        "changed": false, 
                        "failed": false, 
                        "invocation": {
                            "module_args": {
                                "auth_pass": null, 
                                "authorize": null, 
                                "commands": [
                                    "show ip int brief | in Ethernet2/1"
                                ], 
                                "host": null, 
                                "interval": 1, 
                                "match": "all", 
                                "password": null, 
                                "port": null, 
                                "provider": null, 
                                "retries": 10, 
                                "ssh_keyfile": null, 
                                "timeout": null, 
                                "username": null, 
                                "wait_for": null
                            }
                        }, 
                        "item": "Ethernet2/1", 
                        "stdout": [
                            "Ethernet2/1                unassigned      YES NVRAM  administratively down down"
                        ], 
                        "stdout_lines": [
                            [
                                "Ethernet2/1                unassigned      YES NVRAM  administratively down down"
                            ]
                        ]
                    }
                ]
            }
        ]
    }
}
ok: [hub2] => {
    "PortUpDown": {
        "changed": false, 
        "msg": "All items completed", 
        "results": [
            {
                "ansible_loop_var": "item", 
                "changed": false, 
                "failed": false, 
                "item": [
                    {
                        "ansible_facts": {
                            "discovered_interpreter_python": "/usr/bin/python"
                        }, 
                        "ansible_loop_var": "item", 
                        "changed": false, 
                        "failed": false, 
                        "invocation": {
                            "module_args": {
                                "auth_pass": null, 
                                "authorize": null, 
                                "commands": [
                                    "show ip int brief | in Ethernet1/1"
                                ], 
                                "host": null, 
                                "interval": 1, 
                                "match": "all", 
                                "password": null, 
                                "port": null, 
                                "provider": null, 
                                "retries": 10, 
                                "ssh_keyfile": null, 
                                "timeout": null, 
                                "username": null, 
                                "wait_for": null
                            }
                        }, 
                        "item": "Ethernet1/1", 
                        "stdout": [
                            "Ethernet1/1                unassigned      YES NVRAM  up                    up"
                        ], 
                        "stdout_lines": [
                            [
                                "Ethernet1/1                unassigned      YES NVRAM  up                    up"
                            ]
                        ]
                    }, 
                    {
                        "ansible_loop_var": "item", 
                        "changed": false, 
                        "failed": false, 
                        "invocation": {
                            "module_args": {
                                "auth_pass": null, 
                                "authorize": null, 
                                "commands": [
                                    "show ip int brief | in Ethernet2/1"
                                ], 
                                "host": null, 
                                "interval": 1, 
                                "match": "all", 
                                "password": null, 
                                "port": null, 
                                "provider": null, 
                                "retries": 10, 
                                "ssh_keyfile": null, 
                                "timeout": null, 
                                "username": null, 
                                "wait_for": null
                            }
                        }, 
                        "item": "Ethernet2/1", 
                        "stdout": [
                            "Ethernet2/1                unassigned      YES NVRAM  administratively down down"
                        ], 
                        "stdout_lines": [
                            [
                                "Ethernet2/1                unassigned      YES NVRAM  administratively down down"
                            ]
                        ]
                    }
                ], 
                "msg": [
                    {
                        "ansible_facts": {
                            "discovered_interpreter_python": "/usr/bin/python"
                        }, 
                        "ansible_loop_var": "item", 
                        "changed": false, 
                        "failed": false, 
                        "invocation": {
                            "module_args": {
                                "auth_pass": null, 
                                "authorize": null, 
                                "commands": [
                                    "show ip int brief | in Ethernet1/1"
                                ], 
                                "host": null, 
                                "interval": 1, 
                                "match": "all", 
                                "password": null, 
                                "port": null, 
                                "provider": null, 
                                "retries": 10, 
                                "ssh_keyfile": null, 
                                "timeout": null, 
                                "username": null, 
                                "wait_for": null
                            }
                        }, 
                        "item": "Ethernet1/1", 
                        "stdout": [
                            "Ethernet1/1                unassigned      YES NVRAM  up                    up"
                        ], 
                        "stdout_lines": [
                            [
                                "Ethernet1/1                unassigned      YES NVRAM  up                    up"
                            ]
                        ]
                    }, 
                    {
                        "ansible_loop_var": "item", 
                        "changed": false, 
                        "failed": false, 
                        "invocation": {
                            "module_args": {
                                "auth_pass": null, 
                                "authorize": null, 
                                "commands": [
                                    "show ip int brief | in Ethernet2/1"
                                ], 
                                "host": null, 
                                "interval": 1, 
                                "match": "all", 
                                "password": null, 
                                "port": null, 
                                "provider": null, 
                                "retries": 10, 
                                "ssh_keyfile": null, 
                                "timeout": null, 
                                "username": null, 
                                "wait_for": null
                            }
                        }, 
                        "item": "Ethernet2/1", 
                        "stdout": [
                            "Ethernet2/1                unassigned      YES NVRAM  administratively down down"
                        ], 
                        "stdout_lines": [
                            [
                                "Ethernet2/1                unassigned      YES NVRAM  administratively down down"
                            ]
                        ]
                    }
                ]
            }
        ]
    }
}

最佳答案

您可以使用过滤器 map 的组合提取列表中所有元素的 stdout_lines ,然后 flatten列表的结果列表,以便执行此操作。

鉴于剧本:

- hosts: all
  gather_facts: no
      
  tasks:
    - command: "echo {{ item }}"
      loop: 
        - "Ethernet2/1                unassigned      YES NVRAM  administratively down down"
        - "Ethernet1/1                unassigned      YES NVRAM  up                    up"
      register: PortUpDown

    - debug: 
        msg: "{{ item }}"
      loop: "{{ PortUpDown.results | map(attribute='stdout_lines') | list | flatten }}"  

这产生了回顾:

PLAY [all] *********************************************************************************************************

TASK [command] *****************************************************************************************************
changed: [localhost] => (item=Ethernet2/1                unassigned      YES NVRAM  administratively down down)
changed: [localhost] => (item=Ethernet1/1                unassigned      YES NVRAM  up                    up)

TASK [debug] *******************************************************************************************************
ok: [localhost] => (item=Ethernet2/1 unassigned YES NVRAM administratively down down) => {
    "msg": "Ethernet2/1 unassigned YES NVRAM administratively down down"
}
ok: [localhost] => (item=Ethernet1/1 unassigned YES NVRAM up up) => {
    "msg": "Ethernet1/1 unassigned YES NVRAM up up"
}

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

关于json - ansible 从寄存器变量访问项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64142171/

相关文章:

android - 使用 Robolectric 和 Gradle 测试资源文件夹

ios - 如何在 Swift 中简化几乎相等的枚举扩展

javascript - 角色扮演游戏 : store dialog in DB or JSON

java - 在 Java 中测试隔离的自定义 JsonDeserializer

python - Cython:无法将 Python 对象转换为 'double *'

r - 基于列类的列表内的子集数据框

php - 将 MySQL 数据库值分配给 PHP 变量

C# 局部变量

java - 今天早些时候在考试中遇到问题 - ArrayList 方法

php - 可打印的 PHP 变量中的 MySQL 数据输出