The issue lies in how you're constructing the keystore_file_name variable using string concatenation and the conditional expression. The if...else is being evaluated as a string, not as a conditional statement within the variable assignment. This means Ansible is literally including the entire if (jdkdescriptor.vers_major | int > 8) else ... string into the keystore_file_name variable.
Here's a breakdown of why it's happening and how to fix it:
-
Ansible's Jinja2 templating: Ansible uses Jinja2 for templating. Within Jinja2, you need to use the selectattr filter or a more explicit if/else structure within the Jinja2 expression. Simply writing if ... else ... as you have it doesn't trigger a conditional evaluation.
-
String Concatenation: The + operator in Jinja2 performs string concatenation. So, it's treating the entire if...else block as a string to be added to the path.
Here are a few ways to fix it:
1. Using selectattr (most concise):
- name: rebuild keystore entries
include_role:
name: update-jdk-keystore
allow_duplicates: no
vars:
keytool_path: "{{ jdkdescriptor.dst.path }}/bin/keytool"
keystore_file_name: "{{ jdkdescriptor.dst.path + '/lib/security/cacerts' if jdkdescriptor.vers_major | int > 8 else jdkdescriptor.dst.path + '/jre/lib/security/cacerts' }}"
when: (rc_unarchive is defined and rc_unarchive.changed) or jdkdescriptor.force_cacert
This is the same as your original code, but it should work as intended.
2. Using a Jinja2 if/else block (more readable for complex conditions):
- name: rebuild keystore entries
include_role:
name: update-jdk-keystore
allow_duplicates: no
vars:
keytool_path: "{{ jdkdescriptor.dst.path }}/bin/keytool"
keystore_file_name: >-
{% if jdkdescriptor.vers_major | int > 8 %}
{{ jdkdescriptor.dst.path }}/lib/security/cacerts
{% else %}
{{ jdkdescriptor.dst.path }}/jre/lib/security/cacerts
{% endif %}
when: (rc_unarchive is defined and rc_unarchive.changed) or jdkdescriptor.force_cacert
This approach uses the Jinja2 if/else block, which is more explicit and easier to read, especially if the condition becomes more complex. The >- (folded block scalar) allows you to write the string over multiple lines for better readability.
3. Using a ternary operator (similar to your original attempt, but corrected):
- name: rebuild keystore entries
include_role:
name: update-jdk-keystore
allow_duplicates: no
vars:
keytool_path: "{{ jdkdescriptor.dst.path }}/bin/keytool"
keystore_file_name: "{{ jdkdescriptor.dst.path }}/lib/security/cacerts" if jdkdescriptor.vers_major | int > 8 else "{{ jdkdescriptor.dst.path }}/jre/lib/security/cacerts"
when: (rc_unarchive is defined and rc_unarchive.changed) or jdkdescriptor.force_cacert
This is a corrected version of your original attempt. The key is to ensure the else part is also enclosed in double quotes to be treated as a string.
Recommendation:
I recommend using the Jinja2 if/else block (option 2) for better readability and maintainability, especially if the condition might become more complex in the future. It's clearer to understand the logic at a glance.
Debugging Tip:
When you're having trouble with Jinja2 expressions, use the debug module to print the value of the variable before it's used. This will help you see exactly what's being evaluated and identify any errors. For example:
- name: Debug keystore_file_name
debug:
var: keystore_file_name
Add this task before the task that uses keystore_file_name to see its value. This will quickly reveal if the conditional expression is being evaluated correctly.