I have a playbook that runs a task, registers a variable. Sets a fact from that registered variables output. It later imports a role, within that role it has another set_fact using JINJA2 conditions with a for loop over the fact set in the playbook. The issue is, it appears the JINJA2 templating is being compiled before the set_fact in the playbook takes place. If I set that fact variable manually or hard code it, it works just fine.
Is this a bug or unknown issue with JINJA2 templating?
playbook:
- hosts: all
gather_facts: false
pre_tasks:
- name: Show Hostname
ansible.windows.win_powershell:
script: |
$returnname = "$($env:ComputerName).$((Get-WMIObject Win32_ComputerSystem).Domain)"
return $returnname
register: dns_hostname
- name: Set umpire_common_name
ansible.builtin.set_fact:
common_name: "{{ dns_hostname.output[0] | default(inventory_hostname) }}"
- name: Set umpire_common_name
ansible.builtin.set_fact:
umpire_common_name: "{{ umpire_common_name | default([]) + [common_name] }}"
roles:
- { role: umpire, umpire_debug: true, umpire_create_cert: true, umpire_dry_run: true, umpire_pfx: true }
The set_fact from the task inside the umpire fole.
- name: Determine umpire commands
ansible.builtin.set_fact:
umpire_cmd: >-
{%- set cmd_list = [] -%}
{% if umpire_common_name is defined and variable|length > 0 %}
{%- for cn in umpire_common_name -%}
{%- if umpire_chain is defined and umpire_chain == true -%}{%- set chain_flag = '--include-ca-chain' -%}{%- else -%}{%- set chain_flag = '' -%}{%- endif -%}
{%- if umpire_format is defined and umpire_format|length > 0 -%}{%- set format_flag = '-f={{ umpire_format }}' -%}{%- else -%}{%- set format_flag = '' -%}{%- endif -%}
{%- set cmd_flags = format_flag ~ ' ' ~ chain_flag -%}
{%- if umpire_dry_run is defined and umpire_dry_run -%}
{%- set _ = cmd_list.extend(['cert_umpire universal certificates create {{ playbook_dir }}/' ~ cn ~ '.yml -a {{ playbook_dir }}/auth.yml -e {{ cert_umpire_domain }} ' ~ cmd_flags | trim ~ ' -d']) -%}
{%- else -%}
{%- set _ = cmd_list.extend(['cert_umpire universal certificates create {{ playbook_dir }}/' ~ cn ~ '.yml -a {{ playbook_dir }}/auth.yml -e {{ cert_umpire_domain }} ' ~ cmd_flags | trim]) -%}
{% endif %}
{%- endfor -%}
{% endif %}
{{ cmd_list }}
The issue is it doesn’t like the fact umpire_common_name
is used in the template. If I hard code, or use umpire_common_name: ["{{ansible_inventory}}"]
or set it in my inv file it works. It’s only when I try to set it through/after a regsitered var.