mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-30 14:21:26 -07:00
Overridable safety (#53458)
* create overridable sanitation function * now used in aws, gce and azure plugins * added new option to these plugins to work in conjunction with general toggle to make it easier to emulate inventory script behavior.
This commit is contained in:
parent
e9f9bcae6a
commit
3e52a6a693
5 changed files with 88 additions and 12 deletions
|
@ -69,6 +69,20 @@ DOCUMENTATION = '''
|
|||
False in the inventory config file which will allow 403 errors to be gracefully skipped.
|
||||
type: bool
|
||||
default: True
|
||||
use_contrib_script_compatible_sanitization:
|
||||
description:
|
||||
- By default this plugin is using a general group name sanitization to create safe and usable group names for use in Ansible.
|
||||
This option allows you to override that, in efforts to allow migration from the old inventory script and
|
||||
matches the sanitization of groups when the script's ``replace_dash_in_groups`` option is set to ``False``.
|
||||
To replicate behavior of ``replace_dash_in_groups = True`` with constructed groups,
|
||||
you will need to replace hyphens with underscores via the regex_replace filter for those entries.
|
||||
- For this to work you should also turn off the TRANSFORM_INVALID_GROUP_CHARS setting,
|
||||
otherwise the core engine will just use the standard sanitization on top.
|
||||
- This is not the default as such names break certain functionality as not all characters are valid Python identifiers
|
||||
which group names end up being used as.
|
||||
type: bool
|
||||
default: False
|
||||
version_added: '2.8'
|
||||
'''
|
||||
|
||||
EXAMPLES = '''
|
||||
|
@ -135,12 +149,13 @@ compose:
|
|||
# (note: this does not modify inventory_hostname, which is set via I(hostnames))
|
||||
ansible_host: private_ip_address
|
||||
'''
|
||||
import re
|
||||
|
||||
from ansible.errors import AnsibleError
|
||||
from ansible.module_utils._text import to_native, to_text
|
||||
from ansible.module_utils.ec2 import ansible_dict_to_boto3_filter_list, boto3_tag_list_to_ansible_dict
|
||||
from ansible.module_utils.ec2 import camel_dict_to_snake_dict
|
||||
from ansible.plugins.inventory import BaseInventoryPlugin, Constructable, Cacheable, to_safe_group_name
|
||||
from ansible.plugins.inventory import BaseInventoryPlugin, Constructable, Cacheable
|
||||
from ansible.utils.display import Display
|
||||
|
||||
try:
|
||||
|
@ -431,7 +446,7 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
|
|||
break
|
||||
if hostname:
|
||||
if ':' in to_text(hostname):
|
||||
return to_safe_group_name(to_text(hostname))
|
||||
return self._sanitize_group_name((to_text(hostname)))
|
||||
else:
|
||||
return to_text(hostname)
|
||||
|
||||
|
@ -518,9 +533,14 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
|
|||
return False
|
||||
|
||||
def parse(self, inventory, loader, path, cache=True):
|
||||
|
||||
super(InventoryModule, self).parse(inventory, loader, path)
|
||||
|
||||
config_data = self._read_config_data(path)
|
||||
self._read_config_data(path)
|
||||
|
||||
if self.get_option('use_contrib_script_compatible_sanitization'):
|
||||
self._sanitize_group_name = self._legacy_script_compatible_group_sanitization
|
||||
|
||||
self._set_credentials()
|
||||
|
||||
# get user specifications
|
||||
|
@ -553,3 +573,11 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
|
|||
# when the user is using caching, update the cached inventory
|
||||
if cache_needs_update or (not cache and self.get_option('cache')):
|
||||
self.cache.set(cache_key, results)
|
||||
|
||||
@staticmethod
|
||||
def _legacy_script_compatible_group_sanitization(name):
|
||||
|
||||
# note that while this mirrors what the script used to do, it has many issues with unicode and usability in python
|
||||
regex = re.compile(r"[^A-Za-z0-9\_\-]")
|
||||
|
||||
return regex.sub('_', name)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue