Pass Variables Explicitly to included Roles from Ansible 2.18

From Ansible 2.18 onward, pass every variable an included role needs but does not own explicitly via vars: on ansible.builtin.include_role or ansible.builtin.import_role, so the role does not silently fall back to defaults or receive untemplated values from hostvars.

Problem

Implicit variable visibility between a calling role and an included role used to make some integrations look simple, but it hid the real contract. Under Ansible 2.18 that implicit visibility is gone, so three failure modes appear:

  1. A variable defined in the calling role is undefined inside the included role, causing the task to fail.
  2. The included role falls back to its own defaults/main.yml value, which may work by accident but is not the intended configuration.
  3. The included role reads a dictionary from hostvars[inventory_hostname]. Nested Jinja expressions inside that dictionary, such as {{ hostvars[inventory_hostname]['ansible_host'] }}, can remain untemplated and are passed literally to modules.

C2-1824 is an example of the third case: an older version of c2platform.core.cacerts2 read haproxy_cacerts2_certificates from hostvars, so the SAN entry IP:{{ hostvars[inventory_hostname]['ansible_host'] }} reached community.crypto.openssl_csr as a literal string instead of the actual IP address.

Context

Ansible 2.18 changed the variable scope for include_role and import_role so that included roles do not automatically inherit the calling role’s defaults/main.yml and vars/main.yml. Inventory variables and facts are still available in the included role and still override role defaults, but relying on that is fragile when the included role also has defaults that look reasonable.

public: true is not a solution. That option exposes the included role’s variables to the rest of the play; it does not give the included role access to the calling role’s variables.

Reading dictionaries from hostvars[inventory_hostname] is also unreliable, because Ansible templates hostvars values lazily and does not recursively template nested hostvars[...] expressions. Accessing the same variable through the normal variable namespace, for example by passing it via vars:, renders the full value in the caller’s context.

Solution

For every include_role or import_role task, decide where each variable the included role uses comes from:

  1. Required variables with no workable default. Give them a placeholder default such as __TODO__ in the included role and add an ansible.builtin.assert that fails when the value is still the placeholder. Set the real value in inventory or pass it explicitly via vars:.
  2. Optional variables the caller wants to control or expose as part of its API. Pass these explicitly via vars: on the include task. This is the only way to guarantee that the caller’s value wins over the included role’s default.
  3. Optional variables set in inventory. These still override role defaults without being passed explicitly. Do not duplicate them in vars: unless the caller needs to transform or override the inventory value.

When a data structure passed to the included role contains Jinja expressions that reference caller variables, pass the whole structure via vars:. Rendering happens in the caller’s context, so caller variables inside the structure are resolved and nested expressions are fully templated before the value reaches the included role.

Prefer ansible.builtin.include_role when runtime values from the caller are needed; remember that vars: on ansible.builtin.import_role is evaluated at compile time.

Benefits

  • Makes the variable contract between caller and included role explicit.
  • Prevents silent fallback to role defaults that hide misconfigurations.
  • Ensures Jinja inside dictionaries is rendered in the caller’s context.
  • Keeps the code compatible with Ansible 2.18 and later.

Examples and Implementation

Pass the dictionary and caller variables explicitly

The c2platform.mw.haproxy role needs a certificate dictionary from inventory. That dictionary contains Jinja expressions such as {{ hostvars[inventory_hostname]['ansible_host'] }} that must be rendered in the caller’s context:

 group_vars/cka_lb/certs.yml

 1---
 2haproxy_cacerts2_certificates:
 3  - common_name: haproxy
 4    subject_alt_name:
 5      - "DNS:cka.c2platform.org"
 6      - "DNS:{{ ansible_hostname }}"
 7      - "DNS:{{ ansible_fqdn }}"
 8      - "IP:{{ hostvars[inventory_hostname]['ansible_host'] }}"
 9    ansible_group: haproxy
10    deploy:
11      pem:
12        dir: /etc/haproxy/conf
13        owner: www-data
14        group: www-data
15        mode: '640'

Before Ansible 2.18, the HAProxy role could pass only cacerts2_role_name and let cacerts2 look up haproxy_cacerts2_certificates from hostvars:

- name: Include certs tasks
  ansible.builtin.include_role:
    name: c2platform.core.cacerts2
    tasks_from: certs
  vars:
    cacerts2_role_name: haproxy

Under Ansible 2.18 that lookup leaves nested Jinja untemplated. The role now passes the dictionary explicitly:

 roles/haproxy/tasks/certs.yml

1---
2- name: Include cacerts2 certificate tasks
3  ansible.builtin.import_role:
4    name: c2platform.core.cacerts2
5    tasks_from: certs
6  vars:
7    cacerts2_role_name: haproxy
8    cacerts2_certificates: "{{ haproxy_cacerts2_certificates }}"

cacerts2_role_name is still used so that cacerts2 can publish the enriched certificates back as haproxy_cacerts2_certificates. The input, however, is now cacerts2_certificates, which is rendered in the HAProxy role’s context. The IP address is resolved before the value reaches cacerts2.

Fail loudly on required variables

When a variable must be supplied by the caller, do not hide a missing value behind a default that happens to work. Use a placeholder default and an assert:

 roles/cacerts2/defaults/main.yml

10cacerts2_ca_server: __TODO__
11cacerts2_ca_domain: __TODO__

 roles/cacerts2/tasks/asserts.yml

 1---
 2- name: Verify required cacerts2 variables are set
 3  ansible.builtin.assert:
 4    that:
 5      - vars[item] is defined
 6      - vars[item] != '__TODO__'
 7    fail_msg: >-
 8      Required cacerts2 variable {{ item }} is not set
 9      (current value: {{ vars[item] | default('undefined') }}).
10      Set it in inventory or pass it explicitly via vars:.
11    quiet: true
12  loop:
13    - cacerts2_ca_server
14    - cacerts2_ca_domain
15  loop_control:
16    label: "{{ item }}"
17  tags: [always, cacerts2-always]

The assert runs before any certificate work, so a missing CA server or domain fails immediately with a clear message instead of producing a bad certificate later.

Additional Information