Ansible - Create a nested List

by
Jeremy Canfield |
Updated: March 15 2023
| Ansible articles
Here is how you can create a nested list. In this example, using the vars plugin, the "food" key is an list and the "fruit" key is a child list as indicated by the bracket characters [ ]. The debug module is used to output the entire list.
---
- hosts: localhost
gather_facts: false
vars:
food:
- fruit:
- apple
- banana
- orange
- grapes
tasks:
- debug:
var: food
...
Which should return the following, were we can see that the "food" key is an list and the "fruit" key is a nested list.
ok: [localhost] => {
"food": [
{
"fruit": [
"apple",
"banana",
"orange",
"grapes"
]
}
]
}
Did you find this article helpful?
If so, consider buying me a coffee over at