List 以Ansible中的字符串形式从列表中获取项
我正试图通过Ansible从KeyClope安装中获取RSA密钥。从KeyClope API返回的变量是:List 以Ansible中的字符串形式从列表中获取项,list,ansible,List,Ansible,我正试图通过Ansible从KeyClope安装中获取RSA密钥。从KeyClope API返回的变量是: "msg": { "cache_control": "no-cache", "changed": false, "connection": "close", "content_length": "2011&qu
"msg": {
"cache_control": "no-cache",
"changed": false,
"connection": "close",
"content_length": "2011",
"content_type": "application/json",
"cookies": {},
"cookies_string": "",
"date": "Sun, 05 Jul 2020 13:32:42 GMT",
"elapsed": 0,
"failed": false,
"json": {
"active": {
"AES": "redactedredactedredacted-redacted-redacted",
"HS256": "redactedredactedredacted-redacted-redacted",
"RS256": "redactedredactedredacted-redacted-redacted"
},
"keys": [
{
"algorithm": "HS256",
"kid": "redactedredactedredacted-redacted-redacted",
"providerId": "redactedredactedredacted-redacted-redacted",
"providerPriority": 100,
"status": "ACTIVE",
"type": "OCT"
},
{
"algorithm": "AES",
"kid": "redactedredactedredacted-redacted-redacted",
"providerId": "redactedredactedredacted-redacted-redacted",
"providerPriority": 100,
"status": "ACTIVE",
"type": "OCT"
},
{
"algorithm": "RS256",
"certificate": "redactedredactedredacted-redacted-redactedredactedredactedredacted-redacted-redactedredactedredactedredacted-redacted-redactedredactedredactedredacted-redacted-redactedredactedredactedredacted-redacted-redactedredactedredactedredacted-redacted-redactedredactedredactedredacted-redacted-redactedredactedredactedredacted-redacted-redactedredactedredactedredacted-redacted-redactedredactedredactedredacted-redacted-redactedredactedredactedredacted-redacted-redactedredactedredactedredacted-redacted-redacted=",
"kid": "redactedredactedredacted-redacted-redacted",
"providerId": "redactedredactedredacted-redacted-redacted",
"providerPriority": 100,
"publicKey": "thisISApublicKEYthisISApublicKEYthisISApublicKEYthisISApublicKEYthisISApublicKEYthisISApublicKEYthisISApublicKEYthisISApublicKEYthisISApublicKEYthisISApublicKEYthisISApublicKEYthisISApublicKEYthisISApublicKEYthisISApublicKEYthisISApublicKEYthisISApublicKEYthisISApublicKEYthisISApublicKEY",
"status": "ACTIVE",
"type": "RSA"
}
]
},
"msg": "OK (2011 bytes)",
"redirected": false,
"status": 200,
"strict_transport_security": "max-age=31536000; includeSubDomains",
"url": "http://localhost:8080/auth/admin/realms/master/keys",
"x_content_type_options": "nosniff",
"x_frame_options": "SAMEORIGIN",
"x_xss_protection": "1; mode=block"
}
}
。。。我希望公钥从中生成JWK。假设我正在使用API响应对象中的变量kc_keys
"{{ kc_keys.json['keys'] | map(attribute='publicKey') }}"
。。。返回“msg”:“
好的,这是一个物体。我可以通过“{{kc_keys.json['keys']|map(attribute='publicKey')|list}}”
将其转换为一个列表,它返回
"msg": "[AnsibleUndefined, AnsibleUndefined, 'thisISApublicKEYthisISApublicKEYthisISApublicKEYthisISApublicKEYthisISApublicKEYthisISApublicKEYthisISApublicKEYthisISApublicKEYthisISApublicKEYthisISApublicKEYthisISApublicKEYthisISApublicKEYthisISApublicKEYthisISApublicKEYthisISApublicKEYthisISApublicKEYthisISApublicKEYthisISApublicKEY']"
这里的文档不清楚如何将我的
ThisisPublicKey…
提取为字符串。如何从上面的最后一步中获取列表中的最后一个元素作为字符串?如果您只是在寻找publicKey
,这个简单的json\u查询过滤器应该可以
-调试:
msg:{kc_keys.json | json_查询('*[].publicKey')}
替代方案
-调试:
msg:{kc_keys.json['keys']| selectattr('publicKey','defined')| map(attribute='publicKey')| list}
在这里找到了答案:
|最后一个
是我缺少的过滤器。所以根据公认的答案
“{kc|keys.json['keys']| selectattr('publicKey','defined')| map(attribute='publicKey')| list | last | to_json}}”
根据需要返回一个转义的json字符串,| list | last | string
如果您愿意:
“msg”:“\”这是公钥这是公钥这是公钥这是公钥这是公钥这是公钥这是公钥这是公钥这是公钥这是公钥这是公钥这是公钥这是公钥这是公钥这是公钥这是公钥这是公钥这是公钥这是公钥这是公钥”}
基于对类似问题的另一个回答,我尝试了json_query
,但似乎使用它需要jmespath,我希望避免依赖运行ansible,尽管这似乎可行。Ansible本机答案在我自己的回答中:当然,如果最后一项始终具有公钥,last
将起作用。刚刚添加了一个备选方案,以防出现问题。很好地调用了defined
,添加了该选项!