Fix strat inv (#58982)

* Fix strategy functions that update inventory

* added tests
pull/58642/merge
Brian Coca 5 years ago committed by GitHub
parent b0f38931b0
commit a7b14ec1be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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', [])
for group_name in new_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.add_host(self._inventory.hosts[host_name])
@ -738,11 +738,15 @@ class StrategyBase:
group_name = result_item.get('add_group')
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:
# create the new group and add it to inventory
self._inventory.add_group(name)
changed = True
group = self._inventory.groups[group_name]
for parent_group_name in parent_group_names:
parent_group = self._inventory.groups[parent_group_name]

@ -9,3 +9,7 @@ if [ "$?" != "1" ]; then
echo "Non-matching limit should cause failure"
exit 1
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

@ -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…
Cancel
Save