Ansible从输出中获取特定值
我一直在为一件我认为很容易做的事而烦恼 我有剧本:Ansible从输出中获取特定值,ansible,Ansible,我一直在为一件我认为很容易做的事而烦恼 我有剧本: - hosts: all tasks: - name: Get F5 connections bigip_command: commands: show ltm node /mypartition/myserver.domain.com provider: server: "F5server.domain.com" user: ""
- hosts: all
tasks:
- name: Get F5 connections
bigip_command:
commands: show ltm node /mypartition/myserver.domain.com
provider:
server: "F5server.domain.com"
user: ""
password: ""
validate_certs: "no"
server_port: 443
register: connections
delegate_to: localhost
- name: output
debug:
msg: "{{ connections }}"
当我运行该剧本时,它会输出以下内容:
ok: [myserver.domain.com] => {
"msg": {
"changed": false,
"executed_commands": [
"tmsh -c \\\"show ltm node /mypartition/myserver.domain.com\\\""
],
"failed": false,
"stdout": [
"-------------------------------------------------------------\nLtm::Node: /mypartition/myserver.domain.com (123.45.67.89)\n-------------------------------------------------------------\nStatus \n Availability : available\n State : enabled\n Reason : Node address is available\n Monitor : /Common/gateway_icmp (default node monitor)\n Monitor Status : up\n Session Status : enabled\n \nTraffic ServerSide General\n Bits In 635.9M -\n Bits Out 2.8G -\n Packets In 102.8K -\n Packets Out 266.6K -\n Current Connections 7 -\n Maximum Connections 11 -\n Total Connections 6.5K -\n Total Requests - 13.0K\n Current Sessions - 0"
],
"stdout_lines": [
[
"-------------------------------------------------------------",
"Ltm::Node: /mypartition/myserver.domain.com (123.45.67.89)",
"-------------------------------------------------------------",
"Status ",
" Availability : available",
" State : enabled",
" Reason : Node address is available",
" Monitor : /Common/gateway_icmp (default node monitor)",
" Monitor Status : up",
" Session Status : enabled",
" ",
"Traffic ServerSide General",
" Bits In 635.9M -",
" Bits Out 2.8G -",
" Packets In 102.8K -",
" Packets Out 266.6K -",
" Current Connections 7 -",
" Maximum Connections 11 -",
" Total Connections 6.5K -",
" Total Requests - 13.0K",
" Current Sessions - 0"
]
]
}
}
我的问题是,如何简单地获取“当前连接”值,即在本例中为7,并将其存储在变量中
我尝试过各种不同的解决方案,但似乎都不管用
我的Ansible版本是2.9
有人能帮忙吗?这项任务
- debug:
msg: "Current Connections: {{ item.split().2 }}"
loop: "{{ connections.stdout_lines.0 }}"
when: item is match('^ Current Connections(.*)$')
给予
使用正则表达式提取值并设置变量“current_connections”
-名称:当前连接
变量:
模式:'(?感谢“xloto”的帮助,我将您的代码追加到我的末尾,但在“当前连接”任务中失败:致命:[myserver.domain.com]:失败!=>{“msg”:“在({{{Connections.stdout | regex_search(pattern)}上发生意外的模板类型错误:{Connections.stdout}):预期的字符串或缓冲区”}虽然Vlads answer对我有效,但我也很想看看我是否能让这个正则表达式解决方案工作,因为我在网上尝试的所有正则表达式方法似乎都会给我这个错误。可能正则表达式在使用提供的输出数据时有问题。@BruH如果变量connections.stdout
未定义,您会得到意外的结果出现模板类型错误
。请在运行regex\u搜索之前尝试调试变量。另请参阅以下回答:谢谢Vlad,这很好。正是我所需要的。谢谢。
"msg": "Current Connections: 7"
- name: Current Connections
vars:
pattern: '(?<=Current\sConnections)\s*\d+'
set_fact:
current_connections: "{{ connections.stdout | regex_search(pattern) }}"
- debug:
var: hostvars[inventory_hostname]['current_connections']