Home > other >  Ansible - set fact for key of dict based on sub-key value
Ansible - set fact for key of dict based on sub-key value

Time:11-25

Pfoef, describing my issue at hand is quite difficult. Please bear with me.

I have this dict:

my_dict:
  FIRST:
    some_key: first_value
  SECOND:
    some_key: second_value

My Ansible task is:

- 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>

When Ansible executes, it will execute the shell command twice, because there are 2 items in the dict.

Q: How can I configure Ansible so that: Ansible will set a fact and add the key (FIRST or SECOND) if the value of some_key is e.g. 'second_value'. In this example case, the fact will contain "SECOND".

CodePudding user response:

You can find back the whole item that was part of a loop when register'ing its output via the item.item property.

So, in your case, you will find, in the items under exec_output.results:

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

So, based on that you could have a playbook like:

- 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

That would give you the expected result:

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   
  • Related