Add a toggle for the leading separator for keyed_groups (#60882)

* Add a toggle for the leading separator for keyed_groups if no prefix is given

* changelog

* Add tests for backward compatibility and the new toggle
This commit is contained in:
Sloane Hertel 2020-10-23 15:57:44 -04:00 committed by GitHub
parent 0b2b82c0b6
commit 8b07d46166
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 89 additions and 0 deletions

View file

@ -0,0 +1,2 @@
minor_changes:
- constructed - Add a toggle to allow the separator to be omitted if no prefix has been provided.

View file

@ -39,4 +39,15 @@ options:
key: use_extra_vars key: use_extra_vars
env: env:
- name: ANSIBLE_INVENTORY_USE_EXTRA_VARS - name: ANSIBLE_INVENTORY_USE_EXTRA_VARS
leading_separator:
description:
- Use in conjunction with keyed_groups.
- By default, a keyed group that does not have a prefix or a separator provided will have a name that starts with an underscore.
- This is because the default prefix is "" and the default separator is "_".
- Set this option to False to omit the leading underscore (or other separator) if no prefix is given.
- If the group name is derived from a mapping the separator is still used to concatenate the items.
- To not use a separator in the group name at all, set the separator for the keyed group to an empty string instead.
type: boolean
default: True
version_added: '2.11'
''' '''

View file

@ -443,6 +443,8 @@ class Constructable(object):
raise AnsibleParserError("Invalid group name format, expected a string or a list of them or dictionary, got: %s" % type(key)) raise AnsibleParserError("Invalid group name format, expected a string or a list of them or dictionary, got: %s" % type(key))
for bare_name in new_raw_group_names: for bare_name in new_raw_group_names:
if prefix == '' and self.get_option('leading_separator') is False:
sep = ''
gname = self._sanitize_group_name('%s%s%s' % (prefix, sep, bare_name)) gname = self._sanitize_group_name('%s%s%s' % (prefix, sep, bare_name))
result_gname = self.inventory.add_group(gname) result_gname = self.inventory.add_group(gname)
self.inventory.add_host(host, result_gname) self.inventory.add_host(host, result_gname)

View file

@ -0,0 +1 @@
shippable/posix/group3

View file

@ -0,0 +1,19 @@
plugin: constructed
keyed_groups:
- key: hostvar0
- key: hostvar1
- key: hostvar2
- key: hostvar0
separator: 'separator'
- key: hostvar1
separator: 'separator'
- key: hostvar2
separator: 'separator'
- key: hostvar0
prefix: 'prefix'
- key: hostvar1
prefix: 'prefix'
- key: hostvar2
prefix: 'prefix'

View file

@ -0,0 +1,20 @@
plugin: constructed
keyed_groups:
- key: hostvar0
- key: hostvar1
- key: hostvar2
- key: hostvar0
separator: 'separator'
- key: hostvar1
separator: 'separator'
- key: hostvar2
separator: 'separator'
- key: hostvar0
prefix: 'prefix'
- key: hostvar1
prefix: 'prefix'
- key: hostvar2
prefix: 'prefix'
leading_separator: False

View file

@ -0,0 +1,25 @@
#!/usr/bin/env bash
set -ex
ansible-inventory -i static_inventory.yml -i constructed.yml --graph | tee out.txt
grep '@_hostvalue1' out.txt
grep '@_item0' out.txt
grep '@_key0_value0' out.txt
grep '@prefix_hostvalue1' out.txt
grep '@prefix_item0' out.txt
grep '@prefix_key0_value0' out.txt
grep '@separatorhostvalue1' out.txt
grep '@separatoritem0' out.txt
grep '@separatorkey0separatorvalue0' out.txt
ansible-inventory -i static_inventory.yml -i no_leading_separator_constructed.yml --graph | tee out.txt
grep '@hostvalue1' out.txt
grep '@item0' out.txt
grep '@key0_value0' out.txt
grep '@key0separatorvalue0' out.txt
grep '@prefix_hostvalue1' out.txt
grep '@prefix_item0' out.txt
grep '@prefix_key0_value0' out.txt

View file

@ -0,0 +1,8 @@
all:
hosts:
host0:
hostvar0:
key0: value0
hostvar1: hostvalue1
hostvar2:
- item0

View file

@ -33,6 +33,7 @@ def inventory_module():
r = InventoryModule() r = InventoryModule()
r.inventory = InventoryData() r.inventory = InventoryData()
r.templar = Templar(None) r.templar = Templar(None)
r._options = {'leading_separator': True}
return r return r