ansible --version = ansible 2.4.0.0
molecule --version = molecule, version 2.4.0
How to access ansible variables inside of Python test code?
I have a role that writes a string into a file and I wanted to check if the file has the string.
e.g.:
import os
import testinfra.utils.ansible_runner
testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all')
def test_passwd_file(host):
passwd_file = host.file('/etc/passwd')
my_string = 'my_username_value' # How to import ansible my_username var?
assert passwd_file.contains(my_string)
The code works, but instead of hardcoding the string (my_username on the example), I wanted to import my role's default file and use the variables in the test.
Something like:
import os
import testinfra.utils.ansible_runner
testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all')
def test_passwd_file(host):
passwd_file = host.file('/etc/passwd')
my_string = Ansible("include_var","./defaults/main.yml")["my_username"]
assert passwd_file.contains(my_string)
Just figured out how:
import os
import testinfra.utils.ansible_runner
testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all')
def test_passwd_file(host, Ansible):
ansible_vars = Ansible("include_vars", os.path.join("../../../defaults/main.yml"))["ansible_facts"]
passwd_file = host.file('/etc/passwd')
my_string = ansible_vars["my_username"]
assert passwd_file.contains(my_string)
Hope this issue helps others in need.
The Ansible fixture feature is deprecated.
Here is a better way to access the variables using the host fixture and include_vars Ansible module:
import os
import testinfra.utils.ansible_runner
testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all')
def test_passwd_file(host, Ansible):
ansible_vars = host.ansible("include_vars", "file=../../../defaults/main.yml")["ansible_facts"]
passwd_file = host.file('/etc/passwd')
my_string = ansible_vars["my_username"]
assert passwd_file.contains(my_string)
This isn't a Molecule issue, this is really a testinfra issue. However, I personally don't recommend this approach. If one inadvertently changes a variable, the tests will continue to pass.
As an example, if the listen port were a variable and it was changed, the tests may consume the listen port variable and continue to function, even though this change could have drastic side effects when deployed.
Again, this is more of a testinfra question than a Molecule question. Closing, since we have had this issue asked quite a few times. See closed issues.
Most helpful comment
This isn't a Molecule issue, this is really a testinfra issue. However, I personally don't recommend this approach. If one inadvertently changes a variable, the tests will continue to pass.
As an example, if the listen port were a variable and it was changed, the tests may consume the listen port variable and continue to function, even though this change could have drastic side effects when deployed.
Again, this is more of a testinfra question than a Molecule question. Closing, since we have had this issue asked quite a few times. See closed issues.