Ansible and Jinja Templates
Quite recently I have adopted the OUD 12c configuration playbook to work with 11g PS3. And here I run into a tricky difference in configuration parameters. I'm pretty sure there are numerous ways to solve this, but my story is all about Ansible and Jinja.
Let's start with the task: OUD 12c introduced new parameter --instancePath, so you can specify where you want to place your new OUD instance, something like this:
$ $ORACLE_HOME/oud/oud-setup --cli --instancePath /u01/app/oracle/instances/oud1 ...
There are no easy ways to do so in OUD 11g. If you need a custom location, you should declare environment INSTANCE_NAME (really?) and specify a path to the location. Sounds easy, doesn't it? Not so much, you should specify the patch relative to the $ORACLE_HOME which means it would look similar to:
$ export INSTANCE_NAME=../../../../instances/oud1
$ $OUD_HOME/OUD/oud-setup --cli ...
And that where the all fun begins. Just stop for a second and try to solve this problem in any language. Java, RegExp, Bash, Python - your call. Right, if you are not the RegExp guru, you end up with the same conclusion: some code to produce the relative path from the root folder. Fortunately, Jinja2 templates are powerful enough to handle this. Here is my quick and dirty solution:
- name: Configure OUD 11g PS3 instance
shell:
cmd: |
export JAVA_HOME=/u01/app/oracle/product/jdk
{% set oud_path=( oracle_home ~ "/Oracle_OUD1").split('/') %}
export INSTANCE_NAME="{%- for p in oud_path[1:] %}
{{ '..' if loop.last else '../' }}
{% endfor -%}{{ instnace_home }}/{{ instance_name }}"
{{ oracle_home }}/Oracle_OUD1/OUD/oud-setup --cli ....
A few comments on how I generate the final shell script.
- Expression
yaml {% set oud_path = %}
defines new list oud_path. It contains all path entries to the OUD 11g home. - Jinja2 control
yaml {% for in %} {% endfor %}
lops through the oud_path list, from 1st element to and produces backtrack to the root folder. - I don't use list values. The template produces '../' for each element, or '..' if it is the last one.
- The rest of the path
yaml {{ instnace_home }}/{{ instance_name }}
points to the desired instance location.
Worth to mention: To make it work I added whitespace stripping instructions: {%- -%}. The dash instructs Jinja template to strip all whitespaces, including linefeeds.