Ansible Syntax - Checksum deployments - deployment

I am writing a script that will allow my deployments to return a checksum for nexus artifacts deployment. Everything under the when: condition is new to the script. There are variables that aren't defined here, such as checksum, but I put a default filter on it so that Ansible can still use a variable if it can't find a value.
with_indexed_items: app_deployments.keys()|sort
when: |
deploy_results.results[item.0].md5sum|d().local|d()|match(nx_app_deployments[item.1].checksum|d(deploy_results.results[item.0].md5sum)
Unfortunately I keep getting hit with this error (most probably due to syntax).
Error: TASK: [nexusArtifacts | STAGE | Stage deployments] ****************************
fatal: [ari001-app1] => Failed to template {% if deploy_results.results[item.0].md5sum|d().local|d()|match(nx_app_deployments[i tem.1].checksum|d(deploy_results.results[item.0].md5sum) and
stageNexus|d()|bool and
app_deployments[item.1].name is defined and
app_deployments[item.1].active|d(1)|bool and
deploy_toggles|d({})[item.1]|d(1)|bool and
(item in eapDeployList|d(item.1) or 'all' in eapDeployList|d(item)) and
(app != 'springBatch' or
item in sbDeployList|d(item.1)|listParse) and
item in key_list|d([item.1])
%} True {% else %} False {% endif %}: template error while templating string: unexpected "}", expected ")"
FATAL: all hosts have already failed -- aborting
Any help on this would be greatly appreciated. Much thanks!

Your match( is not terminated by )

Related

Build with Jekyll failed because of unknown tag

I fork a blog repository on Github with JekylI and push my article to the _post folder.
I attached the macro code for Jinja2 to the article as below
{% macro -%}
However the github actions "pages build and deployment" is failed because of the build stage threw an exception
Liquid Exception: Liquid syntax error (line 69): Unknown tag 'macro' in
I tried to increment "\" before "%" like this and Jekyll build success
{\% macro -\%}
But "\" is also displayed in the article
I also tried to fork other Jekyll repository, the phenomenon is the same, so I think it is jekyll build problem
Can anyone help me? Thank you very much for any advice
The problem is {} in the code are identified by Jekyll as Liquid code.
Add {% raw %} and {% endraw %} to tell Jekyll not to treat the content as Liquid code
{% raw %}
{% macro -%}
{% endraw %}

How to use Lookup function in Helm Chart

While deploying a Kubernetes application, I want to check if a particular PodSecurityPolicy exists, and if it does then skip installing it again.
I came across the helm lookup function, which allows us to check the existing K8 resources.
While I understand how to use this function to get all the resources of same kind, how do I use this function to check if a PodSecurityPolicy named "myPodSecurityPolicy" exists.
I tried something like this:
{{- if ne (lookup "v1" "PodSecurityPolicy" "" "") "myPodSecurityPolicy"}}
<do my stuff>
{{- end }}
But it doesn't look like I can compare it this way, seeing an error -
error calling ne: invalid type for comparison
Any inputs? Thanks in advance.
Please check your API version and PSP name. Lookup is returning a map or nil not a string and that's why you are getting that error. The following is working for me. For negative expression, just add not after if.
{{- if (lookup "policy/v1beta1" "PodSecurityPolicy" "" "example") }}
<found: do your stuff>
{{- end }}
HTH

How to use salt to test out jinja functionality?

I am looking for a way to test if salt would render a jinja template the way I expect it to.
If I have a file template.sls containing:
{% for usr in ['moe','larry','curly'] %}
{{ usr }}:
user.present
{% endfor %}
Can I run a salt command that will show me the rendered template?
NB: I understand that what's happening is Jinja doing the rendering, and I can template it in python. But I want to ensure that I am using the Jinja version used by salt. AFAIK salt embeds the jinja engine.
An example of what I am looking for is a salt command that would function similarly to the following ansible command:
The following was taken from accepted answer to: How can I test jinja2 templates in ansible?
3_Ansible (using --check)
Create test playbook jinja2test.yml:
---
- hosts: 127.0.0.1
tasks:
- name: Test jinja2template
template: src=test.j2 dest=test.conf
and run it:
ansible-playbook jinja2test.yml --check --diff --connection=local
sample output:
PLAY [127.0.0.1] **************************************************************
GATHERING FACTS ***************************************************************
ok: [127.0.0.1]
TASK: [Test jinja2template] ***************************************************
--- before: test.conf
+++ after: /Users/user/ansible/test.j2
## -0,0 +1,4 ##
+Mike
+Smith
+Klara
+Alex
changed: [127.0.0.1]
PLAY RECAP ********************************************************************
127.0.0.1 : ok=2 changed=1 unreachable=0 failed=0
In order to display the resulting SLS you can use the slsutil.renderer
It's quite easy, and should do the trick.
In my case I'm using it like this:
salt 'test01' slsutil.renderer salt://blabla/test.sls 'jinja|yaml'
Best and simplest way is to use state.show_sls as said here
Please check the official doc here
For your specific case that would be something along this :
salt "your_minion" state.show_sls template.sls

Helm chart throws an error on release, but works fine on --dry-run

So I got an environmental variable in my Helm chart which works fine when I'm running a --dry-run. However, when I'm trying to release it for real an error is thrown.
Because the amount of Postgres nodes differs based on the user's input in values.yaml I need a way to generate the partner nodes variable based on the number of nodes specified.
What I tried to do is create a loop that iterates over the numbers using the until function which returns a list of integers beginning with 0 and ending with $until-1 where I give the postgres_nodes value as input number like so:
- name: "PARTNER_NODES"
value: "{{ range $i, $e := until ( int $.Values.postgres_nodes ) }}{{ if $i }},{{ end }}{{ $.Values.name }}-db-node-{{ $i }}.{{ $.Values.name }}-db{{ end }}"
When ran as helm install --dry-run --debug it works fine and a configuration file gets printed correctly:
"xxx-db-node-0.xxx-db,xxx-db-node-1.xxx-db,xxx-db-node-2.xxx-db"
but when I remove the --dry-run to deploy it for real the following error gets thrown:
Error: release ha-postgres failed: StatefulSet in version "v1beta1" cannot be handled as a StatefulSet: v1beta1.StatefulSet.Spec: v1beta1.StatefulSetSpec.Replicas: readUint32: unexpected character: �, error found in #10 byte of ...|eplicas":"3","servic|..., bigger context ...|-node","namespace":"default"},"spec":{"replicas":"3","serviceName":"boost-db","template":{"metadata"|...
Any help would be much appreciated, and thanks in advance.
Turned out I had my replicas between quotation marks resulting in this error.

How to fail a helm release based on inputs in values.yaml

I'm installing up a helm chart using helm install command. I have values.yaml which takes a few inputs from the user. One of the keys in values.yaml is action, which can only take three predefined values (let's say action1, action2 and action3) as an input. Any other value other than this is invalid.
When a user provides the value to action field in values.yaml and trigger the helm install command, the first thing I need to check is that if the action key has a valid value or not. If the action value is invalid, I want the release to be failed with a proper error message.
e.g.: In case the user has given action: action4, this is not valid and release should fail as .Values.action can only be action1, action2, or action3.
How I can achieve this use case and which file should be best to handle this validation considering the helm structure?
I was able to achieve the use case with below changes.
Added the following code in _helpers.tpl
{{- define "actionValidate" -}}
{{ $action := .Values.actions }}
{{- if or (eq $action "action1") (eq $action "action2") (eq $action "action3") -}}
true
{{- end -}}
{{- end -}}
Invoked this function from a .tpl file like this:-
{{ include "actionValidate" . | required "Action value is incorrect. The valid values are 'action1', 'action2', 'action3' " }}
With HelmV3 there is now an easier way. Just specify a schema that includes your values.
For example:
title: Values
type: object
properties:
action:
description: Some action
type: string
pattern: "^(action1|action2|action3)$"
Not enough rep to comment. However, in response to #Saurabh, you now must YAML comment out the function invocation. Otherwise, Go Template will encounter an invalid return type runtime error from that random true value existing in the middle of the invocation file.
Example
# validation.yaml
---
#{{ include "actionValidate" . | required "Action value is incorrect. The valid values are 'action1', 'action2', 'action3' " }}