Add a magical function testinfra_hosts_update to update host list in runtime
This adds a support for optional magical function testinfra_hosts_update to each module, which allows to filter host list after it was created by testinfra.get_hosts before doing an actual test parametrization. Practially, it's allow to filter/update/mangle host list after it was become a list of concrete hosts.
The issue it tries to solve is that testinfra_hosts when used in tests for molecule/ansible environment usually contains group names (as host names are volatile in many molecule usecases), and some tests may requires to be "run_once" for a given group.
The practical scenario is like this (mons is a groups for Ceph monitors):
testinfra_hosts = ["ansible://mons?force_ansible=True"]
def testinfra_hosts_update(host_list):
return testinfra[host_list[0]]
def test_cluster_OK(host):
assert 'HEALTH_OK' in host.check_output('ceph -s').stdout
Hi thanks for this. But I think it's a bit too magical :)
Maybe It's better to handle ansible patterns no ? See #602
I thought about it. I really would like ability to specify the hosts-style host list (something like that: ansible://mons[0],mgrs,!foobar, which says 'host no1 from monitors, all mgrs, but not the stuff in foobar group or named foobar'), but I suspect it's hard to implement.
Actually some kind of hook to process the hostlist after it was parsed and processed by Ansible is really welcomed. Ansible inventory is really tricky thing (given the molecule control over inventory), so ability to tweak results are important, I think.
The magicalily of testinfra_hosts_update is on pair with testinfra_hosts, I think. Better naming may be a good idea, though.
@amarao I added some comment on #602 , seems ansible --list-hosts <pattern> resolve hosts from the pattern. In any cases I think we need to implement ansible patterns. And maybe later on we could add such hook if still needed.