【发布时间】: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 Ansible 和to 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