List or dictionary search

Hello,

I’m calling on your knowledge because I’m facing a question in my development.
I’ve written a role whose dictionary looks like this:

test_dict:
  valeur1: # "Test_dict.valeur1 dans group_vars/all.yml"
    - test: "une valeur"
    - test2: "une autre"
  valeur2: #"Test_dict.valeur2 dans group_vars/all.yml"
    - test: "une valeur2"
      arg: "un argument"
    - test2: "une autre valeur2"
      arg: "un autre"

test_dict2:
  valeur1: "{{ test_dict.valeur1 }}"
  valeur2: "{{ test_dict.valeur2}}"

I would like to be able to retrieve the content of dict.binaries in a variable of my inventory like this

I search to use 'valeur1 or 2" like a index 0 or 1

How can I review an inventory if needed?
I’m not very good with YAML

Thanks in advance

What exactly do you mean? Sorry I don’t understand the question, what is the result you are after?

1 Like

Sorry, I think I’ve found it
Here’s the structure of my inventory.

test_dict:
  binaire:
    valeur1: # "Test_dict.valeur1 dans group_vars/all.yml"
      - test: "une valeur"
      - test2: "une autre"
    valeur2: #"Test_dict.valeur2 dans group_vars/all.yml"
      - test: "une valeur2"
        arg: "un argument"
      - test2: "une autre valeur2"
        arg: "un autre"

test_dict2:
  valeur1: "{{ test_dict.binaire['valeur1'] }}"
  valeur2: "{{ test_dict.binaire['valeur2'] }}"

one play test

- name: play pour tester les inventaires
  hosts: ma_machine3
  gather_facts: no
  connection: local
  tasks:
    - name: "test_dict"
      debug:
        msg: "{{ test_dict }}"
    - name: "test_dict2.valeur1"
      debug:
        msg: "{{ test_dict2.valeur1 }}"
    - name: "test_dict2.valeur2"
      debug:
        msg: "{{ test_dict2.valeur2 }}"
    - name: Boucle sur test_dict2.valeur2
      debug:
        msg: "{{ item }}"
      loop: "{{ test_dict2.valeur2 }}"

a good result

[alexandre@alma-linux test-inventaire]$ ansible-playbook -i hosts.yml play_test_inventory.yml 

PLAY [play pour tester les inventaires] ******************************************************************************************************

TASK [test_dict] *****************************************************************************************************************************
ok: [ma_machine3] => {
    "msg": {
        "binaire": {
            "valeur1": [
                {
                    "test": "une valeur"
                },
                {
                    "test2": "une autre"
                }
            ],
            "valeur2": [
                {
                    "arg": "un argument",
                    "test": "une valeur2"
                },
                {
                    "arg": "un autre",
                    "test2": "une autre valeur2"
                }
            ]
        }
    }
}

TASK [test_dict2.valeur1] ********************************************************************************************************************
ok: [ma_machine3] => {
    "msg": [
        {
            "test": "une valeur"
        },
        {
            "test2": "une autre"
        }
    ]
}

TASK [test_dict2.valeur2] ********************************************************************************************************************
ok: [ma_machine3] => {
    "msg": [
        {
            "arg": "un argument",
            "test": "une valeur2"
        },
        {
            "arg": "un autre",
            "test2": "une autre valeur2"
        }
    ]
}

TASK [Boucle sur test_dict2.valeur2] *********************************************************************************************************
ok: [ma_machine3] => (item={'test': 'une valeur2', 'arg': 'un argument'}) => {
    "msg": {
        "arg": "un argument",
        "test": "une valeur2"
    }
}
ok: [ma_machine3] => (item={'test2': 'une autre valeur2', 'arg': 'un autre'}) => {
    "msg": {
        "arg": "un autre",
        "test2": "une autre valeur2"
    }
}

PLAY RECAP ***********************************************************************************************************************************
ma_machine3                : ok=4    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Sorry, I must’ve been tired last night

have a nice day

1 Like