首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >来自F5 bigip_command的可能的筛选器标准输出

来自F5 bigip_command的可能的筛选器标准输出
EN

Stack Overflow用户
提问于 2021-11-04 14:55:56
回答 1查看 121关注 0票数 0
代码语言:javascript
复制
bigip_command:
  commands:
    - "tmsh list ltm policy {{ item }}"

这是我得到的输出:

代码语言:javascript
复制
    TASK [f5_maintenance : tmsh list policies] *************************************
task path: /tmp/bwrap_107256_wsbqhduw/awx_107256_ltm6_eq5/requirements_roles/f5_maintenance/tasks/f5_gather_facts.yml:125
<localhost> Using network group action bigip for bigip_command
<localhost> connection transport is rest
Using module file /usr/lib/python2.7/site-packages/ansible/modules/network/f5/bigip_command.py
Pipelining is enabled.
<localhost> ESTABLISH LOCAL CONNECTION FOR USER: awx
<localhost> EXEC /bin/sh -c '/usr/bin/python2 && sleep 0'
ok: [LSEL2401.site -> localhost] => (item=policy_test.net_policy) => {
    "ansible_loop_var": "item", 
    "changed": false, 
    "executed_commands": [
        "tmsh -c \\\\\\"list ltm policy policy_test.net_policy\\\\\\""
    ], 
    "invocation": {
        "module_args": {
            "chdir": null, 
            "commands": [
                "tmsh list ltm policy policy_test.net_policy"
            ], 
            "interval": 1, 
            "match": "all", 
            "provider": {
                "auth_provider": null, 
                "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", 
                "server": "LSEL2401.site", 
                "server_port": 443, 
                "ssh_keyfile": null, 
                "timeout": null, 
                "transport": "rest", 
                "user": "TOWER", 
                "validate_certs": false
            }, 
            "retries": 10, 
            "transport": "rest", 
            "wait_for": null, 
            "warn": true
        }
    }, 
    "item": "policy_test.net_policy", 
    "stdout": [
        "ltm policy policy_test.net_policy {\\n    controls { forwarding persistence }\\n    last-modified 2021-09-24:21:53:22\\n    requires { http }\\n    rules {\\n        policy_test.net_rule1 {\\n            actions {\\n                0 {\\n                    forward\\n                    select\\n                    pool policy_test.net_pool1\\n                }\\n                1 {\\n                    persist\\n                    cookie-insert\\n                    name FSdefault\\n                }\\n            }\\n            conditions {\\n                0 {\\n                    http-uri\\n                    path\\n                    starts-with\\n                    values { / }\\n                }\\n            }\\n            ordinal 1\\n        }\\n        policy_test.net_rule2 {\\n            actions {\\n                0 {\\n                    forward\\n                    select\\n                    pool policy_test.net_pool2\\n                }\\n                1 {\\n                    persist\\n                    cookie-insert\\n                    name KSexpress\\n                }\\n            }\\n            conditions {\\n                0 {\\n                    http-uri\\n                    path\\n                    starts-with\\n                    values { /test }\\n                }\\n            }\\n        }\\n    }\\n    status published\\n    strategy first-match\\n}"
    ], 
    "stdout_lines": [
        [
            "ltm policy policy_test.net_policy {", 
            "    controls { forwarding persistence }", 
            "    last-modified 2021-09-24:21:53:22", 
            "    requires { http }", 
            "    rules {", 
            "        policy_test.net_rule1 {", 
            "            actions {", 
            "                0 {", 
            "                    forward", 
            "                    select", 
            "                    pool policy_test.net_pool1", 
            "                }", 
            "                1 {", 
            "                    persist", 
            "                    cookie-insert", 
            "                    name FSdefault", 
            "                }", 
            "            }", 
            "            conditions {", 
            "                0 {", 
            "                    http-uri", 
            "                    path", 
            "                    starts-with", 
            "                    values { / }", 
            "                }", 
            "            }", 
            "            ordinal 1", 
            "        }", 
            "        policy_test.net_rule2 {", 
            "            actions {", 
            "                0 {", 
            "                    forward", 
            "                    select", 
            "                    pool policy_test.net_pool2", 
            "                }", 
            "                1 {", 
            "                    persist", 
            "                    cookie-insert", 
            "                    name KSexpress", 
            "                }", 
            "            }", 
            "            conditions {", 
            "                0 {", 
            "                    http-uri", 
            "                    path", 
            "                    starts-with", 
            "                    values { /test }", 
            "                }", 
            "            }", 
            "        }", 
            "    }", 
            "    status published", 
            "    strategy first-match", 
            "}"
        ]
    ]
}

这是我的任务。

代码语言:javascript
复制
- name: Collect bigip facts
  bigip_device_info:
    gather_subset: 
      - ltm-policies
    provider: "{{ cli }}"
  register: policy_facts

- name: Policies
  set_fact:
    policy: "{{ policy_facts.ltm_policies|default({}) | to_json | from_json | json_query(query_string) }}"
  vars:
   query_string: "[?contains(name, '{{ app_fqdn }}')].name"

- name: policy name 
  debug:
    msg: "{{ policy }}"

- name: tmsh list policies
  bigip_command:
    commands:
     - "tmsh list ltm policy {{ item }} "
    provider: "{{ cli }}"
  delegate_to: localhost
  loop: "{{ policy }}"
  register: layer7_pools

- name: VIP Results stdout_lines
  debug: msg="{{ layer7_pools.results }}"
EN

回答 1

Stack Overflow用户

发布于 2021-11-08 12:24:14

经过研究,我从F5文档中了解到,tmsh list的输出是声明性的,但不是JSON。对于JSON输出,他们引用了REST API。

由于stdout_lines的值是一个只有一个元素的列表,而stdout只是一个只包含换行符(\n)的字符串元素的列表,所以我将重点放在通过在字符串中进行搜索来获得所需的结果。

为此,我根据您提供的结果设置了一个测试

代码语言:javascript
复制
- name: Set result value for bigip_command tmsh list
  set_fact:
    layer7_pools:
      stdout: [
        "ltm policy policy_test.net {\n ... controls  strategy first-match\n}"
      ]
      stdout_lines: [
        [
          "ltm policy policy_test.net {",
          "    controls { forwarding }",
          ...
          "    strategy first-match",
          "}"
        ]
      ]

并设置以下过滤器。

代码语言:javascript
复制
- name: Show result, filter for pool names
  debug:
    msg: "{{ layer7_pools.stdout[0] | regex_findall('pool .*', multiline=True) }}"

代码语言:javascript
复制
TASK [Show result, filter for pool names] **********************************************************************************************************************
ok: [test.example.com] =>
  msg:
  - pool policy_test_rule1.net_pool
  - pool policy_test_rule2_pool

多亏了

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69841247

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档