mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-08 14:22:46 +00:00
add support of nested groups in group_by
This commit is contained in:
@@ -18,6 +18,7 @@ from __future__ import (absolute_import, division, print_function)
|
||||
__metaclass__ = type
|
||||
|
||||
from ansible.plugins.action import ActionBase
|
||||
from ansible.module_utils.six import string_types
|
||||
|
||||
|
||||
class ActionModule(ActionBase):
|
||||
@@ -38,8 +39,11 @@ class ActionModule(ActionBase):
|
||||
return result
|
||||
|
||||
group_name = self._task.args.get('key')
|
||||
group_name = group_name.replace(' ', '-')
|
||||
parent_groups = self._task.args.get('parents', ['all'])
|
||||
if isinstance(parent_groups, string_types):
|
||||
parent_groups = [parent_groups]
|
||||
|
||||
result['changed'] = False
|
||||
result['add_group'] = group_name
|
||||
result['add_group'] = group_name.replace(' ', '-')
|
||||
result['parent_groups'] = [name.replace(' ', '-') for name in parent_groups]
|
||||
return result
|
||||
|
||||
@@ -632,12 +632,17 @@ class StrategyBase:
|
||||
# host object from the master inventory
|
||||
real_host = self._inventory.hosts[host.name]
|
||||
group_name = result_item.get('add_group')
|
||||
parent_group_names = result_item.get('parent_groups', [])
|
||||
|
||||
if group_name not in self._inventory.groups:
|
||||
# create the new group and add it to inventory
|
||||
self._inventory.add_group(group_name)
|
||||
changed = True
|
||||
for name in [group_name] + 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]
|
||||
parent_group.add_child_group(group)
|
||||
|
||||
if real_host.name not in group.get_hosts():
|
||||
group.add_host(real_host)
|
||||
|
||||
Reference in New Issue
Block a user