Skip to content

Instantly share code, notes, and snippets.

@halberom
Last active October 31, 2019 09:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save halberom/c14924c923305d3671f51a7d2221cbc1 to your computer and use it in GitHub Desktop.
Save halberom/c14924c923305d3671f51a7d2221cbc1 to your computer and use it in GitHub Desktop.
ansible - example of selecting from list of dicts
---
- 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' }}"
# 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
# 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