Last active
October 31, 2019 09:51
-
-
Save halberom/c14924c923305d3671f51a7d2221cbc1 to your computer and use it in GitHub Desktop.
ansible - example of selecting from list of dicts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
- hosts: localhost | |
gather_facts: false | |
connection: local | |
vars: | |
nvidia_cards_and_drivers: | |
# Comment the following list item to see the difference | |
- model: "Quadro FX 5800" | |
driver_version: "340xx" | |
- model: "Quadro 4000" | |
driver_version: "390xx" | |
tasks: | |
- name: select driver version for model | |
set_fact: | |
driver_ver: "{{ nvidia_cards_and_drivers|selectattr('model', 'equalto', 'Quadro FX 5800')|map(attribute='driver_version')|list }}" | |
- name: do something with it | |
debug: | |
msg: "{{ driver_ver if driver_ver else 'needs latest drv' }}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# With list item commented | |
PLAY [localhost] ************************************************************************************************************************************************************************************************************************************************************************************************************* | |
TASK [select driver version for model] *************************************************************************************************************************************************************************************************************************************************************************************** | |
ok: [localhost] | |
TASK [do something with it] ************************************************************************************************************************************************************************************************************************************************************************************************** | |
ok: [localhost] => { | |
"msg": [ | |
"340xx" | |
] | |
} | |
PLAY RECAP ******************************************************************************************************************************************************************************************************************************************************************************************************************* | |
localhost : ok=2 changed=0 unreachable=0 failed=0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# With list item uncommented | |
PLAY [localhost] ************************************************************************************************************************************************************************************************************************************************************************************************************* | |
TASK [select driver version for model] *************************************************************************************************************************************************************************************************************************************************************************************** | |
ok: [localhost] | |
TASK [do something with it] ************************************************************************************************************************************************************************************************************************************************************************************************** | |
ok: [localhost] => { | |
"msg": "needs latest drv" | |
} | |
PLAY RECAP ******************************************************************************************************************************************************************************************************************************************************************************************************************* | |
localhost : ok=2 changed=0 unreachable=0 failed=0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment