我正在写一部剧本来收集某些网络设备中的数据,但是现在我很难过滤这些数据。我只需要某些键的值即可。
这是我得到的完整数据:
"license": {
"response": {
"@status": "success",
"result": {
"licenses": {
"entry": [
{
"authcode": "11111111",
"description": "Feature A",
"expired": "no",
"expires": "September 10, 2020",
"feature": "Feature A",
"issued": "September 10, 2016",
"serial": "96765555555"
},
{
"authcode": "222222222",
"description": "Feature B",
"expired": "no",
"expires": "September 14, 2020",
"feature": "Feature B",
"issued": "September 14, 2016",
"serial": "77777777777"
从这开始,我只需要功能并过期信息,如下所示:
"expires": "September 10, 2020",
"feature": "Feature A",
"expires": "September 14, 2020",
"feature": "Feature B",
我正在尝试使用如下所示的正则表达式,但是我无法获得完整的值,并且它无法仅将第一个键(即两个具有相同名称的键)行进:
set_fact: 功能:“ {{(license_output.stdout | regex_search('feature(。+?)'))}}”” 过期:“ {{(license_output.stdout | regex_search('expires(。+?)'))}}”
调试: 讯息:“ {{功能}}” 信息:“ {{过期}}”
我当前的代码:
set_fact: 功能:“ {{(license_output.stdout | regex_search('feature(。+?)'))}}}”
过期:“ {{(license_output.stdout | regex_search('expires(。+?)'))}}“
调试: 讯息:“ {{功能}}” 信息:“ {{过期}}”
这是预期的结果:
"expires": "September 10, 2020",
"feature": "Feature A",
"expires": "September 14, 2020",
"feature": "Feature B",
答案 0 :(得分:0)
似乎您在license_output.stdout
中得到了JSON响应。当您可以直接在json数据中查找值时,没有理由尝试使用正则表达式进行解析。考虑:
---
- hosts: localhost
gather_facts: false
vars:
license_output:
stdout: |
{
"license": {
"response": {
"@status": "success",
"result": {
"licenses": {
"entry": [
{
"authcode": "11111111",
"description": "Feature A",
"expired": "no",
"expires": "September 10, 2020",
"feature": "Feature A",
"issued": "September 10, 2016",
"serial": "96765555555"
},
{
"authcode": "222222222",
"description": "Feature B",
"expired": "no",
"expires": "September 14, 2020",
"feature": "Feature B",
"issued": "September 14, 2016",
"serial": "77777777777"
}
]
}
}
}
}
}
tasks:
- debug:
msg:
feature: "{{ item.feature }}"
expires: "{{ item.expires }}"
loop: "{{ (license_output.stdout|from_json).license.response.result.licenses.entry }}"
这将输出:
TASK [debug] **********************************************************************************
ok: [localhost] => (item=Feature A) => {
"msg": {
"expires": "September 10, 2020",
"feature": "Feature A"
}
}
ok: [localhost] => (item=Feature B) => {
"msg": {
"expires": "September 14, 2020",
"feature": "Feature B"
}
}
不清楚要使用这些值做什么。您在示例中使用的是set_fact
,但是由于具有多种功能,因此不能使用名为feature
或expires
的单个变量。一种可能性是这样的:
- set_fact:
"expires_{{ item.feature.lower().replace(' ', '_') }}": "{{ item.expires }}"
loop: "{{ (license_output.stdout|from_json).license.response.result.licenses.entry }}"
loop_control:
label: "{{ item.feature }}"
- debug:
msg: "Feature A expires on {{ expires_feature_a }}"
这将创建名称为expires_feature_a
和expires_feature_b
的变量。上面的示例将输出:
TASK [debug] **********************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "Feature A expires on September 10, 2020"
}
另一个例子可能是建立一个字典,将特征名称映射到失效日期:
- set_fact:
features: "{{ features|default({})|combine({item.feature: item.expires}) }}"
loop: "{{ (license_output.stdout|from_json).license.response.result.licenses.entry }}"
loop_control:
label: "{{ item.feature }}"
- debug:
msg: "Feature {{ item.0 }} expires on {{ item.1 }}"
loop: "{{ features.items() }}"
这将输出:
TASK [debug] **********************************************************************************************************************************************************************************
ok: [localhost] => (item=[u'Feature B', u'September 14, 2020']) => {
"msg": "Feature Feature B expires on September 14, 2020"
}
ok: [localhost] => (item=[u'Feature A', u'September 10, 2020']) => {
"msg": "Feature Feature A expires on September 10, 2020"
}