Skip to content

Commit

Permalink
Add support for Jinja templates, closes redhat-cop#119
Browse files Browse the repository at this point in the history
  • Loading branch information
pabrahamsson committed Jul 31, 2019
1 parent 18b0821 commit 69164b3
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 0 deletions.
7 changes: 7 additions & 0 deletions roles/openshift-applier/handlers/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,10 @@
state: absent
with_items:
- "{{ tmp_dep_dir }}"

- name: Clean up temporary Jinja directory
file:
path: "{{ item }}"
state: absent
with_items:
- "{{ jinja_tmp_dir.path }}"
28 changes: 28 additions & 0 deletions roles/openshift-applier/tasks/process-jinja.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---

- name: Delegate processing to localhost
block:
- name: Set file or template fact
set_fact:
process_file_or_template: "{{ (file != '') | ternary('file', 'template') }}"

- name: Create temporary directory for Jinja template output
tempfile:
state: directory
register: jinja_tmp_dir
notify:
- Clean up temporary Jinja directory

- name: Set processed Jinja template output path
set_fact:
dest_path: "{{ jinja_tmp_dir.path }}/processed_{{ lookup('vars', process_file_or_template) | basename | regex_replace('.j2$','.yml') }}"

- name: Process Jinja template
template:
src: "{{ lookup('vars', process_file_or_template) }}"
dest: "{{ dest_path }}"

- name: Update path
set_fact: {"{{ process_file_or_template }}":"{{ dest_path }}"}

delegate_to: localhost
4 changes: 4 additions & 0 deletions roles/openshift-applier/tasks/process-one-entry.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
loop_control:
loop_var: step

- name: "Process Jinja Template (if applicable)"
include_tasks: process-jinja.yml
when: template | regex_search('\.j2$') or file | regex_search('\.j2$')

- name: "Process Template (if applicable)"
include_tasks: process-template.yml
when:
Expand Down
1 change: 1 addition & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ ansible-playbook playbooks/openshift-cluster-seed.yml -i tests/inventories/param
ansible-playbook playbooks/openshift-cluster-seed.yml -i tests/inventories/pre-post-steps
ansible-playbook playbooks/openshift-cluster-seed.yml -i tests/inventories/patch
ansible-playbook playbooks/openshift-cluster-seed.yml -i tests/inventories/cluster-template
ansible-playbook playbooks/openshift-cluster-seed.yml -i tests/inventories/jinja-templates
```


Expand Down
12 changes: 12 additions & 0 deletions tests/files/jinja-templates/projectrequest_file.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
apiVersion: v1
kind: List
items:
{% for environment in environments %}
- apiVersion: v1
kind: {{ 'Project' if (oc_action == 'delete') else 'ProjectRequest' }}
metadata:
name: {{ namespace_metadata.NAMESPACE }}-file-{{ environment }}
description: {{ namespace_metadata.NAMESPACE_DESCRIPTION }} (file) - {{ environment }}
displayName: {{ namespace_metadata.NAMESPACE_DISPLAY_NAME }} (file) - {{ environment }}
{% endfor %}
34 changes: 34 additions & 0 deletions tests/files/jinja-templates/projectrequest_template.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
apiVersion: v1
kind: Template
labels:
template: projectrequest-template
message: |-
The following project/namespace has been created: ${NAMESPACE}
metadata:
annotations:
description: |-
ProjectRequest Template
creationTimestamp: null
name: ${NAMESPACE}
objects:
{% for environment in environments %}
- apiVersion: v1
kind: {{ 'Project' if (oc_action == 'delete') else 'ProjectRequest' }}
metadata:
name: ${NAMESPACE}-template-{{ environment }}
description: '${NAMESPACE_DESCRIPTION} (template) - {{ environment }}'
displayName: '${NAMESPACE_DISPLAY_NAME} (template) - {{ environment }}'
{% endfor %}
parameters:
- description: Name
displayName: Name
name: NAMESPACE
required: true
- description: DisplayName
displayName: DisplayName
name: NAMESPACE_DISPLAY_NAME
required: true
- description: Description
displayName: Description
name: NAMESPACE_DESCRIPTION
29 changes: 29 additions & 0 deletions tests/inventories/jinja-templates/group_vars/seed-hosts.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---

environments:
- dev
- test
- prod

namespace_metadata:
NAMESPACE: oa-ci-jinja-templates
NAMESPACE_DISPLAY_NAME: OpenShift Applier Jinja Templates Test 1 (displayName)
NAMESPACE_DESCRIPTION: OpenShift Applier Jinja Templates Test 1 (description)

openshift_cluster_content:
- object: projectrequest
content:
- name: jinja-project-test-template
template: "{{ inventory_dir }}/../../files/jinja-templates/projectrequest_template.j2"
params_from_vars: "{{ namespace_metadata }}"
action: create
- name: jinja-project-test-file
file: "{{ inventory_dir }}/../../files/jinja-templates/projectrequest_file.j2"
action: create
- name: delete jinja-project-test-template
template: "{{ inventory_dir }}/../../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
3 changes: 3 additions & 0 deletions tests/inventories/jinja-templates/host_vars/localhost.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---

ansible_connection: local
3 changes: 3 additions & 0 deletions tests/inventories/jinja-templates/hosts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

[seed-hosts]
localhost

0 comments on commit 69164b3

Please sign in to comment.