How do I share variables/facts between Molecule Playbooks?

1.5k views Asked by At

I'm trying to share a variable between my converge step and my cleanup step of my Molecule. Since both playbooks are being run on the same host, I figure I can use facts to cache the variable as a fact.

In converge.yml:

- name: Cache some variable
    set_fact:
      cacheable: yes
      my_fact: "howdy, world"

In cleanup.yml:

- debug:
    msg: "{{ my_fact }}"

and in my top-level ansible.cfg I specify:

[defaults]
gathering = smart
fact_caching = jsonfile
fact_caching_connection = /tmp/facts_cache
fact_caching_timeout = 7200

I first run molecule converge and then I run molecule cleanup I'm getting the error: (reformatted)

TASK [debug] *******************************************************************
fatal: [cluster-toolchain]: FAILED! => {"msg": "The task includes an option
with an undefined variable. The error was: 'my_fact' is undefined
The error appears to be in '{REDACTED}/cleanup.yml': line 5, column 7, but may be
elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:\n\n  tasks:\n    - debug:\n      ^ here\n"}

I've got some guesses, like maybe Molecule doesn't see my top level ansible.cfg or something.

Anyway, I was hoping for some help here. Maybe there is a better way to share the variable in Molecule world.

1

There are 1 answers

0
Jean-Pierre Matsumoto On

Molecule is a tool to test Ansible roles. It is not targeted to execute Ansible playbooks. Usual command is ansible-playbook.

Molecule cannot not read your ansible.cfg with fact caching settings. If you want to make your commands working, add settings in molecule.yml:

provisioner:
  name: ansible
  config_options:
    defaults:
      gathering: smart
      fact_caching: jsonfile
      fact_caching_connection: /tmp/facts_cache
      fact_caching_timeout: 7200

Then molecule converge followed by molecule cleanup gives:

PLAY [Cleanup] ************************************************************************************************************************

TASK [debug] **************************************************************************************************************************
ok: [vagrant-instance] => {
    "msg": "howdy, world"
}