# List or dictionary search

**URL:** <https://forum.ansible.com/t/list-or-dictionary-search/11194>\
**Category:** Get Help\
**Created:** [November 27, 2024, 9:07pm UTC](https://forum.ansible.com/t/list-or-dictionary-search/11194 "2024-11-27T21:07:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![alexb81](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/alexb81/32/5126_2.png) [@alexb81](https://forum.ansible.com/u/alexb81)\
**Post date:** [November 27, 2024, 9:07pm UTC](https://forum.ansible.com/t/list-or-dictionary-search/11194/1 "2024-11-27T21:07:31Z")

</div>

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:

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

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [November 27, 2024, 9:13pm UTC](https://forum.ansible.com/t/list-or-dictionary-search/11194/2 "2024-11-27T21:13:06Z")

</div>

> [@alexb81](#):
>
> How can I review an inventory if needed?

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

---

<div class="post-metadata">

**Author:** ![alexb81](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/alexb81/32/5126_2.png) [@alexb81](https://forum.ansible.com/u/alexb81)\
**Post date:** [November 28, 2024, 5:05am UTC](https://forum.ansible.com/t/list-or-dictionary-search/11194/3 "2024-11-28T05:05:12Z")

</div>

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

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

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

```auto
[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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [December 28, 2024, 5:05am UTC](https://forum.ansible.com/t/list-or-dictionary-search/11194/4 "2024-12-28T05:05:48Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
