# loop using only the top items in a data structure.

**URL:** <https://forum.ansible.com/t/loop-using-only-the-top-items-in-a-data-structure/29975>\
**Category:** Ansible Project\
**Tags:** kubernetes\
**Created:** [March 25, 2019, 3:24pm UTC](https://forum.ansible.com/t/loop-using-only-the-top-items-in-a-data-structure/29975 "2019-03-25T15:24:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Steve\_Button](https://avatars.discourse-cdn.com/v4/letter/s/b5a626/32.png) [@Steve\_Button](https://forum.ansible.com/u/Steve_Button)\
**Post date:** [March 25, 2019, 3:24pm UTC](https://forum.ansible.com/t/loop-using-only-the-top-items-in-a-data-structure/29975/1 "2019-03-25T15:24:09Z")

</div>

Hi,

Bit of a confusing one, and perhaps I’m barking up the wrong tree (which is why I’ve turned here after some research) .

I’m trying to create a bunch of kubernetes namespaces using a template, have got that working fine, but now I want to add in different default limit and request values for each namesapce.

Initially, I just had a list along these lines :-

namespaces:

- si

- qa

- dev

- build

- core-qa

- core-si

But now, I’ve tried to be clever and add some defaults below the build namespace :-

namespaces:

- si
- qa
- dev
- build:  
spec:  
limits:
- default:  
cpu: 100m  
memory: 256Mi  
defaultRequest:  
cpu: 50m  
memory: 128Mi
- core-qa
- core-si

these get used later on in my playbook (not able to test that bit yet)

Here’s where I simply create a tmp file for use later on…

- name: Env Namespaces | create template in /tmp  
tags: namespaces  
template:  
src: namespace.j2  
dest: /tmp/{{ item }}.yaml  
loop: “{{ namespaces }}”

However this breaks badly, as it tries to use the whole data structure for the filename, including the default cpu, memory, request, etc.

I want something like this…

- name: Env Namespaces | create template in /tmp  
tags: namespaces  
template:  
src: namespace.j2  
dest: /tmp/{{ item[top\_level\_only\_here] }}.yaml  
loop: “{{ namespaces }}”

Is Ansible able to handle this for me??

Once I’ve got that working, I’ll need to get it to only set the limits for certain namespaces and leave others without any limits. Scratching my head how to achieve that as well!?

Thanks,

Steve

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [March 25, 2019, 5:58pm UTC](https://forum.ansible.com/t/loop-using-only-the-top-items-in-a-data-structure/29975/2 "2019-03-25T17:58:13Z")

</div>

You need to be consistent, either have a list, list of dict or a dict.  
Easiest way is to use a list of dict.

namespaces:  
&nbsp;&nbsp;- name: si  
&nbsp;&nbsp;- name: qa  
&nbsp;&nbsp;- name: dev  
&nbsp;&nbsp;- name: build  
&nbsp;&nbsp;&nbsp;&nbsp;spec: \<something\>  
&nbsp;&nbsp;&nbsp;&nbsp;limits:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;default:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cpu: 100m  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memory: 256Mi  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;defaultRequest:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cpu: 50m  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memory: 128Mi  
&nbsp;&nbsp;- name: core-qa  
&nbsp;&nbsp;- name: core-si

Then you can loop like this

- name: Env Namespaces | create template in /tmp  
&nbsp;&nbsp;tags: namespaces  
&nbsp;&nbsp;template:  
&nbsp;&nbsp;&nbsp;&nbsp;src: namespace.j2  
&nbsp;&nbsp;&nbsp;&nbsp;dest: /tmp/{{ item.name }}.yaml  
&nbsp;&nbsp;loop: "{{ namespaces }}"
