Skip to content

Commit 944f536

Browse files
pabrahamssonoybed
authored andcommitted
Add support for passing vars to jinja templates, fixes #160 (#161)
1 parent a182920 commit 944f536

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

roles/openshift-applier/README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,11 @@ openshift_cluster_content:
107107
content:
108108
- name: Applying Openshift template
109109
file: "https://example.com/openshift/files/file.j2"
110+
jinja_vars:
111+
key1: value1
112+
key2: value2
110113
```
111-
Ansible variables are available and can be used in the Jinja template.
114+
Ansible variables are available and can be used in the Jinja template. Any variable required by the jinja template(s) can be passed through the `jinja_vars` dictionary (this is useful if you're using the same jinja template multiple times in your inventory). The `jinja_vars` are using `set_fact` and thus has precedence over host_vars and group_vars, see [Ansible variable precedence](https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#variable-precedence-where-should-i-put-a-variable) for more info.
112115
Additional examples are available in the [test directory](https://github.com/redhat-cop/openshift-applier/tree/master/tests/files/jinja-templates)
113116
114117
**NOTE: In order to use the jinja processing engine the file suffix must be '.j2'**

roles/openshift-applier/tasks/process-jinja.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66
set_fact:
77
process_file_or_template: "{{ (file != '') | ternary('file', 'template') }}"
88

9+
- name: Set facts to make variables available for the template
10+
set_fact:
11+
"{{ var.key }}": "{{ var.value }}"
12+
with_dict: "{{ content.jinja_vars | default({}) }}"
13+
loop_control:
14+
loop_var: var
15+
916
- name: Create temporary directory for Jinja template output
1017
tempfile:
1118
state: directory
@@ -43,4 +50,11 @@
4350
- name: Update path
4451
set_fact: {"{{ process_file_or_template }}":"{{ dest_path_yml }}"}
4552

53+
- name: Clear facts to ensure that they don't carry over
54+
set_fact:
55+
"{{ var.key }}": ""
56+
with_dict: "{{ content.jinja_vars | default({}) }}"
57+
loop_control:
58+
loop_var: var
59+
4660
delegate_to: localhost

tests/inventories/jinja-templates/group_vars/seed-hosts.yml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace_metadata:
99
NAMESPACE_DISPLAY_NAME: OpenShift Applier Jinja Templates Test 1 (displayName)
1010
NAMESPACE_DESCRIPTION: OpenShift Applier Jinja Templates Test 1 (description)
1111
openshift_applier_raw: "https://raw.githubusercontent.com/redhat-cop/openshift-applier"
12-
openshift_applier_ver: v2.1.0
12+
openshift_applier_ver: master
1313

1414
openshift_cluster_content:
1515
- object: projectrequest
@@ -21,10 +21,22 @@ openshift_cluster_content:
2121
- name: jinja-project-test-file
2222
file: "{{ inventory_dir }}/../../files/jinja-templates/projectrequest_file.j2"
2323
action: create
24+
- name: jinja-project-test-file-with-vars
25+
file: "{{ inventory_dir }}/../../files/jinja-templates/projectrequest_file.j2"
26+
jinja_vars:
27+
environments:
28+
- jinja
29+
action: create
2430
- name: delete jinja-project-test-template
2531
template: "{{ openshift_applier_raw }}/{{ openshift_applier_ver }}/tests/files/jinja-templates/projectrequest_template.j2"
2632
params_from_vars: "{{ namespace_metadata }}"
2733
action: delete
2834
- name: delete jinja-project-test-file
2935
file: "{{ inventory_dir }}/../../files/jinja-templates/projectrequest_file.j2"
3036
action: delete
37+
- name: delete jinja-project-test-file-with-vars
38+
file: "{{ inventory_dir }}/../../files/jinja-templates/projectrequest_file.j2"
39+
jinja_vars:
40+
environments:
41+
- jinja
42+
action: delete

0 commit comments

Comments
 (0)