从Ansible中的stdout获取特定输出,同时循环多个值

时间:2019-05-21 10:59:32

标签: ansible

我们需要从执行的任意bigip命令中获取GTM池成员信息。我们已将输出的内容存储到寄存器中,并将获取池成员并将其存储在列表中。

我们基本上想要构建一个常规以匹配输出,如下所述。

我们可以尝试在python中构建自定义过滤器并获取所有池成员。

预期输出:-

如果我们需要遍历多个GTM池以获取GTM池成员信息,那将是什么逻辑?

预期输出:-

“ test0506:10_10_10_2_9094, test0304:10_10_10_1_9093, abc0102:/ Common / vs_10_10_10_10_80, xyz0102:/ Common / vs_10_10_10_11_80, test0102:/ Common / vs_10_10_10_12_80, foo0102:/ Common / vs_10_10_10_13_80“。

从下面提到的输出中,我需要获取成员信息并将其存储在列表中:-

---
- hosts: gtm.abc.com
  connection: local
  gather_facts: no

  tasks:

      - name: Check Pool Contents
        bigip_command:
          user: admin
          password: admin
          server: "{{ inventory_hostname }}"
          commands: "list gtm pool a {{ item }}"
          warn: no
          validate_certs: no
        register: output
        delegate_to: localhost
        loop: "{{ gtm_pool }}"

      - debug: var=output

如果池列表包含多个池名称,则输出的调试将包含多个stdout。

"output": {
        "changed": false, 
        "deprecations": [
            {
                "msg": "Param 'server' is deprecated. See the module docs for more information", 
                "version": 2.9
            }, 
            {
                "msg": "Param 'user' is deprecated. See the module docs for more information", 
                "version": 2.9
            }, 
            {
                "msg": "Param 'password' is deprecated. See the module docs for more information", 
                "version": 2.9
            }, 
            {
                "msg": "Param 'validate_certs' is deprecated. See the module docs for more information", 
                "version": 2.9
            }, 
            {
                "msg": "Param 'server' is deprecated. See the module docs for more information", 
                "version": 2.9
            }, 
            {
                "msg": "Param 'user' is deprecated. See the module docs for more information", 
                "version": 2.9
            }, 
            {
                "msg": "Param 'password' is deprecated. See the module docs for more information", 
                "version": 2.9
            }, 
            {
                "msg": "Param 'validate_certs' is deprecated. See the module docs for more information", 
                "version": 2.9
            }
        ], 
        "msg": "All items completed", 
        "results": [
            {
                "_ansible_delegated_vars": {
                    "ansible_delegated_host": "localhost", 
                    "ansible_host": "localhost"
                }, 
                "_ansible_ignore_errors": null, 
                "_ansible_item_label": "test-pool1", 
                "_ansible_item_result": true, 
                "_ansible_no_log": false, 
                "_ansible_parsed": true, 
                "changed": false, 
                "executed_commands": [
                    "tmsh -c \\\"list gtm pool a test-pool1\\\""
                ], 
                "failed": false, 
                "invocation": {
                    "module_args": {
                        "auth_provider": null, 
                        "chdir": null, 
                        "commands": "list gtm pool a test-pool1", 
                        "interval": 1, 
                        "match": "all", 
                        "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", 
                        "provider": {
                            "auth_provider": null, 
                            "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", 
                            "server": "gtm.abc.com", 
                            "server_port": null, 
                            "ssh_keyfile": null, 
                            "timeout": null, 
                            "transport": "rest", 
                            "user": "verify", 
                            "validate_certs": false
                        }, 
                        "retries": 10, 
                        "server": "gtm.abc.com", 
                        "server_port": null, 
                        "transport": "rest", 
                        "user": "verify", 
                        "validate_certs": false, 
                        "wait_for": null, 
                        "warn": false
                    }
                }, 
                "item": "test-pool1", 
                "stdout": [
                    "gtm pool a test-pool1 {\n    fallback-mode drop-packet\n    members {\n        test0304:10_10_10_1_9093 {\n            depends-on {\n                test0506:10_10_10_2_9094 { }\n            }\n            member-order 0\n        }\n    }\n}"
                ], 
                "stdout_lines": [
                    [
                        "gtm pool a test-pool1 {", 
                        "    fallback-mode drop-packet", 
                        "    members {", 
                        "        test0304:10_10_10_1_9093 {", 
                        "            depends-on {", 
                        "                test0506:10_10_10_2_9094 { }", 
                        "            }", 
                        "            member-order 0", 
                        "        }", 
                        "    }", 
                        "}"
                    ]
                ]
            }, 
            {
                "_ansible_delegated_vars": {
                    "ansible_delegated_host": "localhost", 
                    "ansible_host": "localhost"
                }, 
â–½             "_ansible_ignore_errors": null, 
                "_ansible_item_label": "test-pool2", 
                "_ansible_item_result": true, 
                "_ansible_no_log": false, 
                "_ansible_parsed": true, 
                "changed": false, 
                "executed_commands": [
                    "tmsh -c \\\"list gtm pool a test-pool2\\\""
                ], 
                "failed": false, 
                "invocation": {
                    "module_args": {
                        "auth_provider": null, 
                        "chdir": null, 
                        "commands": "list gtm pool a test-pool2", 
                        "interval": 1, 
                        "match": "all", 
                        "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", 
                        "provider": {
                            "auth_provider": null, 
                            "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", 
                            "server": "gtm.abc.com", 
                            "server_port": null, 
                            "ssh_keyfile": null, 
                            "timeout": null, 
                            "transport": "rest", 
                            "user": "verify", 
                            "validate_certs": false
                        }, 
                        "retries": 10, 
                        "server": "gtm.abc.com", 
                        "server_port": null, 
                        "transport": "rest", 
                        "user": "verify", 
                        "validate_certs": false, 
                        "wait_for": null, 
                        "warn": false
                    }
                }, 
                "item": "test-pool2", 
                "stdout": [
                    "gtm pool a test-pool2 { alternate-mode global-availability fallback-ip 10.10.10.10 fallback-mode fallback-ip max-answers-returned 5 members { abc0102:/Common/vs_10_10_10_10_80 { disabled member-order 1 } xyz0102:/Common/vs_10_10_10_11_80 { member-order 0 } test0102:/Common/vs_10_10_10_12_80 { member-order 3 } foo0102:/Common/vs_10_10_10_13_80 { member-order 2 } } }"
                ], 
                "stdout_lines": [
                    [
            gtm pool a test-pool2 {
            alternate-mode global-availability
                    fallback-ip 1.1.1.1
                fallback-mode fallback-ip
                max-answers-returned 5
            members {
                    abc0102:/Common/vs_10_10_10_10_80 {
                        disabled
                        member-order 1
                }
                    xyz0102:/Common/vs_10_10_10_11_80 {
                        member-order 0
                }
                    test0102:/Common/vs_10_10_10_12_80 {
                        member-order 3
                }
                    foo0102:/Common/vs_10_10_10_13_80 {
                        member-order 2
                        "        }", 
                        "    }", 
                        "}"
                    ]
                ]
            }
        ]
    }
}

输出场景1:-

gtm pool a test-pool2 {
    alternate-mode global-availability
    fallback-ip 1.1.1.1
    fallback-mode fallback-ip
    max-answers-returned 5
    members {
        abc0102:/Common/vs_10_10_10_10_80 {
            disabled
            member-order 1
        }
        xyz0102:/Common/vs_10_10_10_11_80 {
            member-order 0
        }
        test0102:/Common/vs_10_10_10_12_80 {
            member-order 3
        }
        foo0102:/Common/vs_10_10_10_13_80 {
            member-order 2
        }
    }
}

输出场景2:-

gtm pool a test-pool1 {, 
                            fallback-mode drop-packet", 
                            members {", 
                                test0304:10_10_10_1_9093 {", 
                                    depends-on {", 
                                        test0506:10_10_10_2_9094 { }, 
                                    }, 
                                    member-order 0", 
                                }, 
                            }, 

0 个答案:

没有答案