pytest-testinfra icon indicating copy to clipboard operation
pytest-testinfra copied to clipboard

Read Ansible custom facts in testinfra/molecule

Open shoneslab opened this issue 7 years ago • 4 comments

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"]

shoneslab avatar Nov 16 '17 18:11 shoneslab

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).

philpep avatar Nov 22 '17 20:11 philpep

Thanks @philpep

shoneslab avatar Dec 01 '17 01:12 shoneslab

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!

decentral1se avatar May 29 '19 09:05 decentral1se

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.

till avatar Feb 13 '20 13:02 till