Pfoef,描述我手頭的問題非常困難。請多多包涵。
我有這個命令:
my_dict:
FIRST:
some_key: first_value
SECOND:
some_key: second_value
我的 Ansible 任務是:
- shell: "echo {{ item.value['some_key'] }}"
register: exec_output
loop: "{{ my_dict | dict2items }}"
# This is something where I do not know what to do
- set_fact:
desired_output: ???
when: <some_key_contains_a_value>
Ansible執行的時候,會執行兩次shell命令,因為dict里面有2項。
問: 我如何配置 Ansible,以便:如果 some_key 的值是例如“second_value”,Ansible 將設定一個事實并添加鍵(FIRST 或 SECOND)。在本例中,事實將包含“SECOND”。
uj5u.com熱心網友回復:
item當register通過item.item屬性輸出回圈時,您可以找到整個回圈的一部分。
因此,在您的情況下,您會在以下專案中找到exec_output.results:
-
item: key: FIRST value: some_key: first_value -
item: key: SECOND value: some_key: second_value
所以,在此基礎上,你可以有一個劇本,如:
- hosts: localhost
gather_facts: no
tasks:
- shell: "echo {{ item.value.some_key }}"
register: exec_output
loop: "{{ my_dict | dict2items }}"
vars:
my_dict:
FIRST:
some_key: first_value
SECOND:
some_key: second_value
- set_fact:
desired_output: "{{ item.item.key }}"
when: some_key in item.stdout
loop: "{{ exec_output.results }}"
vars:
some_key: second
loop_control:
label: "{{ item.stdout }}"
- debug:
var: desired_output
這會給你預期的結果:
PLAY [localhost] *******************************************************************************************************************
TASK [shell] ***********************************************************************************************************************
changed: [localhost] => (item={'key': 'FIRST', 'value': {'some_key': 'first_value'}})
changed: [localhost] => (item={'key': 'SECOND', 'value': {'some_key': 'second_value'}})
TASK [set_fact] ********************************************************************************************************************
skipping: [localhost] => (item=first_value)
ok: [localhost] => (item=second_value)
TASK [debug] ***********************************************************************************************************************
ok: [localhost] =>
desired_output: SECOND
PLAY RECAP *************************************************************************************************************************
localhost : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/365930.html
上一篇:nextflow:找不到命令
下一篇:df-h給出假資料?
