ansible/pytest-ansible

How to apply ansible arguments in runtime?

Blueve opened this issue · 2 comments

I have an inventory file just like below:

; Inventory file
[vms]
VM0100 ansible_host=10.250.0.51

I want to indicate the connection type/network_os/... etc. in runtime and I write following code:

hosts = ansible_adhoc(
            become=True,
            become_method='enable',
            connection='network_cli',
            network_os='eos')

I can see the HostManager accept my input(I printed the hosts.option and found that the connection type has been changed from 'smart' to 'network_cli') but when I run a module on a target host:

hosts['VM0100'].eos_config()

It returns following message and seems like my custom ansible arguments are not apply to the ansible_adhoc

Connection type ssh is not valid for this module

Did I missed something here? Is there a way to apply ansible arguments in runtime?

Thanks

I got a solution from Guohan's PR: https://github.com/Azure/sonic-mgmt/pull/1475/files

We can apply custom configurations by update variable manager's extra_vars instead of inject from constructor directly:

hosts = ansible_adhoc(become=True, connection='network_cli')
evars = { 'ansible_connection': 'network_cli',  network_os='eos' }
hosts.options['variable_manager'].extra_vars.update(evars)

I still think it is an issue in pytest ansible.