mirror of
https://github.com/ansible/awx.git
synced 2026-05-07 17:37:37 -02:30
Merge pull request #4298 from jladdjr/add_foreman_options
pass along all foreman options to plugin, add support for group_patterns
This commit is contained in:
@@ -4,6 +4,7 @@
|
|||||||
# Python
|
# Python
|
||||||
import datetime
|
import datetime
|
||||||
import time
|
import time
|
||||||
|
import json
|
||||||
import logging
|
import logging
|
||||||
import re
|
import re
|
||||||
import copy
|
import copy
|
||||||
@@ -2578,11 +2579,26 @@ class satellite6(PluginFileInjector):
|
|||||||
def inventory_as_dict(self, inventory_update, private_data_dir):
|
def inventory_as_dict(self, inventory_update, private_data_dir):
|
||||||
ret = super(satellite6, self).inventory_as_dict(inventory_update, private_data_dir)
|
ret = super(satellite6, self).inventory_as_dict(inventory_update, private_data_dir)
|
||||||
|
|
||||||
|
group_patterns = '[]'
|
||||||
|
group_prefix = 'foreman_'
|
||||||
|
want_hostcollections = False
|
||||||
want_ansible_ssh_host = False
|
want_ansible_ssh_host = False
|
||||||
|
want_facts = True
|
||||||
|
|
||||||
foreman_opts = inventory_update.source_vars_dict.copy()
|
foreman_opts = inventory_update.source_vars_dict.copy()
|
||||||
for k, v in foreman_opts.items():
|
for k, v in foreman_opts.items():
|
||||||
if k == 'satellite6_want_ansible_ssh_host' and isinstance(v, bool):
|
if k == 'satellite6_group_patterns' and isinstance(v, str):
|
||||||
|
group_patterns = v
|
||||||
|
elif k == 'satellite6_group_prefix' and isinstance(v, str):
|
||||||
|
group_prefix = v
|
||||||
|
elif k == 'satellite6_want_hostcollections' and isinstance(v, bool):
|
||||||
|
want_hostcollections = v
|
||||||
|
elif k == 'satellite6_want_ansible_ssh_host' and isinstance(v, bool):
|
||||||
want_ansible_ssh_host = v
|
want_ansible_ssh_host = v
|
||||||
|
elif k == 'satellite6_want_facts' and isinstance(v, bool):
|
||||||
|
want_facts = v
|
||||||
|
else:
|
||||||
|
ret[k] = str(v)
|
||||||
|
|
||||||
# Compatibility content
|
# Compatibility content
|
||||||
group_by_hostvar = {
|
group_by_hostvar = {
|
||||||
@@ -2605,13 +2621,61 @@ class satellite6(PluginFileInjector):
|
|||||||
"key": "foreman['content_facet_attributes']['content_view_name'] | "
|
"key": "foreman['content_facet_attributes']['content_view_name'] | "
|
||||||
"lower | regex_replace(' ', '') | regex_replace('[^A-Za-z0-9\_]', '_')"}
|
"lower | regex_replace(' ', '') | regex_replace('[^A-Za-z0-9\_]', '_')"}
|
||||||
}
|
}
|
||||||
ret['keyed_groups'] = [group_by_hostvar[grouping_name] for grouping_name in group_by_hostvar]
|
|
||||||
ret['legacy_hostvars'] = True
|
ret['legacy_hostvars'] = True # convert hostvar structure to the form used by the script
|
||||||
ret['want_facts'] = True
|
|
||||||
ret['want_params'] = True
|
ret['want_params'] = True
|
||||||
|
ret['group_prefix'] = group_prefix
|
||||||
|
ret['want_hostcollections'] = want_hostcollections
|
||||||
|
ret['want_facts'] = want_facts
|
||||||
|
|
||||||
if want_ansible_ssh_host:
|
if want_ansible_ssh_host:
|
||||||
ret['compose'] = {'ansible_ssh_host': "foreman['ip6'] | default(foreman['ip'], true)"}
|
ret['compose'] = {'ansible_ssh_host': "foreman['ip6'] | default(foreman['ip'], true)"}
|
||||||
|
ret['keyed_groups'] = [group_by_hostvar[grouping_name] for grouping_name in group_by_hostvar]
|
||||||
|
|
||||||
|
def form_keyed_group(group_pattern):
|
||||||
|
"""
|
||||||
|
Converts foreman group_pattern to
|
||||||
|
inventory plugin keyed_group
|
||||||
|
|
||||||
|
e.g. {app_param}-{tier_param}-{dc_param}
|
||||||
|
becomes
|
||||||
|
"%s-%s-%s" | format(app_param, tier_param, dc_param)
|
||||||
|
"""
|
||||||
|
if type(group_pattern) is not str:
|
||||||
|
return None
|
||||||
|
params = re.findall('{[^}]*}', group_pattern)
|
||||||
|
if len(params) == 0:
|
||||||
|
return None
|
||||||
|
|
||||||
|
param_names = []
|
||||||
|
for p in params:
|
||||||
|
param_names.append(p[1:-1].strip()) # strip braces and space
|
||||||
|
|
||||||
|
# form keyed_group key by
|
||||||
|
# replacing curly braces with '%s'
|
||||||
|
# (for use with jinja's format filter)
|
||||||
|
key = group_pattern
|
||||||
|
for p in params:
|
||||||
|
key = key.replace(p, '%s', 1)
|
||||||
|
|
||||||
|
# apply jinja filter to key
|
||||||
|
key = '"{}" | format({})'.format(key, ', '.join(param_names))
|
||||||
|
|
||||||
|
keyed_group = {'key': key,
|
||||||
|
'separator': ''}
|
||||||
|
return keyed_group
|
||||||
|
|
||||||
|
try:
|
||||||
|
group_patterns = json.loads(group_patterns)
|
||||||
|
|
||||||
|
if type(group_patterns) is list:
|
||||||
|
for group_pattern in group_patterns:
|
||||||
|
keyed_group = form_keyed_group(group_pattern)
|
||||||
|
if keyed_group:
|
||||||
|
ret['keyed_groups'].append(keyed_group)
|
||||||
|
except json.JSONDecodeError:
|
||||||
|
logger.warning('Could not parse group_patterns. Expected JSON-formatted string, found: {}'
|
||||||
|
.format(group_patterns))
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
|
base_source_var: value_of_var
|
||||||
compose:
|
compose:
|
||||||
ansible_ssh_host: foreman['ip6'] | default(foreman['ip'], true)
|
ansible_ssh_host: foreman['ip6'] | default(foreman['ip'], true)
|
||||||
|
group_prefix: foo_group_prefix
|
||||||
keyed_groups:
|
keyed_groups:
|
||||||
- key: foreman['environment_name'] | lower | regex_replace(' ', '') | regex_replace('[^A-Za-z0-9\_]', '_') | regex_replace('none', '')
|
- key: foreman['environment_name'] | lower | regex_replace(' ', '') | regex_replace('[^A-Za-z0-9\_]', '_') | regex_replace('none', '')
|
||||||
prefix: foreman_environment_
|
prefix: foreman_environment_
|
||||||
@@ -16,7 +18,12 @@ keyed_groups:
|
|||||||
- key: foreman['content_facet_attributes']['content_view_name'] | lower | regex_replace(' ', '') | regex_replace('[^A-Za-z0-9\_]', '_')
|
- key: foreman['content_facet_attributes']['content_view_name'] | lower | regex_replace(' ', '') | regex_replace('[^A-Za-z0-9\_]', '_')
|
||||||
prefix: foreman_content_view_
|
prefix: foreman_content_view_
|
||||||
separator: ''
|
separator: ''
|
||||||
|
- key: '"%s-%s-%s" | format(app, tier, color)'
|
||||||
|
separator: ''
|
||||||
|
- key: '"%s-%s" | format(app, color)'
|
||||||
|
separator: ''
|
||||||
legacy_hostvars: true
|
legacy_hostvars: true
|
||||||
plugin: theforeman.foreman.foreman
|
plugin: theforeman.foreman.foreman
|
||||||
want_facts: true
|
want_facts: true
|
||||||
|
want_hostcollections: true
|
||||||
want_params: true
|
want_params: true
|
||||||
|
|||||||
@@ -6,12 +6,12 @@ user = fooo
|
|||||||
password = fooo
|
password = fooo
|
||||||
|
|
||||||
[ansible]
|
[ansible]
|
||||||
group_patterns = foo_group_patterns
|
group_patterns = ["{app}-{tier}-{color}", "{app}-{color}"]
|
||||||
want_facts = True
|
want_facts = True
|
||||||
want_hostcollections = True
|
want_hostcollections = True
|
||||||
group_prefix = foo_group_prefix
|
group_prefix = foo_group_prefix
|
||||||
want_ansible_ssh_host = True
|
want_ansible_ssh_host = True
|
||||||
rich_params = True
|
rich_params = False
|
||||||
|
|
||||||
[cache]
|
[cache]
|
||||||
path = /tmp
|
path = /tmp
|
||||||
|
|||||||
@@ -64,11 +64,10 @@ INI_TEST_VARS = {
|
|||||||
'tags': 'Creator:jmarshall, peanutbutter:jelly'
|
'tags': 'Creator:jmarshall, peanutbutter:jelly'
|
||||||
},
|
},
|
||||||
'satellite6': {
|
'satellite6': {
|
||||||
'satellite6_group_patterns': 'foo_group_patterns',
|
'satellite6_group_patterns': '["{app}-{tier}-{color}", "{app}-{color}"]',
|
||||||
'satellite6_group_prefix': 'foo_group_prefix',
|
'satellite6_group_prefix': 'foo_group_prefix',
|
||||||
'satellite6_want_hostcollections': True,
|
'satellite6_want_hostcollections': True,
|
||||||
'satellite6_want_ansible_ssh_host': True,
|
'satellite6_want_ansible_ssh_host': True,
|
||||||
'satellite6_rich_params': True,
|
|
||||||
'satellite6_want_facts': True
|
'satellite6_want_facts': True
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user