pytest-testinfra
pytest-testinfra copied to clipboard
Read Ansible custom facts in testinfra/molecule
Question:
Is any way to read custom fact set by the user in roles/tasks in the testinfra python script when using molecule.
In the ansible role, I have a tasks like
- name: get the timestamp
set_fact:
my_dir_timestamp: "{{ lookup('pipe', 'date +%m%d%Y-%H%M%S') }}"
Is it possible to read the above facts in the testscript.
def test_read_facts(Ansible, host):
myvar = Ansible("debug", "msg={{ my_dir_timestamp }}")["msg"]
Hi, I don't think it's possible right now because testinfra does not run (within) the playbook.
But it could be an interesting feature to run tests within the playbook (for a quick validation during deployment).
Thanks @philpep
This is a much better report of what I tried to express in the Long Read of https://github.com/philpep/testinfra/issues/345!
Duplicate warning!
Has anyone considered using fact caching in Ansible? I am not sure if this extends to the custom facts one can add with set_fact, but then we could assert on them.
Context: I am refactoring a role which installs software from a repository, and depending on the distribution (or os family), all paths are different. I now have the logic twice, once in Ansible to put files where they belong, and another instance in testinfra to assert they are in the location and contain what I expect.