Arbitrary host expressions for Ansible backend #807
+200
−7
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Ansible has extensive so-called host expressions: ability to join/exclude hosts from a host list based on logical expressions:
Examples:
database,&primary
- hosts in database and primary groupsdatabase,!standby
- hosts in database group but not in standby groupdatabase[0]
- first host in the groupFull list is here: https://docs.ansible.com/ansible/latest/inventory_guide/intro_patterns.html
testnfra lacked this ability for long time (see #609, rejected, where I was asked to bring patterns).
I've decided to try to implement them. Implementing from scratch is very hard (see link above to see all features and complexity). I've tried to reuse ansible code (specifically, InventoryManager, which allows evaluation of the host expressions). Proper support for InventoryManager also required to add ConfigManager (I wanted to keep full support for
ANSIBLE_INVENTORY
environment var which I use a lot for many testcases).I understand, that bringing
from ansible import ..
could break a lot of things, so instead of 'switching to Ansible for everything', I've added a simple heuristic:Examples
Host expression problem
I also found, that
ansible://groupname[0]
is not valid uri ([] is reserved for IPv6 addreses in the host part of the URI). I thought about possible workarounds, and the least invasive was to replaceansible://groupname[0]
withansible://groupname(0)
. Slightly odd, but perfectly readable and limited only to the specific case.Limitations for possible characters in 'host' part of URI is also the reason why regexp (~) can not be supported this way, unfortunately.