hosts.py: Generalized supported operations of group pattern

master
Felix Stupp 4 years ago
parent 7d858342e2
commit 0b0135dd57
Signed by: zocker
GPG Key ID: 93E1BD26F6B02FB7

@ -1,6 +1,7 @@
#!/usr/bin/env python3
import json
import re
import sys
import yaml
@ -117,6 +118,12 @@ def _read_yaml(path):
except yaml.YAMLError as e:
return AnsibleError(e)
GROUPS_PATTERN_OPS = {
"": lambda old, add: old | add,
"!": lambda old, add: old - add,
}
GROUPS_PATTERN_OPS_NAMES = "".join(GROUPS_PATTERN_OPS.keys())
GROUPS_PATTERN = re.compile(r'^(?P<operation>[' + GROUPS_PATTERN_OPS_NAMES + r']?)(?P<group_name>[^' + GROUPS_PATTERN_OPS_NAMES + r'].*)$')
def _parse_group_aliasses(inv, data):
for group, syntax in data.items():
if isinstance(syntax, str):
@ -131,10 +138,10 @@ def _parse_group_aliasses(inv, data):
group_list.insert(0, 'all') # remove group from all for inversion
hosts = set()
for group_name in group_list:
if group_name[0] == '!':
hosts -= inv.all_hosts_of_group(group_name[1:])
else:
hosts |= inv.all_hosts_of_group(group_name)
group_matched = GROUPS_PATTERN.match(group_name)
add = inv.all_hosts_of_group(group_matched.group('group_name'))
op = GROUPS_PATTERN_OPS[group_matched.group('operation')]
hosts = op(hosts, add)
inv.add_hosts_to_group(hosts, group)
def _parse_groups(inv, data):

Loading…
Cancel
Save