Looping through a map in helm - yaml

I am new to yaml and helm templating and have a question in it.
I have the following in my values.yaml file
regionInfo:
usa:
DrRegions: uk
region: usa
region_key: usa
uk:
DrRegions: usa
region: uk
region_key: lhr
I need to iterate over the above map and the output should be in this format for each region
{{ $region }}:
dnsAbbrev: {{ region_key }}
abbrev: {{ region_key }}
drRegion: {{ DrRegions }}
I tried the following
Method 1
{{ range $reg, $props := .Values.regionInfo }}
{{ $reg }}:
dnsAbbrev: {{ $props.region_key }}
abbrev: {{ $props.region_key }}
drRegion: {{ $props.DrRegions }}
{{ end }}
Method 2
{{ range reg, props := .Values.regionInfo }}
{{ range $props }}
{{ $reg }}:
dnsAbbrev: {{ .region_key }}
abbrev: {{ .region_key }}
drRegion: {{ .DrRegions }}
{{ end }}
{{ end }}
I was getting couldn't range over map in both the scenarios... Any help or guidance on this is highly appreciated.

Related

How can I write an if statement in resources.Get Hugo?

I have the following code:
{{ with resources.Get .Site.Params.image }}
<meta property="og:image" content="{{ .Permalink }}" />
{{ end }}
I get a image path in the config.toml file. However I now want to be able to overwrite the file by frontmatter data... Something like this (in pseudo code):
{{ if isset .Image }}
{{ $image := .Image }}
{{ else }}
{{ $image := .Site.Params.image }}
{{ end }}
{{ with resources.Get $image }}
...
How can I write this statement? Also if you have good tutorials on these statements and syntax let me know!
Use the or function:
{{ with resources.Get (or .Params.thumbnailImage .Site.Params.ogImage) }}
<meta property="og:image" content="{{ .Permalink }}" />
{{ end }}
EDIT: I updated to #brendan solution.
This is my solution:
{{ $ogImage := "" }}
{{ if .Params.thumbnailImage }}
{{ $ogImage = .Params.thumbnailImage }}
{{ else }}
{{ $ogImage = .Site.Params.ogImage }}
{{ end }}
{{ $ogImageRender := resources.Get $ogImage }}
{{ with $ogImageRender }}
<meta property="og:image" content="{{ .Permalink }}" />
{{ end }}
:= defines variables and set initial value
= set variables to another value

Ansible Nested variables and Jinja2 templates

I'm trying to figure out why my jinja2 template (and ansible for that matter) cannot find my variables in my inventory file.
Here is my inventory file:
all:
hosts:
test05:
ansible_host: 192.168.x.x
filebeat:
version: 7.15.2
applog:
- title: Separate Application Log Path with Tags
type: log
paths:
- /var/log/something/moresomething/current
tags: '["something", "application"]'
- title: Separate Application Log Path, with Tags, and "decode_json_fields" processor.
type: log
paths:
- /var/log/something/moresomething/blah-shell.log
tags: ["application", "something"]
fields: ["message"]
depth: 2
- title: Separate Application Log Path, with Tags, and Multiline fields
type: log
paths:
- /var/log/something/moresomething/production.log
tags: ["application", "something"]
multiline_type: pattern
multiline_patern: 'Started'
multiline_negate: true
multiline_match: after
Then attempting to get the first title. I'm doing the following:
- name: debugging
debug:
var: filebeat.applog.title
when I run this I end up getting filebeat.applog.title: VARIABLE IS NOT DEFINED! which I think is good since it doesn't know what title I want. So changing this to
- name: debugging
debug:
var: filebeat.applog.0.title
I end up getting what I want filebeat.applog.0.title: Separate Application Log Path with Tags. However, how do I use this in a jinja2 template?
I have the following for a template, I know I need to update this to loop through the different items in my inventory. That's a different problem on how to loop through this.
title: {{ filebeat.applog.title }}
- type: {{ filebeat.applog.type }}
enabled: true
paths:
- {{ filebeat.applog.path }}
tags: {{ filebeat.applog.tags }}
{% if filebeat.applog.fields is defined %}
processors:
- decode_json_fields:
fields: {{ filebeat.applog.fields }}
max_depth: {{ filebeat.applog.depth }}
target: {{ filebeat.applog.target | default "" }}
{% endif %}
{% if filebeat.applog.multiline_pattern is defined %}
multiline.type: {{ filebeat.applog.multiline_type }}
multiline.pattern: {{ filebeat.applog.multiline_pattern }}
multiline.negate: {{ filebeat.applog.multiline_negate }}
multiline.match: {{ filebeat.applog.multiline_match }}
{% endif %}
each time I get the following, even when I do use {{ filebeat.applog.0.logtitle }} in the template:
fatal: [test05]: FAILED! => changed=false
msg: |-
AnsibleError: template error while templating string: expected token 'end of print statement', got 'string'. String: title: {{ filebeat.applog.title }}
- type: {{ filebeat.applog.type }}
enabled: true
paths:
- {{ filebeat.applog.path }}
tags: {{ filebeat.applog.tags }}
{% if filebeat.applog.fields is defined %}
processors:
- decode_json_fields:
fields: {{ filebeat.applog.fields }}
max_depth: {{ filebeat.applog.depth }}
target: {{ filebeat.applog.target | default "" }}
{% endif %}
{% if filebeat.applog.multiline_pattern is defined %}
multiline.type: {{ filebeat.applog.multiline_type }}
multiline.pattern: {{ filebeat.applog.multiline_pattern }}
multiline.negate: {{ filebeat.applog.multiline_negate }}
multiline.match: {{ filebeat.applog.multiline_match }}
{% endif %}
I'm not sure what I'm missing or doing wrong. I'm thinking I'm doing something wrong since this the first time doing something like this.
So the template you have should either:
have a for loop to iterate over filebeat.applog
OR
reference n'th element of filebeat.applog
Aside from that, there are some errors like below:
1.
target: {{ filebeat.applog.target | default "" }}
This is the main one, and this is what the error message is complaining about, i.e. got 'string'. The correct usage for default filter is {{ some_variable | default("") }}.
2.
{% if filebeat.applog.multiline_pattern is defined %}
In the inventory this variable is mis-spelled, i.e. multiline_patern (missing one "t"). Fix this in your inventory.
3.
when I do use {{ filebeat.applog.0.logtitle }} in the template
This should be {{ filebeat.applog.0.title }} to work.
Considering the above fixes, a template that loops over filebeat.applog such as below should work:
{% for applog in filebeat.applog %}
title: {{ applog.title }}
- type: {{ applog.type }}
enabled: true
paths: {{ applog.paths }}
tags: {{ applog.tags }}
{% if applog.fields is defined %}
processors:
- decode_json_fields:
fields: {{ applog.fields }}
max_depth: {{ applog.depth }}
target: {{ applog.target | default("") }}
{% endif %}
{% if applog.multiline_pattern is defined %}
multiline.type: {{ applog.multiline_type }}
multiline.pattern: {{ applog.multiline_pattern }}
multiline.negate: {{ applog.multiline_negate }}
multiline.match: {{ applog.multiline_match }}
{% endif %}
{% endfor %}

What are Parse Html token methods for email templates in golang?

I am trying to create email templates having html tokens in golang. I have searched all over the web and found
"html/template"
library. It supports token format like below
Hello {{.Name}}
Confirm email address
But the requirement for html token is something like
Name: {{ test.name }}
Phone: {{ test.phone }}
Address: {{ test.address }}, {{ test.city }}, {{ test.state }} {{ test.zip }}
I could not found such token system in golang or any library supporting such format. Can anyone please tell how can I achieve to create such tokens. There should be no dot before the attribue. either it should be only the attribute like {{Name}} or like {{ test.name }}.
Thank you!
If you can use a $ before attribute names, you can use the template's [with][1] action. Something like:
tmpl :=`
{{ with $test := . }}
Name: {{ $test.Name }}
Phone: {{ $test.Phone }}
Address: {{ $test.Address }}, {{ $test.City }}, {{ $test.State }} {{ $test.Zip }}
{{ end }}
`
Note that each struct field needs to be exported.

Golang iris framework render inside a range

I'm trying to loop over items and render it in separate template, but {{ . }} in item.html contains root variables instead of a single item. I tried to assign item to a variable {{ range $i, $item := .Items }}, but the result is the same.
<!-- list.html -->
{{ range .Items }}
{{ . }} this one is OK
{{ render "item.html" }}
{{ end }}
<!-- item.html -->
{{ . }} this one is not

Ansible: multi line over playbook

I have a question about multi line and Ansible playbook:
I have created a playbook with a line very large, I need to cut this line for better reading. How I can doing it?
- name: 'Create VM Azure :-P '
shell: if ! grep {{ item }} /tmp/vm_{{ rgName }}; then azure vm create --vm-size {{ groups['item'][vmsize] }} --resource-group {{ rgName }} --name {{ item }} --location {{ location }} --admin-username {{ username }} --ssh-publickey-file {{ sshfile }} --storage-account-name {{ rgName | lower }} --os-type {{ groups['item'][type_os] }} --image-urn {{ image }} --data-disk-size {{ disksize }} --subnet-id {{ subnetid_key }} --nic-names {{ item }}; fi
with_items: groups['test']
I would like to make it as follows way, but I have some errors when execute the playbook
- name: 'Create VM Azure :-P '
shell: if ! grep {{ item }} /tmp/vm_{{ rgName }}; then
azure vm create --vm-size {{ groups['item'][vmsize] }}
--resource-group {{ rgName }} --name {{ item }}
--location {{ location }} --admin-username {{ username }}
--ssh-publickey-file {{ sshfile }} --storage-account-name {{ rgName | lower }}
--os-type {{ groups['item'][type_os] }} --image-urn {{ image }}
--data-disk-size {{ disksize }} --subnet-id {{ subnetid_key }}
--nic-names {{ item }}; fi
with_items: groups['test']
The error:
ERROR! Syntax Error while loading YAML.
The error appears to have been in '/home/pvillarruel/docker/azure-ansible/data/playbook.yml': line 79, column 1, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
shell: if ! grep {{ item }} /tmp/vm_{{ rgName }}; then azure vm create --vm-size
{{ groups['item'][vmsize] }} --resource-group {{ rgName }} --name {{ item }}
^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes. Always quote template expression brackets when they
start a value. For instance:
with_items:
- {{ foo }}
Should be written as:
with_items:
- "{{ foo }}"
Thanks
Use a YAML block scalar, they were designed exactly for this kind of thing:
shell: >
if ! grep {{ item }} /tmp/vm_{{ rgName }}; then
azure vm create --vm-size {{ groups['item'][vmsize] }}
--resource-group {{ rgName }} --name {{ item }}
--location {{ location }} --admin-username {{ username }}
--ssh-publickey-file {{ sshfile }}
--storage-account-name {{ rgName | lower }}
--os-type {{ groups['item'][type_os] }} --image-urn {{ image }}
--data-disk-size {{ disksize }} --subnet-id {{ subnetid_key }}
--nic-names {{ item }}; fi
The > indicates that the following block is a scalar and lines should be folded, i.e. a newline character will be converted into a single space. You also do not need to worry about special characters, everything will be taken literally in a folded block scalar.

Resources