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 May 16, 2019
1 parent 06d2629 commit aec058c
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 0 deletions.
8 changes: 8 additions & 0 deletions roles/openshift-applier/handlers/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,11 @@
state: absent
with_items:
- "{{ tmp_dep_dir }}"

- name: Clean up temporary jinja dirs
file:
path: "{{ item }}"
state: absent
with_items:
- "{{ tmp_jinja_dir_file }}"
- "{{ tmp_jinja_dir_template }}"
24 changes: 24 additions & 0 deletions roles/openshift-applier/tasks/process-file.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,30 @@
when: not patch_path.local_path
when: oc_action == 'patch'

- name: Process Jinja file (if applicable)
block:
- name: Create temporary directory for Jinja template output
tempfile:
state: directory
register: tmp_jinja_dir_file
notify:
- Clean up temporary inventory dir

- name: Set processed Jinja template output path
set_fact:
jinja_file_output_path: "{{ tmp_jinja_dir_file.path }}/{{ file_facts.oc_path | basename }}"

- name: Process Jinja template
template:
src: "{{ file_facts.oc_path }}"
dest: "{{ jinja_file_output_path }}"

- name: Update file path
set_fact:
file_facts: "{{ file_facts | combine( {'oc_path': jinja_file_output_path} ) }}"
when:
- jinjatemplate

- name: "{{ oc_action | capitalize }} OpenShift objects based on static files for '{{ entry.object }} : {{ content.name | default(file | basename) }}'"
command: >
oc {{ oc_action }} \
Expand Down
1 change: 1 addition & 0 deletions roles/openshift-applier/tasks/process-one-entry.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
template: "{{ content.template | default('') }}"
params: "{{ content.params | default('') }}"
params_from_vars: "{{ content.params_from_vars | default({}) }}"
jinjatemplate: "{{ content.jinjatemplate | default(false) }}"
oc_ignore_unknown_parameters: "{{ content.ignore_unknown_parameters | default(oc_ignore_unknown_parameters) }}"
no_log: False

Expand Down
24 changes: 24 additions & 0 deletions roles/openshift-applier/tasks/process-template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,30 @@
param_facts: "{{ (processed_file_facts|length == 0) | ternary([{ 'oc_path': '' }], processed_file_facts) }}"
processed_file_facts: []

- name: Process Jinja template (if applicable)
block:
- name: Create temporary directory for Jinja template output
tempfile:
state: directory
register: tmp_jinja_dir_template
notify:
- Clean up temporary inventory dir

- name: Set processed Jinja template output path
set_fact:
jinja_template_output_path: "{{ tmp_jinja_dir_template.path }}/{{ template_facts.oc_path | basename }}"

- name: Process Jinja template
template:
src: "{{ template_facts.oc_path }}"
dest: "{{ jinja_template_output_path }}"

- name: Update template path
set_fact:
template_facts: "{{ template_facts | combine( {'oc_path': jinja_template_output_path} ) }}"
when:
- jinjatemplate

- name: "{{ oc_action | capitalize }} OpenShift objects based on template with params for '{{ entry.object }} : {{ content.name | default(template | basename) }}'"
shell: >
oc process \
Expand Down
1 change: 1 addition & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,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.yml
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: 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.yml
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: 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
24 changes: 24 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,24 @@
---

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.yml"
params_from_vars: "{{ namespace_metadata }}"
action: create
jinjatemplate: true
- name: jinja-project-test-file
file: "{{ inventory_dir }}/../../files/jinja-templates/projectrequest_file.yml"
action: create
jinjatemplate: true
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 aec058c

Please sign in to comment.