【问题标题】:Ansible filter stdout from F5 bigip_command来自 F5 bigip_command 的 Ansible 过滤器标准输出
【发布时间】:2021-12-18 19:12:48
【问题描述】:

我试图弄清楚如何从 F5 bigip_command_module 的标准输出结果中过滤掉池名称

bigip_command:
  commands:
    - "tmsh list ltm policy {{ item }}"

这是我得到的输出:

    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", 
            "}"
        ]
    ]
}

这是我的任务。

- 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 }}"

【问题讨论】:

  • 您能否提供您的任务的完整描述?尤其是你是怎么注册结果的,你是怎么得到layer7_pools的?由于stdout_lines的内容看起来像JSON,如果你register: result并打印出"{{ result.json }}",这样行吗?
  • 是的,我只是想过滤返回值以仅获取池。我需要池信息来进一步查找池的成员及其状态。
  • 经过一些研究,您似乎喜欢做JSON parsing in Ansibleto get the values from Ansible JSON stdout。根据这个,您可以先检查{{ layer7_pools.stdout | from_json}} 的输出,看看它是否能让您更好地访问数据?
  • 这是我在使用 {{ layer7_pools.stdout |从_json}}。 “在 ({{ layer7_pools.stdout | from_json}}) 上发生了意外的模板类型错误:预期的字符串或缓冲区”,“_ansible_no_log”:false

标签: ansible f5


【解决方案1】:

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

由于stdout_lines 的值是一个只有一个元素的列表列表,而stdout 只是一个只有一个字符串元素的列表,包括换行符(\n),我专注于通过在字符串中搜索得到必要的结果。

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

- 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",
          "}"
        ]
      ]

并设置以下过滤器。

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

导致输出

TASK [Show result, filter for pool names] **********************************************************************************************************************
ok: [test.example.com] =>
  msg:
  - pool policy_test_rule1.net_pool
  - pool policy_test_rule2_pool

感谢

【讨论】:

  • 我现在收到以下错误。 { "msg": "在 ({{ layer7_pools.stdout[0] | regex_findall('pool .*', multiline=True) }}) 发生意外的模板类型错误:预期的字符串或缓冲区", "_ansible_no_log": false } @U880D
  • @SDay,变量layer7_pools now 的内容是什么?
  • 我用 var layer7_pools 的整个输出更新了我的原始问题,它包括标准输出和标准输出线
  • 啊,我明白了。你是registering variables with a loop。我的示例有效,因为它还没有循环实现。我认为您需要loop: "{{ layer7_pools }}"debug: msg: "{{ item[0] ...。也许我也可以这样设置。
猜你喜欢
  • 1970-01-01
  • 2021-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-03
  • 1970-01-01
  • 2016-12-18
  • 1970-01-01
相关资源
最近更新 更多