loop using only the top items in a data structure.

8 views
Skip to first unread message

Steve Button

unread,
Mar 25, 2019, 11:24:09 AM3/25/19
to Ansible Project
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


Kai Stian Olstad

unread,
Mar 25, 2019, 1:58:30 PM3/25/19
to ansible...@googlegroups.com
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:
- name: si
- name: qa
- name: dev
- name: build
spec: <something>
limits:
default:
cpu: 100m
memory: 256Mi
defaultRequest:
cpu: 50m
memory: 128Mi
- name: core-qa
- name: core-si

Then you can loop like this

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


--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages