Jinja templating
Jinja templating can be use in some part of the Squest configuration.
Jinja templating usage with {{ instance }}
as context:
Jinja templating usage with {{ request }}
as context:
Operation job template config (inventory, credentials, tags, limit)
Examples
String with no jinja
Even if the context is sent, a hard coded string can be used without using it.
Jinja string
My hard coded value
Result
My hard coded value
Using the instance as context
Accessing instance name:
Instance context
{
"id" : 31 ,
"state" : "AVAILABLE" ,
"resources" : [],
"billing_group" : null ,
"spoc" : {
"id" : 2 ,
"username" : "admin" ,
"email" : "admin@squest.domain" ,
"profile" : {
"request_notification_enabled" : true ,
"support_notification_enabled" : true ,
"request_notification_filters" : [],
"instance_notification_filters" : []
},
"first_name" : "" ,
"last_name" : "" ,
"is_staff" : true ,
"is_superuser" : true ,
"is_active" : true ,
"billing_groups" : []
},
"name" : "my_instance" ,
"spec" : {
"test" : 2
},
"user_spec" : {},
"date_available" : null ,
"service" : 1
}
Jinja string
My hard coded value with {{ instance.name }}
Result
My hard coded value with my_instance
Accessing instance spec:
Instance context
{
"id" : 31 ,
"state" : "AVAILABLE" ,
"resources" : [],
"billing_group" : null ,
"spoc" : {
"id" : 2 ,
"username" : "admin" ,
"email" : "admin@squest.domain" ,
"profile" : {
"request_notification_enabled" : true ,
"support_notification_enabled" : true ,
"request_notification_filters" : [],
"instance_notification_filters" : []
},
"first_name" : "" ,
"last_name" : "" ,
"is_staff" : true ,
"is_superuser" : true ,
"is_active" : true ,
"billing_groups" : []
},
"name" : "my_instance" ,
"spec" : {
"os" : "linux"
},
"user_spec" : {},
"date_available" : null ,
"service" : 1
}
Jinja string
{{ instance.spec.os }}
Result
linux
Note
The spec
and user_spec
variables are only usable on Update or Delete operations as the pending instance does not contain any spec before its provisioning.
Note
If the given variable key doesn't exist, the default value will be set to an empty string.
Using the request as context
This example, used in the "default limit" of the operation job template config, allows to automatically configure the inventory limit following the given "dns" field of the survey.
Instance context
{
"id" : 32 ,
"instance" : {
"id" : 31 ,
"state" : "PENDING" ,
"resources" : [],
"billing_group" : null ,
"spoc" : {
"id" : 2 ,
"username" : "admin" ,
"email" : "admin@squest.domain" ,
"profile" : {
"request_notification_enabled" : true ,
"support_notification_enabled" : true ,
"request_notification_filters" : [],
"instance_notification_filters" : []
},
"first_name" : "" ,
"last_name" : "" ,
"is_staff" : true ,
"is_superuser" : true ,
"is_active" : true ,
"billing_groups" : []
},
"name" : "my-instance" ,
"spec" : {
},
"user_spec" : {},
"date_available" : null ,
"service" : 1
},
"user" : {
"id" : 2 ,
"username" : "admin" ,
"email" : "admin@squest.domain" ,
"profile" : {
"request_notification_enabled" : true ,
"notification_filters" : []
},
"first_name" : "" ,
"last_name" : "" ,
"is_staff" : true ,
"is_superuser" : true ,
"is_active" : true ,
"billing_groups" : []
},
"fill_in_survey" : {
"dns" : "vm-name.domain.com"
},
"admin_fill_in_survey" : {},
"date_submitted" : "2022-09-29T16:01:45.409615+02:00" ,
"date_complete" : null ,
"date_archived" : null ,
"tower_job_id" : null ,
"state" : "ACCEPTED" ,
"operation" : 7 ,
"approval_step" : null
}
Jinja string
{{ request.fill_in_survey.dns }}
Result
vm-name.domain.com
Dict access
Instance JSON spec
{
"spec" : {
"os" : {
"linux" : "ubuntu"
}
}
}
Jinja string
{{ instance.spec.os['linux'] }}
Result
ubuntu
List access
Instance JSON spec
{
"spec" : {
"os" : [ "linux" , "windows" ]
},
"user_spec" : {}
}
Jinja string
{{ spec.os[1] }}
Result
windows
Filters
Jinja filters can also be used to transform variables.
For example, the 'select multiple' field type requires a list of string separated with a carriage return marker (\n
).
Instance JSON spec
{
"spec" : {
"os" : [ "linux" , "windows" ]
},
"user_spec" : {}
}
Jinja string
{{ spec.os | join('\n') }}
Result
linux\nwindows
Conditions
In this example, the target inventory ID is changed following a survey variable is_prod
.
Instance JSON spec
{
"spec" : {
"is_prod" : true
},
"user_spec" : {}
}
Jinja string
{% if is_prod %}1{% else %}3{% endif %}
Result
linux\nwindows