2012-10-27 17:47:47 +00:00
|
|
|
# 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
|
2013-04-11 16:42:41 +00:00
|
|
|
from ansible.utils import parse_kv, check_conditional
|
|
|
|
import ansible.utils.template as template
|
2012-10-27 17:47:47 +00:00
|
|
|
|
|
|
|
class ActionModule(object):
|
|
|
|
''' Create inventory groups based on variables '''
|
|
|
|
|
|
|
|
### We need to be able to modify the inventory
|
|
|
|
BYPASS_HOST_LOOP = True
|
2013-12-11 09:55:56 +00:00
|
|
|
TRANSFERS_FILES = False
|
2012-10-27 17:47:47 +00:00
|
|
|
|
|
|
|
def __init__(self, runner):
|
|
|
|
self.runner = runner
|
|
|
|
|
2013-02-17 20:01:49 +00:00
|
|
|
def run(self, conn, tmp, module_name, module_args, inject, complex_args=None, **kwargs):
|
2013-02-04 00:46:25 +00:00
|
|
|
|
|
|
|
# the group_by module does not need to pay attention to check mode.
|
|
|
|
# it always runs.
|
|
|
|
|
2014-05-07 02:30:37 +00:00
|
|
|
# module_args and complex_args have already been templated for the first host.
|
|
|
|
# Use them here only to check that a key argument is provided.
|
2013-02-28 13:27:42 +00:00
|
|
|
args = {}
|
|
|
|
if complex_args:
|
|
|
|
args.update(complex_args)
|
2014-05-04 12:49:12 +00:00
|
|
|
args.update(parse_kv(module_args))
|
2012-10-27 17:47:47 +00:00
|
|
|
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 = {}
|
2013-05-11 18:00:16 +00:00
|
|
|
|
2012-10-27 17:47:47 +00:00
|
|
|
for host in self.runner.host_set:
|
2013-05-16 16:57:05 +00:00
|
|
|
data = {}
|
|
|
|
data.update(inject)
|
|
|
|
data.update(inject['hostvars'][host])
|
2013-07-22 14:11:19 +00:00
|
|
|
conds = self.runner.conditional
|
|
|
|
if type(conds) != list:
|
|
|
|
conds = [ conds ]
|
2013-09-28 20:02:43 +00:00
|
|
|
next_host = False
|
2013-07-22 14:11:19 +00:00
|
|
|
for cond in conds:
|
|
|
|
if not check_conditional(cond, self.runner.basedir, data, fail_on_undefined=self.runner.error_on_undefined_vars):
|
2013-09-28 20:02:43 +00:00
|
|
|
next_host = True
|
|
|
|
break
|
|
|
|
if next_host:
|
|
|
|
continue
|
2014-05-07 02:30:37 +00:00
|
|
|
|
|
|
|
# Template original module_args and complex_args from runner for each host.
|
|
|
|
host_module_args = template.template(self.runner.basedir, self.runner.module_args, data)
|
|
|
|
host_complex_args = template.template(self.runner.basedir, self.runner.complex_args, data)
|
|
|
|
host_args = {}
|
|
|
|
if host_complex_args:
|
|
|
|
host_args.update(host_complex_args)
|
|
|
|
host_args.update(parse_kv(host_module_args))
|
|
|
|
|
|
|
|
group_name = host_args['key']
|
2012-10-27 17:47:47 +00:00
|
|
|
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)
|
2014-07-16 19:16:27 +00:00
|
|
|
inv_group.vars = inventory.get_group_variables(group, update_cached=False, vault_password=inventory._vault_password)
|
2012-10-27 17:47:47 +00:00
|
|
|
for host in hosts:
|
2014-03-24 20:59:43 +00:00
|
|
|
if host in self.runner.inventory._vars_per_host:
|
|
|
|
del self.runner.inventory._vars_per_host[host]
|
2012-10-27 17:47:47 +00:00
|
|
|
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)
|