lookup plugin and conditionals

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

Thanks,
Hank

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

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'`` }

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]

or just: firmware[Model].idrac

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

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