Pfoef, describir mi problema en cuestión es bastante difícil. Por favor, tenga paciencia conmigo.
Tengo este dictado:
my_dict: FIRST: some_key: first_value SECOND: some_key: second_valueMi tarea de Ansible es:
- 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>Cuando se ejecuta Ansible, ejecutará el comando de shell dos veces, porque hay 2 elementos en el dict.
P: ¿Cómo puedo configurar Ansible para que: Ansible establezca un hecho y agregue la clave (PRIMERA o SEGUNDA) si el valor de alguna_clave es, por ejemplo, 'segundo_valor'. En este caso de ejemplo, el hecho contendrá "SEGUNDO".
Puede recuperar el item completo que formaba parte de un bucle al register su salida a través de la propiedad item.item .
Entonces, en su caso, encontrará, en los elementos bajo exec_output.results :
item: key: FIRST value: some_key: first_value item: key: SECOND value: some_key: second_valueEntonces, en base a eso, podría tener un libro de jugadas como:
- 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_outputEso te daría el resultado esperado:
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