How to use the Ansible template properly?
Hi all, I am trying to figure out how to use the templates via testinfra via ansible backend, and the documentation is lacking on this very specific kind of thing.
I have a SO open up too that may shed alittle more light on the subject as well. https://stackoverflow.com/questions/52860896/what-is-the-correct-way-to-pass-variables-to-an-ansible-template-with-testinfra
Essentially I am at a loss at how to pass in variables into a template, and right now all i am really able to do is to push files to the remote (files that are not templated) using the template command.
Anyone have any ideas on this?
Hi, I don't think it's currently possible because we have no way to set vars directly through the host.ansible().
However, host vars from ansible inventory or ./host_vars/<ansible_host> and group vars should be loaded and available within the jinja rendering.
Example:
>>> open('t.yaml', 'wb').write(b'host = {{ansible_host}}\n')
>>> import testinfra
>>> host = testinfra.get_host('ansible://localhost')
>>> host.ansible('template', 'src=t.yaml dest=/tmp/t.txt', check=False)
>>> open('/tmp/t.txt').read()
'host = 127.0.0.1\n'
I think we might have a way to add variables by using the variable_manager from testinfra/utils/ansible_runner.py