What's the proper way to enable fact storage?

Posted on

I need to enable fact (variable) storage so that a fact stored in one job template is available in a following job template in a workflow. Here's what my job templates look like:

Job template #1: Set Fact

  • Enable Fact Storage: checked
  • Playbook: set_fact_playbook.yml

set_fact_playbook.yml:

---
- name: Set fact
  hosts: localhost
  gather_facts: yes

  tasks:
    - name: Assign my_variable
      ansible.builtin.set_fact:
        my_variable: My variable content
        cacheable: yes

Job template #2: Get Fact

Enable Fact Storage: checked
Playbook: get_fact_playbook.yml
get_fact_playbook.yml:

---
- name: Get fact
  hosts: localhost
  gather_facts: yes

  tasks:
    - ansible.builtin.debug:
        var: my_variable

    - ansible.builtin.debug:
        var: ansible_facts['my_variable']

Job template #2 (Get Fact) runs "On Success" after job template #1 (Set Fact) in the workflow. Here's the result of Get Fact:

TASK [ansible.builtin.debug] ***************************************************
task path: /runner/project/ansible/get_fact_playbook.yml:7
ok: [localhost] => {
    "my_variable": "VARIABLE IS NOT DEFINED!: 'my_variable' is undefined. 'my_variable' is undefined"
}

TASK [ansible.builtin.debug] ***************************************************
task path: /runner/project/ansible/get_fact_playbook.yml:10
ok: [localhost] => {
    "ansible_facts['my_variable']": "VARIABLE IS NOT DEFINED!: 'dict object' has no attribute 'my_variable'. 'dict object' has no attribute 'my_variable'"
}

I also tried enabling fact storage in ansible.cfg:

[defaults]
fact_caching = jsonfile
fact_caching_connection = /tmp/ansible_cache
fact_caching_prefix = ''
fact_caching_timeout = 86400

I stored the ansible.cfg at the root of the repository from which the project syncs, in accordance with this documentation. I've verified that AAP can see this file with a stat task, resulting in:

"stat": {
  "exists": true,
  "path": "/runner/project/ansible.cfg",

I even found this Red Hat KB solution which states that the AAP must be configured for the UTC time zone, which mine is.

At this point I don't know what else I need to do to enable fact storage. What am I doing wrong and what's the right way to accomplish this?

Responses