mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-22 12:50:22 -07:00
Merge pull request #1474 from jhoekx/group-by-plugin-template
Add a group_by action plugin.
This commit is contained in:
commit
0853ece810
3 changed files with 117 additions and 0 deletions
71
lib/ansible/runner/action_plugins/group_by.py
Normal file
71
lib/ansible/runner/action_plugins/group_by.py
Normal file
|
@ -0,0 +1,71 @@
|
|||
# Copyright 2012, Jeroen Hoekx <jeroen@hoekx.be>
|
||||
#
|
||||
# This file is part of Ansible
|
||||
#
|
||||
# Ansible is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Ansible is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import ansible
|
||||
|
||||
from ansible.callbacks import vv
|
||||
from ansible.errors import AnsibleError as ae
|
||||
from ansible.runner.return_data import ReturnData
|
||||
from ansible.utils import parse_kv, template
|
||||
|
||||
class ActionModule(object):
|
||||
''' Create inventory groups based on variables '''
|
||||
|
||||
### We need to be able to modify the inventory
|
||||
BYPASS_HOST_LOOP = True
|
||||
|
||||
def __init__(self, runner):
|
||||
self.runner = runner
|
||||
|
||||
def run(self, conn, tmp, module_name, module_args, inject):
|
||||
args = parse_kv(self.runner.module_args)
|
||||
if not 'key' in args:
|
||||
raise ae("'key' is a required argument.")
|
||||
|
||||
vv("created 'group_by' ActionModule: key=%s"%(args['key']))
|
||||
|
||||
inventory = self.runner.inventory
|
||||
|
||||
result = {'changed': False}
|
||||
|
||||
### find all groups
|
||||
groups = {}
|
||||
for host in self.runner.host_set:
|
||||
data = inject['hostvars'][host]
|
||||
group_name = template(self.runner.basedir, args['key'], data)
|
||||
group_name = group_name.replace(' ','-')
|
||||
if group_name not in groups:
|
||||
groups[group_name] = []
|
||||
groups[group_name].append(host)
|
||||
|
||||
result['groups'] = groups
|
||||
|
||||
### add to inventory
|
||||
for group, hosts in groups.items():
|
||||
inv_group = inventory.get_group(group)
|
||||
if not inv_group:
|
||||
inv_group = ansible.inventory.Group(name=group)
|
||||
inventory.add_group(inv_group)
|
||||
for host in hosts:
|
||||
inv_host = inventory.get_host(host)
|
||||
if not inv_host:
|
||||
inv_host = ansible.inventory.Host(name=host)
|
||||
if inv_group not in inv_host.get_groups():
|
||||
result['changed'] = True
|
||||
inv_group.add_host(inv_host)
|
||||
|
||||
return ReturnData(conn=conn, comm_ok=True, result=result)
|
Loading…
Add table
Add a link
Reference in a new issue