Ansible在寄存器循环中获取唯一值
Ansible get unique value in loop for register
嗨,朋友,下面是我从注册器名称 dataInfo 中输出的示例
ok: [123.23.44.123] => {
"msg": [
{
"changed": true,
"item": [
{
"artifactName": "helloWorld.jar",
"status": "false"
},
"myGroup1"
],
"rc": 0,
"stderr_lines": [],
"stdout": "ok",
"stdout_lines": [
"ok"
]
},
{
"changed": true,
"item": [
{
"artifactName": "helloWorld.jar",
"status": "false"
},
"myGroup2"
],
"rc": 0,
"stderr_lines": [],
"stdout": "ok",
"stdout_lines": [
"ok"
]
}
下面是我的代码
- name: My ArtifactName Name
debug:
msg: "artifactName = {{ item.item[0].artifactName }}"
loop: "{{ dataInfo.results }}"
when: item.changed | bool == true and item.stdout == "ok"
怎么只能显示如下
artifactName = helloWorld.jar
而不是
artifactName = helloWorld.jar
artifactName = helloWorld.jar
我已经分享了我的代码,如上所示。请指教
当您使用 dataInfo.results
循环时,debug
消息将针对每个“项目”重复。如果你只想要唯一的 artifactName
,那么你可以使用 json_query
将它保存到一个变量中,并选择只显示 unique
个。
像这样:
# Save list of artifactName found in dataInfo.results even with duplicates
- name: save artifact names
set_fact:
artifact_names: "{{ artifact_names | default([]) + [ item | json_query('item[].artifactName') ] }}"
loop: "{{ dataInfo.results }}"
# Show each item of artifact_names using unique filter to eliminate duplicates
- name: show unique artifact names
debug:
var: item
loop: "{{ artifact_names | flatten | unique }}"
更新:
低于set_fact
:
artifact_names: "{{ artifact_names | default([]) + [ item.item[0].artifactName ] }}"
...如果你在 item
中只得到 1 个散列就可以工作,像这样:
"item": [
{
"artifactName": "helloWorld.jar",
"status": "false"
},
],
但是如果它可能有多个像下面这样的哈希值,它将不起作用:
"item": [
{
"artifactName": "helloWorld.jar",
"status": "false"
},
{
"artifactName": "someother.jar",
"status": "false"
},
...
],
如果有可能,则应使用 json_query
。它可以像 jq
命令一样使用。
嗨,朋友,下面是我从注册器名称 dataInfo 中输出的示例
ok: [123.23.44.123] => {
"msg": [
{
"changed": true,
"item": [
{
"artifactName": "helloWorld.jar",
"status": "false"
},
"myGroup1"
],
"rc": 0,
"stderr_lines": [],
"stdout": "ok",
"stdout_lines": [
"ok"
]
},
{
"changed": true,
"item": [
{
"artifactName": "helloWorld.jar",
"status": "false"
},
"myGroup2"
],
"rc": 0,
"stderr_lines": [],
"stdout": "ok",
"stdout_lines": [
"ok"
]
}
下面是我的代码
- name: My ArtifactName Name
debug:
msg: "artifactName = {{ item.item[0].artifactName }}"
loop: "{{ dataInfo.results }}"
when: item.changed | bool == true and item.stdout == "ok"
怎么只能显示如下
artifactName = helloWorld.jar
而不是
artifactName = helloWorld.jar
artifactName = helloWorld.jar
我已经分享了我的代码,如上所示。请指教
当您使用 dataInfo.results
循环时,debug
消息将针对每个“项目”重复。如果你只想要唯一的 artifactName
,那么你可以使用 json_query
将它保存到一个变量中,并选择只显示 unique
个。
像这样:
# Save list of artifactName found in dataInfo.results even with duplicates
- name: save artifact names
set_fact:
artifact_names: "{{ artifact_names | default([]) + [ item | json_query('item[].artifactName') ] }}"
loop: "{{ dataInfo.results }}"
# Show each item of artifact_names using unique filter to eliminate duplicates
- name: show unique artifact names
debug:
var: item
loop: "{{ artifact_names | flatten | unique }}"
更新:
低于set_fact
:
artifact_names: "{{ artifact_names | default([]) + [ item.item[0].artifactName ] }}"
...如果你在 item
中只得到 1 个散列就可以工作,像这样:
"item": [
{
"artifactName": "helloWorld.jar",
"status": "false"
},
],
但是如果它可能有多个像下面这样的哈希值,它将不起作用:
"item": [
{
"artifactName": "helloWorld.jar",
"status": "false"
},
{
"artifactName": "someother.jar",
"status": "false"
},
...
],
如果有可能,则应使用 json_query
。它可以像 jq
命令一样使用。