Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix strat inv #58982

Merged
merged 3 commits into from
Jul 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/fix_strategy_inv_up.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- Fix strategy functions that update inventory and back 'add_host' and 'group_by' actions.
8 changes: 6 additions & 2 deletions lib/ansible/plugins/strategy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ def _add_host(self, host_info, iterator):
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])

Expand All @@ -738,11 +738,15 @@ def _add_group(self, host, result_item):
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]
Expand Down
4 changes: 4 additions & 0 deletions test/integration/targets/inventory/runme.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 12 additions & 0 deletions test/integration/targets/inventory/strategy.yml
Original file line number Diff line number Diff line change
@@ -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