Skip to content

Commit

Permalink
Add support for passing vars to jinja templates, fixes #160 (#161)
Browse files Browse the repository at this point in the history
  • Loading branch information
pabrahamsson authored and oybed committed Nov 14, 2019
1 parent a182920 commit 944f536
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
5 changes: 4 additions & 1 deletion roles/openshift-applier/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,11 @@ openshift_cluster_content:
content:
- name: Applying Openshift template
file: "https://example.com/openshift/files/file.j2"
jinja_vars:
key1: value1
key2: value2
```
Ansible variables are available and can be used in the Jinja template.
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.
Additional examples are available in the [test directory](https://github.com/redhat-cop/openshift-applier/tree/master/tests/files/jinja-templates)
**NOTE: In order to use the jinja processing engine the file suffix must be '.j2'**
Expand Down
14 changes: 14 additions & 0 deletions roles/openshift-applier/tasks/process-jinja.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
set_fact:
process_file_or_template: "{{ (file != '') | ternary('file', 'template') }}"

- name: Set facts to make variables available for the template
set_fact:
"{{ var.key }}": "{{ var.value }}"
with_dict: "{{ content.jinja_vars | default({}) }}"
loop_control:
loop_var: var

- name: Create temporary directory for Jinja template output
tempfile:
state: directory
Expand Down Expand Up @@ -43,4 +50,11 @@
- name: Update path
set_fact: {"{{ process_file_or_template }}":"{{ dest_path_yml }}"}

- name: Clear facts to ensure that they don't carry over
set_fact:
"{{ var.key }}": ""
with_dict: "{{ content.jinja_vars | default({}) }}"
loop_control:
loop_var: var

delegate_to: localhost
14 changes: 13 additions & 1 deletion tests/inventories/jinja-templates/group_vars/seed-hosts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace_metadata:
NAMESPACE_DISPLAY_NAME: OpenShift Applier Jinja Templates Test 1 (displayName)
NAMESPACE_DESCRIPTION: OpenShift Applier Jinja Templates Test 1 (description)
openshift_applier_raw: "https://raw.githubusercontent.com/redhat-cop/openshift-applier"
openshift_applier_ver: v2.1.0
openshift_applier_ver: master

openshift_cluster_content:
- object: projectrequest
Expand All @@ -21,10 +21,22 @@ openshift_cluster_content:
- name: jinja-project-test-file
file: "{{ inventory_dir }}/../../files/jinja-templates/projectrequest_file.j2"
action: create
- name: jinja-project-test-file-with-vars
file: "{{ inventory_dir }}/../../files/jinja-templates/projectrequest_file.j2"
jinja_vars:
environments:
- jinja
action: create
- name: delete jinja-project-test-template
template: "{{ openshift_applier_raw }}/{{ openshift_applier_ver }}/tests/files/jinja-templates/projectrequest_template.j2"
params_from_vars: "{{ namespace_metadata }}"
action: delete
- name: delete jinja-project-test-file
file: "{{ inventory_dir }}/../../files/jinja-templates/projectrequest_file.j2"
action: delete
- name: delete jinja-project-test-file-with-vars
file: "{{ inventory_dir }}/../../files/jinja-templates/projectrequest_file.j2"
jinja_vars:
environments:
- jinja
action: delete

0 comments on commit 944f536

Please sign in to comment.