# lookup plugin and conditionals

**URL:** <https://forum.ansible.com/t/lookup-plugin-and-conditionals/18845>\
**Category:** Ansible Developer\
**Created:** [July 24, 2015, 5:32pm UTC](https://forum.ansible.com/t/lookup-plugin-and-conditionals/18845 "2015-07-24T17:32:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hank\_Beatty](https://avatars.discourse-cdn.com/v4/letter/h/f6c823/32.png) [@Hank\_Beatty](https://forum.ansible.com/u/Hank_Beatty)\
**Post date:** [July 24, 2015, 5:32pm UTC](https://forum.ansible.com/t/lookup-plugin-and-conditionals/18845/1 "2015-07-24T17:32:21Z")

</div>

Would it be appropriate for a lookup plugin to use ansible.playbook.conditional?

I'm wanting to modify the recursive lookup plugin that has been discussed on this list to allow conditionals per iteration.

the code for the lookup plugin can be found here: [https://gist.github.com/hkariti/d07695b4b9c5a68d8c02](https://gist.github.com/hkariti/d07695b4b9c5a68d8c02)

Thanks,  
Hank

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [July 24, 2015, 10:37pm UTC](https://forum.ansible.com/t/lookup-plugin-and-conditionals/18845/2 "2015-07-24T22:37:06Z")

</div>

What is it you need to achieve?

I wonder if ansible might already have a way of doing what you need, before you go down the route of making custom code.

Jon

---

<div class="post-metadata">

**Author:** ![Hank\_Beatty](https://avatars.discourse-cdn.com/v4/letter/h/f6c823/32.png) [@Hank\_Beatty](https://forum.ansible.com/u/Hank_Beatty)\
**Post date:** [July 25, 2015, 11:15am UTC](https://forum.ansible.com/t/lookup-plugin-and-conditionals/18845/3 "2015-07-25T11:15:17Z")

</div>

Hi Jon,

Thank you for your response.

I’m trying to iterate over a dictionary in the form of:

`firmware:`  
`PowerEdge_730xd:`  
`bios:`  
`url: [http://downloads.dell.com/FOLDER02868051M/1/BIOS_XR23Y_WN64_1.2.10.EXE](http://downloads.dell.com/FOLDER02868051M/1/BIOS_XR23Y_WN64_1.2.10.EXE)`  
`filename: BIOS_XR23Y_WN64_1.2.10.EXE`  
`target_version: 1.2.10`  
`search: none`  
`minimum_version: none`  
`idrac:`  
`url: [http://downloads.dell.com/FOLDER02881013M/1/iDRAC-with-Lifecycle-Controller_Firmware_FM1PC_WN64_2.10.10.10_A00.EXE](http://downloads.dell.com/FOLDER02881013M/1/iDRAC-with-Lifecycle-Controller_Firmware_FM1PC_WN64_2.10.10.10_A00.EXE)`  
`filename: iDRAC-with-Lifecycle-Controller_Firmware_FM1PC_WN64_2.10.10.10_A00.EXE`  
`target_version: 2.10.10.10`  
`search: none`  
`minimum_version: none`  
`PowerEdge_720xd:`  
`bios:`  
`url: [http://downloads.dell.com/FOLDER02868051M/1/BIOS_XR23Y_WN64_1.2.10.EXE](http://downloads.dell.com/FOLDER02868051M/1/BIOS_XR23Y_WN64_1.2.10.EXE)`  
`filename: BIOS_XR23Y_WN64_1.2.10.EXE`  
`target_version: 1.2.10`  
`search: none`  
`minimum_version: none`  
`idrac:`  
`url: [http://downloads.dell.com/FOLDER02881013M/1/iDRAC-with-Lifecycle-Controller_Firmware_FM1PC_WN64_2.10.10.10_A00.EXE](http://downloads.dell.com/FOLDER02881013M/1/iDRAC-with-Lifecycle-Controller_Firmware_FM1PC_WN64_2.10.10.10_A00.EXE)`  
`filename: iDRAC-with-Lifecycle-Controller_Firmware_FM1PC_WN64_2.10.10.10_A00.EXE`  
`target_version: 2.10.10.10`  
`search: none`  
`minimum_version: none`  
`PowerEdge_620xd:`  
`bios:`  
`url: [http://downloads.dell.com/FOLDER02797483M/1/BIOS_CNN4X_WN64_2.5.2.EXE](http://downloads.dell.com/FOLDER02797483M/1/BIOS_CNN4X_WN64_2.5.2.EXE)`  
`filename: BIOS_CNN4X_WN64_2.5.2.EXE`  
`target_version: 2.5.2`  
`search: none`  
`minimum_version: none`

Here is the snippet from the playbook:

`- debug: var=Model`

`- debug: msg={{ item.key }}`  
`when: item.key == 'idrac'`  
`with_recursive:`  
`- { name: dict, args: firmware }`  
`- { name: dict, args: "{{item.value}}" }`

The above outputs:

`TASK: [idrac-firmware | debug var=Model] ************************************** `  
`ok: [<some server>] => {`  
`"var": {`  
`"Model": "PowerEdge_R730xd"`  
`}`  
`}`

`TASK: [idrac-firmware | debug msg={{ item.key }}] ***************************** `  
`skipping: [<some server>] => (item={'key': 'bios', 'value': {'url': '[http://downloads.dell.com/FOLDER02797483M/1/BIOS_CNN4X_WN64_2.5.2.EXE](http://downloads.dell.com/FOLDER02797483M/1/BIOS_CNN4X_WN64_2.5.2.EXE)', 'search': 'none', 'target_version': '2.5.2', 'minimum_version': 'none', 'filename': 'BIOS_CNN4X_WN64_2.5.2.EXE'}})`  
`skipping: [<some server>] => (item={'key': 'os_collector', 'value': {'url': '[http://downloads.dell.com/FOLDER02775623M/1/Diagnostics_Application_5W2KP_WN64_OSC_1.1_X10-00.EXE](http://downloads.dell.com/FOLDER02775623M/1/Diagnostics_Application_5W2KP_WN64_OSC_1.1_X10-00.EXE)', 'search': 'none', 'target_version': 1.1000000000000001, 'minimum_version': 'none', 'filename': 'Diagnostics_Application_5W2KP_WN64_OSC_1.1_X10-00.EXE'}})`  
`skipping: [<some server>] => (item={'key': 'bios', 'value': {'url': '[http://downloads.dell.com/FOLDER02868051M/1/BIOS_XR23Y_WN64_1.2.10.EXE](http://downloads.dell.com/FOLDER02868051M/1/BIOS_XR23Y_WN64_1.2.10.EXE)', 'search': 'none', 'target_version': '1.2.10', 'minimum_version': 'none', 'filename': 'BIOS_XR23Y_WN64_1.2.10.EXE'}})`  
`ok: [<some server>] => (item={'key': 'idrac', 'value': {'url': '[http://downloads.dell.com/FOLDER02881013M/1/iDRAC-with-Lifecycle-Controller_Firmware_FM1PC_WN64_2.10.10.10_A00.EXE](http://downloads.dell.com/FOLDER02881013M/1/iDRAC-with-Lifecycle-Controller_Firmware_FM1PC_WN64_2.10.10.10_A00.EXE)', 'search': 'none', 'target_version': '2.10.10.10', 'minimum_version': 'none', 'filename': 'iDRAC-with-Lifecycle-Controller_Firmware_FM1PC_WN64_2.10.10.10_A00.EXE'}}) => {`  
`"item": {`  
`"key": "idrac",`  
`"value": {`  
`"filename": "iDRAC-with-Lifecycle-Controller_Firmware_FM1PC_WN64_2.10.10.10_A00.EXE",`  
`"minimum_version": "none",`  
`"search": "none",`  
`"target_version": "2.10.10.10",`  
`"url": ["http://downloads.dell.com/FOLDER02881013M/1/iDRAC-with-Lifecycle-Controller_Firmware_FM1PC_WN64_2.10.10.10_A00.EXE"](http://downloads.dell.com/FOLDER02881013M/1/iDRAC-with-Lifecycle-Controller_Firmware_FM1PC_WN64_2.10.10.10_A00.EXE)`  
`}`  
`},`  
`"msg": "idrac"`  
`}`  
`skipping: [<some server>] => (item={'key': 'bios', 'value': {'url': '[http://downloads.dell.com/FOLDER02868051M/1/BIOS_XR23Y_WN64_1.2.10.EXE](http://downloads.dell.com/FOLDER02868051M/1/BIOS_XR23Y_WN64_1.2.10.EXE)', 'search': 'none', 'target_version': '1.2.10', 'minimum_version': 'none', 'filename': 'BIOS_XR23Y_WN64_1.2.10.EXE'}})`  
`ok: [<some server>] => (item={'key': 'idrac', 'value': {'url': '[http://downloads.dell.com/FOLDER02881013M/1/iDRAC-with-Lifecycle-Controller_Firmware_FM1PC_WN64_2.10.10.10_A00.EXE](http://downloads.dell.com/FOLDER02881013M/1/iDRAC-with-Lifecycle-Controller_Firmware_FM1PC_WN64_2.10.10.10_A00.EXE)', 'search': 'none', 'target_version': '2.10.10.10', 'minimum_version': 'none', 'filename': 'iDRAC-with-Lifecycle-Controller_Firmware_FM1PC_WN64_2.10.10.10_A00.EXE'}}) => {`  
`"item": {`  
`"key": "idrac",`  
`"value": {`  
`"filename": "iDRAC-with-Lifecycle-Controller_Firmware_FM1PC_WN64_2.10.10.10_A00.EXE",`  
`"minimum_version": "none",`  
`"search": "none",`  
`"target_version": "2.10.10.10",`  
`"url": ["http://downloads.dell.com/FOLDER02881013M/1/iDRAC-with-Lifecycle-Controller_Firmware_FM1PC_WN64_2.10.10.10_A00.EXE"](http://downloads.dell.com/FOLDER02881013M/1/iDRAC-with-Lifecycle-Controller_Firmware_FM1PC_WN64_2.10.10.10_A00.EXE)`  
`}`  
`},`  
`"msg": "idrac"`  
`}`

What I’m trying to get to is something that will only return the one idrac entry.

Something like:

`- debug: msg={{ item.key }}`  
`with_recursive:`  
`- { name: dict, args: firmware, when: "{{ Model }}" == "{{ item.key }}" }`  
`- { name: dict, args: "{{item.value}}", ``when: "{{ item.key }}" == 'idrac'`` }`

---

<div class="post-metadata">

**Author:** ![Hagai\_Kariti](https://avatars.discourse-cdn.com/v4/letter/h/4491bb/32.png) [@Hagai\_Kariti](https://forum.ansible.com/u/Hagai_Kariti)\
**Post date:** [July 25, 2015, 11:38am UTC](https://forum.ansible.com/t/lookup-plugin-and-conditionals/18845/4 "2015-07-25T11:38:18Z")

</div>

If you only need to use one entry from your dict, you can just do this:

- debug: var=item  
when: item.key == ‘idrac’  
with\_dict: firmware[Model]

---

<div class="post-metadata">

**Author:** ![Hagai\_Kariti](https://avatars.discourse-cdn.com/v4/letter/h/4491bb/32.png) [@Hagai\_Kariti](https://forum.ansible.com/u/Hagai_Kariti)\
**Post date:** [July 25, 2015, 11:38am UTC](https://forum.ansible.com/t/lookup-plugin-and-conditionals/18845/5 "2015-07-25T11:38:56Z")

</div>

or just: firmware[Model].idrac

---

<div class="post-metadata">

**Author:** ![Hank\_Beatty](https://avatars.discourse-cdn.com/v4/letter/h/f6c823/32.png) [@Hank\_Beatty](https://forum.ansible.com/u/Hank_Beatty)\
**Post date:** [July 25, 2015, 11:57am UTC](https://forum.ansible.com/t/lookup-plugin-and-conditionals/18845/6 "2015-07-25T11:57:06Z")

</div>

Thank you so much.

`- debug: var=item`  
`with_dict: firmware[Model].idrac`

`- debug: var=item`  
`when: item.key == 'idrac'`  
`with_dict: firmware[Model]`

`- debug: var=item`  
`when: item.key == 'idrac'`  
`with_dict:`  
`- firmware[Model]`

`- debug: var=item`  
`when: item.key == 'idrac'`  
`with_dict: firmware[{{ Model }}]`

All of the above fail with:

`TASK: [idrac-firmware | debug var=item] *************************************** `  
`fatal: [<some server>] => with_dict expects a dict`

`FATAL: all hosts have already failed -- aborting`

---

<div class="post-metadata">

**Author:** ![Hank\_Beatty](https://avatars.discourse-cdn.com/v4/letter/h/f6c823/32.png) [@Hank\_Beatty](https://forum.ansible.com/u/Hank_Beatty)\
**Post date:** [July 25, 2015, 12:12pm UTC](https://forum.ansible.com/t/lookup-plugin-and-conditionals/18845/7 "2015-07-25T12:12:19Z")

</div>

OK. This does work:

`- debug: var=item`  
`when: item.key == 'idrac'`  
`with_dict: firmware[Model]`

My ‘Model’ variables didn’t match.

Thank you so much for your help.

Have a great day.

Hank
