pytest-testinfra
pytest-testinfra copied to clipboard
Ansible.cfg is not fully used
Hi!
I use TestInfra through molecule to test my ansible roles.
I need sometimes to have a different user and different config that are present in ansible.cfg.
For now I have done this to change environment variable from info written in the configuration file. Am I right, should I do something else?
import os
import configparser
import testinfra.utils.ansible_runner
config = configparser.ConfigParser()
config.read(os.environ['ANSIBLE_CONFIG'])
if config.has_option('defaults', 'become_user'):
os.environ['ANSIBLE_BECOME_USER'] = config['defaults']['become_user']
if config.has_option('defaults', 'become'):
os.environ['ANSIBLE_BECOME'] = config['defaults']['become']
testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all')
Thanks ;-)
Hi, thanks for reporting this! I'd expect testinfra to use the settings that are defined in the inventory file / ansible.cfg. Looks like a bug, under what version of ansible did you encounter this issue ?
I use Ansible 2.5.0
ansible 2.5.0
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/home/nanou/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/dist-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.14 (default, Sep 23 2017, 22:06:14) [GCC 7.2.0]
But regarding the configuration file, Molecule creates one located at the ANSIBLE_CONFIG
environement variable ;)
I came here to report the same problem. I'm using Ansible 2.7.1.
I'd like to use testinfra in a project that is already making use of Ansible. We use an ansible.cfg
file to control the behavior of Ansible, including setting a path to an inventory file. It appears that the mechanism testinfra
uses for executing Ansible actions does not utilize the settings the ansible.cfg
, which means instead of:
pytest --hosts ansible://controller
I need to always run:
pytest --hosts=ansible://controller --ansible-inventory data/hosts.yml
I worry that other important settings (like host_key_checking
, or the [inventory]
section used to enable inventory plugins) will also be missed. Is there any way to get Ansible to make use of the config file when executing the testinfra
tasks?
What's puzzling is that strace
reveals that something is opening and reading ansible.cfg
, even though the settings are not being applied:
3507 openat(AT_FDCWD, "/home/lars/projects/example/ansible.cfg", O_RDONLY|O_CLOEXEC) = 8
3507 fstat(8, {st_mode=S_IFREG|0664, st_size=282, ...}) = 0
3507 ioctl(8, TCGETS, 0x7ffda41c8d20) = -1 ENOTTY (Inappropriate ioctl for device)
3507 lseek(8, 0, SEEK_CUR) = 0
3507 lseek(8, 0, SEEK_CUR) = 0
3507 fstat(8, {st_mode=S_IFREG|0664, st_size=282, ...}) = 0
3507 read(8, "[defaults]\nhost_key_checking = false\nretry_files_enabled = false\ngathering = smart...", 283) = 282
3507 read(8, "", 1) = 0
3507 close(8) = 0
It also looks as if environment variables such as ANSIBLE_INVENTORY
are ignored.