Fix strat inv (#58982)
* Fix strategy functions that update inventory * added tests
This commit is contained in:
parent
b0f38931b0
commit
a7b14ec1be
4 changed files with 24 additions and 2 deletions
2
changelogs/fragments/fix_strategy_inv_up.yml
Normal file
2
changelogs/fragments/fix_strategy_inv_up.yml
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
bugfixes:
|
||||||
|
- Fix strategy functions that update inventory and back 'add_host' and 'group_by' actions.
|
|
@ -711,7 +711,7 @@ class StrategyBase:
|
||||||
new_groups = host_info.get('groups', [])
|
new_groups = host_info.get('groups', [])
|
||||||
for group_name in new_groups:
|
for group_name in new_groups:
|
||||||
if group_name not in self._inventory.groups:
|
if group_name not in self._inventory.groups:
|
||||||
self._inventory.add_group(group_name)
|
group_name = self._inventory.add_group(group_name)
|
||||||
new_group = self._inventory.groups[group_name]
|
new_group = self._inventory.groups[group_name]
|
||||||
new_group.add_host(self._inventory.hosts[host_name])
|
new_group.add_host(self._inventory.hosts[host_name])
|
||||||
|
|
||||||
|
@ -738,11 +738,15 @@ class StrategyBase:
|
||||||
group_name = result_item.get('add_group')
|
group_name = result_item.get('add_group')
|
||||||
parent_group_names = result_item.get('parent_groups', [])
|
parent_group_names = result_item.get('parent_groups', [])
|
||||||
|
|
||||||
for name in [group_name] + parent_group_names:
|
if group_name not in self._inventory.groups:
|
||||||
|
group_name = self._inventory.add_group(group_name)
|
||||||
|
|
||||||
|
for name in parent_group_names:
|
||||||
if name not in self._inventory.groups:
|
if name not in self._inventory.groups:
|
||||||
# create the new group and add it to inventory
|
# create the new group and add it to inventory
|
||||||
self._inventory.add_group(name)
|
self._inventory.add_group(name)
|
||||||
changed = True
|
changed = True
|
||||||
|
|
||||||
group = self._inventory.groups[group_name]
|
group = self._inventory.groups[group_name]
|
||||||
for parent_group_name in parent_group_names:
|
for parent_group_name in parent_group_names:
|
||||||
parent_group = self._inventory.groups[parent_group_name]
|
parent_group = self._inventory.groups[parent_group_name]
|
||||||
|
|
|
@ -9,3 +9,7 @@ if [ "$?" != "1" ]; then
|
||||||
echo "Non-matching limit should cause failure"
|
echo "Non-matching limit should cause failure"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
ansible-playbook -i ../../inventory "$@" strategy.yml
|
||||||
|
ANSIBLE_TRANSFORM_INVALID_GROUP_CHARS=always ansible-playbook -i ../../inventory "$@" strategy.yml
|
||||||
|
ANSIBLE_TRANSFORM_INVALID_GROUP_CHARS=never ansible-playbook -i ../../inventory "$@" strategy.yml
|
||||||
|
|
12
test/integration/targets/inventory/strategy.yml
Normal file
12
test/integration/targets/inventory/strategy.yml
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
- name: Check that 'invalid' group works, problem exposed in #58980
|
||||||
|
hosts: localhost
|
||||||
|
tasks:
|
||||||
|
- name: add a host to a group, that has - to trigger substitution
|
||||||
|
add_host:
|
||||||
|
name: localhost
|
||||||
|
groups: Not-Working
|
||||||
|
|
||||||
|
- name: group hosts by distribution, with dash to trigger substitution
|
||||||
|
group_by:
|
||||||
|
key: "{{ ansible_distribution }}-{{ ansible_distribution_version }}"
|
||||||
|
changed_when: false
|
Loading…
Reference in a new issue