Using variables in the Jinja2 template in ansible Playbook - scripting

Any idea of how we can use dynamic variables inside the Jinja2 Template. Below is the data from my Jinja2 template.
oracle.install.asm.diskGroup.disks=/dev/oracleasm/disks/DATA,/dev/oracleasm/disks/ARCH,/dev/oracleasm/disks/OCR
The variable values in the defaults/main.yml is:
asm_disk_detail:
- { name: 'OCR', path: '/dev/sde1' }
- { name: 'DATA', path: '/dev/sdf1' }
- { name: 'ARCH', path: '/dev/sdg1' }
I am trying to use these variable values to pass dynamically at the time of running the playbook. These values should automatically get populated in the template.

Yes, this is possible. The main.yml will be sourced automatically when the ansible role is invoked. You just have to write a jinja2 template file for the same.
For example the below file:
A better representation of the main.yml file would be
---
asm_disk_detail:
- name: OCR
path: "/dev/sde1"
- name: DATA
path: "/dev/sdf1"
- name: ARCH
path: "/dev/sdg1"
jinja2 template: supports for loop so you can apply with_items on the above variable asm_disk_detail and create a config file as needed.
Note:- Please try the jinja2 file creation from your side in case any issues please shout :)
===============Play and jinja2 template
playbook-->
---
- name: test
hosts: localhost
tasks:
- name: test
include_vars: vars.yml
- name: jinja2
template:
src: template/template.yml
dest: target/target.yml
jinja2-->
{%- for item in asm_disk_detail -%}
{%- if not loop.last -%}
{{ item.path }}/{{ item.name }},
{%- else -%}
{{ item.path }}/{{ item.name }}
{%- endif -%}
{%- endfor -%}
output-->
oracle.install.asm.diskGroup.disks=/dev/sde1/OCR,/dev/sdf1/DATA,/dev/sdg1/ARCH

Use Ansible template module with a For loop in your template.
{% for disk in asm_disk_detail %}
disk name: {{ disk.name}}
disk path: {{ disk.path }}
{% endfor %}

Related

Is there a way to filter out purchases where transaction ID is (not set)?

I'm working in a GA4 account for someone using a shopify store and am trying to filter out purchases that don't have IDs (i.e. transaction ID is (not set)) from being sent to analytics/counted as revenue, and am wondering if there's a way to do that with liquid in the checkout page. Here's the purchase event as is:
window.dataLayer = window.dataLayer || [];
dataLayer.push({ ecommerce: null });
{% if first_time_accessed %}
dataLayer.push({
event: "purchase",
ecommerce: {
transaction_id: "{{ order.order_number }}",
value: {{ total_price | times: 0.01 }},
tax: {{ tax_price | times: 0.01 }},
shipping: {{ shipping_price | times: 0.01 }},
currency: "{{ order.currency }}",
items: [
{% for line_item in line_items %}{
item_id: "{{ line_item.product_id }}",
item_name: "{{ line_item.title | remove: "'" | remove: '"' }}",
currency: "{{ order.currency }}",
price: {{ line_item.final_price | times: 0.01 }},
quantity: {{ line_item.quantity }}
},{% endfor %}
]
}
});
{% endif %}
Thanks for any guidance!

Bitnami ASP.NET Core Helm chart does not handle ingress extraHosts correctly

The bitnami ASP.NET Core helm chart in version 3.1.18 are not correct. If configuring extraHosts the ingress configuration is incorrect
Input:
[...]
ingress:
enabled: true
selfSigned: false
ingressClassName: nginx
hostname: apigateway.hcv-cluster-local.net
servicePort: http
tls: true
selfsigned: false
annotations:
nginx.ingress.kubernetes.io/backend-protocol: "HTTPS"
#certmanager.k8s.io/issuer: "letsencrypt-prod"
#certmanager.k8s.io/acme-challenge-type: http01
pathType: ImplementationSpecific
extraHosts:
- name: apigateway.hcv-cluster-local.net
path: /api
extraTls:
- hosts:
- apigateway.hcv-cluster-local.net
secretName: apigateway.local-tls
secrets:
- name: apigateway.local-tls
certificate: |-
---- BEGIN CERT...
key: |-
---- BEGIN RSA...
The "helm template" than fails with 'nil pointer evaluating interface {}' on extraPaths. Furthermore the ingress uses old names for the service link. Therefore the ingress.yaml in asp.net need to be adjusted. Seems to be a bug in the bitnami asp.net core chart:
Corrected version:
{{- if .Values.ingress.enabled -}}
apiVersion: {{ include "common.capabilities.ingress.apiVersion" . }}
kind: Ingress
metadata:
name: {{ include "aspnet-core.fullname" . }}
labels: {{- include "common.labels.standard" . | nindent 4 }}
{{- if .Values.commonLabels }}
{{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
{{- end }}
annotations:
{{- if .Values.ingress.certManager }}
kubernetes.io/tls-acme: "true"
{{- end }}
{{- if .Values.ingress.annotations }}
{{- include "common.tplvalues.render" ( dict "value" .Values.ingress.annotations "context" $) | nindent 4 }}
{{- end }}
{{- if .Values.commonAnnotations }}
{{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
{{- end }}
spec:
{{- if and .Values.ingress.ingressClassName (include "common.ingress.supportsIngressClassname" .) }}
ingressClassName: {{ .Values.ingress.ingressClassName | quote }}
{{- end }}
rules:
{{- if .Values.ingress.hostname }}
- host: {{ .Values.ingress.hostname }}
http:
paths:
### HCV Start
{{- if .Values.ingress.extraPaths }}
{{- toYaml .Values.ingress.extraPaths | nindent 10 }}
{{- end }}
### HCV End
- path: {{ .Values.ingress.path }}
{{- if eq "true" (include "common.ingress.supportsPathType" .) }}
pathType: {{ .Values.ingress.pathType }}
{{- end }}
backend: {{- include "common.ingress.backend" (dict "serviceName" (include "aspnet-core.fullname" .) "servicePort" "http" "context" $) | nindent 14 }}
{{- end }}
{{- range .Values.ingress.extraHosts }}
- host: {{ .name }}
http:
paths:
### HCV Start remove extrapaths
### HCV End
### HCV Start
- path: {{ default "/" .path }}
{{- if eq "true" (include "common.ingress.supportsPathType" $) }}
pathType: {{ default "ImplementationSpecific" .pathType }}
{{- end }}
backend: {{- include "common.ingress.backend" (dict "serviceName" (include "aspnet-core.fullname" $) "servicePort" "http" "context" $) | nindent 14 }}
### HCV End
{{- end }}
{{- if or .Values.ingress.tls .Values.ingress.extraTls .Values.ingress.hosts }}
tls:
{{- if .Values.ingress.tls }}
- hosts:
- {{ .Values.ingress.hostname }}
secretName: {{ printf "%s-tls" .Values.ingress.hostname }}
{{- end }}
{{- if .Values.ingress.extraTls }}
{{- toYaml .Values.ingress.extraTls | nindent 4 }}
{{- end }}
{{- end }}
{{- end }}
I loaded an extracted chart in my service, but I think it need to be corrected... Maybe someone could verify...

Different images for different languages in multilingual stores without 3rd party software

As above, I hope that visitors who select different stores can see different images. I have all the text translated and I don't want to rely on 3rd party app. I have come across this thread: https://community.shopify.com/c/technical-q-a/how-can-upload-different-images-based-on-the-different-store/m-p/1007146
There is one reply that mentioned this is possible; but I can't find any other resources on the matter above. Appreciate if anyone can guide me through on the process.
on code is not really hard, but is boring to maintain.
you should upload your images by settings/files.
on the folder Locales of your theme you have the languages JSON files. Create inside an object "images" and there create the keys you need, and set as value the link you have from step 1.
On your template use liquid translation to translate the image address it will manage to change the file address. done!
EX: on pseudo code:
file: locales/en.default.json
{
"images": {
"sliders": {
"1": "slider_1_en.jpg",
"2": "slider_2_en.jpg"
}
}
// .... rest of the file
}
file: locales/es.json
{
"images": {
"sliders": {
"1": "slider_1_es.jpg",
"2": "slider_2_es.jpg"
}
// .... rest of the file
}
IMPORTANT: save the name of the file including the extension. slider_1_es not works. It should be slider_1_es.jpg.
Do the same on all the languages you use
Now on any liquid file were you need the translated image, you can do a simple image:
<img
src="{{ 'images.sliders.1' | t | file_img_url: '1500x' }}"
loading="lazy"
>
Or even somethin more complex using srcset:
<img
srcset="
{{ 'images.sliders.1' | t | file_img_url: '375x' }} 375w,
{{ 'images.sliders.1' | t | file_img_url: '750x' }} 750w,
{{ 'images.sliders.1' | t | file_img_url: '1100x' }} 1100w,
{{ 'images.sliders.1' | t | file_img_url: '1500x' }} 1500w,
{{ 'images.sliders.1' | t | file_img_url: '1780x' }} 1780w,
{{ section.settings.image | img_url: '2000x' }} 2000w
"
sizes="(min-width: 750px) calc(100vw - 22rem), 1100px"
src="{{ 'images.sliders.1' | t | file_img_url: '1500x' }}"
loading="lazy"
>

App Mesh doesn't inject Envoy proxy to pod

I'm integrating app mesh to microservices on EKS. I have deployed the microservices and app mesh controller and labelled my workload namespace with the sidecar injector webhook, but the app mesh controller throws a reconcile error.
I have been working on this for a while, and I haven't figured why I get the error. I will appreciate help from anyone who has implemented this successfully.
Errors I'm getting.
"level":"error","ts":1633349081.3646858,"logger":"controller-runtime.controller","msg":"Reconciler error","controller":"virtualnode","request":"myapp/oidctokenconverter","error":"failed to resolve virtualServiceRef: unable to fetch virtualService: myapp/identity-oidctokenconverter: VirtualService.appmesh.k8s.aws \"oidctokenconverter\" not found","errorVerbose":"VirtualService.appmesh.k8s.aws \"oidctokenconverter\" not found\nunable to fetch virtualService
{{- if .Values.global.appMesh.enabled -}}
apiVersion: appmesh.k8s.aws/v1beta2
kind: VirtualNode
metadata:
name: {{ template "myapp.fullname" . }}
namespace: myapp
labels:
{{ include "myapp.match-labels" . | indent 4 }}
{{ include "myapp.pod-extra-labels" . | indent 4 }}
spec:
awsName: {{ template "myapp.fullname" . }}
meshName: myapp-mesh
podSelector:
matchLabels:
app: {{ template "myapp.name" . }}
release: {{ .Release.Name }}
listeners:
- portMapping:
port: 8080
protocol: http
backends:
- virtualService:
virtualServiceRef:
name: {{ template "myapp.fullname" . }}
namespace: myapp
serviceDiscovery:
dns:
hostname: {{ template "myapp.fullname" . }}.{{ .Release.Namespace }}.svc.cluster.local
{{- end }}
{{- if .Values.global.appMesh.enabled -}}
apiVersion: appmesh.k8s.aws/v1beta2
kind: VirtualService
metadata:
name: {{ template "myapp.fullname" . }}
namespace: myapp
labels:
{{ include "myapp.match-labels" . | indent 4 }}
{{ include "myapp.pod-extra-labels" . | indent 4 }}
spec:
meshName: myapp-mesh
awsName: {{ template "myapp.fullname" . }}.{{ .Release.Namespace }}.svc.cluster.local
provider:
virtualNode:
virtualNodeRef:
name: {{ template "myapp.fullname" . }}
{{- end }}

Directly access nested linklist in shopify

I have a my menu navigation set up like this in shopify:
- main_menu
- top_level_1
- top_level_2
- top_level_2_sub_menu_1
- top_level_2_sub_menu_2
- top_level_2_sub_menu_3
I am able to do this to iterate through all the menu items:
{% for link in linklists.main-menu.links %}
<p>{{ link.handle }}</p>
{% for nestedLink in link.links %}
<p>{{ nestedLink.handle }}</p>
{% endfor %}
{% endfor %}
However I don't seem to be able to iterate JUST through the submenus belonging to top_level_2. I have tried this with no success:
{% for link in linklists.main-menu.top_level_2.links %}
<p>{{ link.handle }}</p>
{% endfor %}
And also tried this:
{% for link in linklists.top_level_2.links %}
<p>{{ link.handle }}</p>
{% endfor %}
Is it possible to directly get a handle to linklist in order to iterate through its child links? Or would I need to first find that link by searching the top level items?
linklists.main-menu.links returns an array of object items.
A.k.a:
[
{
active: false
child_active: false
current: false
child_current: false
levels: 1
links: [...]
object: null
title: "Some title"
type: "http_link"
url: "#:
},
{
active: false
child_active: false
current: false
child_current: false
levels: 1
links: [...]
object: null
title: "Some title"
type: "http_link"
url: "#:
},
{
active: false
child_active: false
current: false
child_current: false
levels: 1
links: [...]
object: null
title: "Some title"
type: "http_link"
url: "#:
},
etc...
]
So you can't say linklists.main-menu.top_level_2 since there is no such object.
You can say linklists.main-menu.links[1].links and do something like so:
{%- for link in linklists.main-menu.links[1].links -%}
{%- endfor -%}
But please note that this will loop only the items in the second navigation item and ignore the rest.