Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

478
Views
Ansible: establezca el hecho para la clave de dict en función del valor de la subclave

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_value

Mi 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".

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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 :

  1.  item: key: FIRST value: some_key: first_value
  2.  item: key: SECOND value: some_key: second_value

Entonces, 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_output

Eso 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
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!