Skip to content

Commit

Permalink
Add support for Jinja templates, closes #119 (#120)
Browse files Browse the repository at this point in the history
* Add support for Jinja templates, closes #119

* Use project.openshift.io/v1

* Remove unsupported '--validate' param from 'delete' action

* Set correct params for patch, delete & get

* Don't use '--ignore-not-found' with patch

* Hold off on 'oc get' for #134
  • Loading branch information
pabrahamsson authored and oybed committed Aug 1, 2019
1 parent 18b0821 commit 1b87b99
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 3 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 }}"
2 changes: 1 addition & 1 deletion roles/openshift-applier/tasks/process-file.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
{{ client }} {{ oc_action }} \
{{ target_namespace }} \
-f {{ file_facts.oc_path }} \
{{ (oc_action == 'delete') | ternary(' --ignore-not-found', '') }} \
{{ (oc_action | regex_search('delete')) | ternary(' --ignore-not-found', '') }} \
{% if oc_action == 'patch' %}
-p '{{ patch_content | to_json }}'
{% endif %}
Expand Down
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
4 changes: 2 additions & 2 deletions roles/openshift-applier/tasks/process-template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@
{{ client }} {{ oc_action }} \
{{ target_namespace }} \
-f - \
{{ (oc_action == 'delete') | ternary(' --ignore-not-found', '') }} \
{{ (client == 'kubectl') | ternary(' --validate=false', '') }}
{{ (oc_action | regex_search('delete')) | ternary(' --ignore-not-found', '') }} \
{{ (client == 'kubectl' and not (oc_action | regex_search('delete|patch'))) | ternary(' --validate=false', '') }}
register: command_result
no_log: "{{ no_log }}"
failed_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: project.openshift.io/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: project.openshift.io/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 1b87b99

Please sign in to comment.