var loaded from yaml file, and working with resulting data structure

19 views
Skip to first unread message

Willard Dennis

unread,
Nov 1, 2018, 11:47:43 PM11/1/18
to Ansible Project
Hi all,

I am working on an idea for an Ansible role that installs CUDA, where I want to have a var consisting of a list of items with multiple key/value pairs (some values being lists of items.) So I came up with this YAML data structure, in a file I named cuda_versions.yml:
---
cuda_versions:
- cuda_8_0:
    cuda_ver: 8.0.61
    min_driver_ver: 375.26
    patchset_url_list:

- cuda_9_0:
    cuda_ver: 9.0.176
    min_driver_ver: 384.81
    patchset_url_list:

- cuda_9_1:
    cuda_ver: 9.1.85
    min_driver_ver: 387.26
    patchset_url_list:

- cuda_9_2:
    cuda_ver: 9.2.148
    min_driver_ver: 396.37
    patchset_url_list:

- cuda_10_0:
    cuda_ver: 10.0.130
    min_driver_ver: 410.48
    patchset_url_list:


Which when turned into an Ansible var via include_vars: ./cuda_versions.yml has the structure:
"cuda_versions": [
    {
        "cuda_8_0": {
            "cuda_ver": "8.0.61",
            "min_driver_ver": 375.26,
            "patchset_url_list": [
            ]
        }
    },
    {
        "cuda_9_0": {
            "cuda_ver": "9.0.176",
            "min_driver_ver": 384.81,
            "patchset_url_list": [
            ]
        }
    },
    {
        "cuda_9_1": {
            "cuda_ver": "9.1.85",
            "min_driver_ver": 387.26,
            "patchset_url_list": [
            ]
        }
    },
    {
        "cuda_9_2": {
            "cuda_ver": "9.2.148",
            "min_driver_ver": 396.37,
            "patchset_url_list": [
            ]
        }
    },
    {
        "cuda_10_0": {
            "cuda_ver": "10.0.130",
            "min_driver_ver": 410.48,
            "patchset_url_list": null
        }
    }
]

Now I am stuck trying to get a value from a given key for a given item key in Ansible. For example, how would one get the "installer_url" value for the "cuda_9_0" item key from this var in Ansible? I tried a few things, but none of them have worked, and this is the most complicated var list that I've yet worked with :)

Or, is there a better way to structure my YAML so that it produces a better (more Ansible-friendly, easier to work with) data structure?

Kai Stian Olstad

unread,
Nov 2, 2018, 1:22:20 AM11/2/18
to ansible...@googlegroups.com
Instead of making cuda_versions a list, make is a dict.

---
cuda_versions:
> Now I am stuck trying to get a value from a given key for a given item
> key
> in Ansible. For example, how would one get the "installer_url" value
> for
> the "cuda_9_0" item key from this var in Ansible? I tried a few things,
> but
> none of them have worked, and this is the most complicated var list
> that
> I've yet worked with :)
>
> Or, is there a better way to structure my YAML so that it produces a
> better
> (more Ansible-friendly, easier to work with) data structure?

With cuda_version as a dict and not a list you can use this

{{ cuda_versions.cuda_9_0.installer_url }}

--
Kai Stian Olstad

Willard Dennis

unread,
Nov 2, 2018, 11:01:03 AM11/2/18
to Ansible Project
Sometimes it's the little things... thanks!
Reply all
Reply to author
Forward
0 new messages