2012-10-30 22:36:33 +01:00
|
|
|
# Copyright 2012, Seth Vidal <skvidal@fedoraproject.org>
|
|
|
|
#
|
|
|
|
# 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-12 16:23:53 +02:00
|
|
|
from ansible.utils import parse_kv
|
2012-10-30 22:36:33 +01:00
|
|
|
from ansible.inventory.host import Host
|
|
|
|
from ansible.inventory.group import Group
|
|
|
|
|
|
|
|
class ActionModule(object):
|
|
|
|
''' Create inventory hosts and groups in the memory inventory'''
|
|
|
|
|
|
|
|
### We need to be able to modify the inventory
|
|
|
|
BYPASS_HOST_LOOP = True
|
2012-11-28 16:22:35 +01:00
|
|
|
NEEDS_TMPPATH = False
|
2012-10-30 22:36:33 +01:00
|
|
|
|
|
|
|
def __init__(self, runner):
|
|
|
|
self.runner = runner
|
|
|
|
|
Fixing syntax error
running install_lib
byte-compiling /usr/lib/python2.6/site-packages/ansible/runner/action_plugins/add_host.py to add_host.pyc
SyntaxError: ('invalid syntax', ('/usr/lib/python2.6/site-packages/ansible/runner/action_plugins/add_host.py', 37, 92, ' def run(self, conn, tmp, module_name, module_args, inject, complex_args=None, **kwargs)\n'))
2013-02-18 00:35:58 +01:00
|
|
|
def run(self, conn, tmp, module_name, module_args, inject, complex_args=None, **kwargs):
|
2013-02-04 01:46:25 +01:00
|
|
|
|
2013-08-20 23:09:44 +02:00
|
|
|
if self.runner.noop_on_check(inject):
|
2013-02-04 12:21:33 +01:00
|
|
|
return ReturnData(conn=conn, comm_ok=True, result=dict(skipped=True, msg='check mode not supported for this module'))
|
2013-02-04 01:46:25 +01:00
|
|
|
|
2013-02-28 14:27:42 +01:00
|
|
|
args = {}
|
|
|
|
if complex_args:
|
|
|
|
args.update(complex_args)
|
|
|
|
args.update(parse_kv(module_args))
|
2013-02-18 02:45:37 +01:00
|
|
|
if not 'hostname' in args and not 'name' in args:
|
|
|
|
raise ae("'name' is a required argument.")
|
2012-10-30 22:36:33 +01:00
|
|
|
|
2013-06-25 19:58:55 +02:00
|
|
|
result = {}
|
2012-10-30 22:36:33 +01:00
|
|
|
|
2013-02-17 02:27:38 +01:00
|
|
|
# Parse out any hostname:port patterns
|
2013-06-19 06:11:52 +02:00
|
|
|
new_name = args.get('name', args.get('hostname', None))
|
|
|
|
vv("creating host via 'add_host': hostname=%s" % new_name)
|
2013-02-18 02:45:37 +01:00
|
|
|
|
2013-06-19 06:11:52 +02:00
|
|
|
if ":" in new_name:
|
|
|
|
new_name, new_port = new_name.split(":")
|
2013-02-17 02:27:38 +01:00
|
|
|
args['ansible_ssh_port'] = new_port
|
|
|
|
|
|
|
|
# create host and get inventory
|
2013-06-19 06:11:52 +02:00
|
|
|
new_host = Host(new_name)
|
2012-10-30 22:36:33 +01:00
|
|
|
inventory = self.runner.inventory
|
|
|
|
|
2013-02-17 02:27:38 +01:00
|
|
|
# Add any variables to the new_host
|
|
|
|
for k in args.keys():
|
2013-02-18 02:45:37 +01:00
|
|
|
if not k in [ 'name', 'hostname', 'groupname', 'groups' ]:
|
2013-02-17 02:27:38 +01:00
|
|
|
new_host.set_variable(k, args[k])
|
|
|
|
|
|
|
|
|
2012-10-30 22:36:33 +01:00
|
|
|
# add the new host to the 'all' group
|
|
|
|
allgroup = inventory.get_group('all')
|
|
|
|
allgroup.add_host(new_host)
|
2013-02-17 17:22:50 +01:00
|
|
|
|
|
|
|
groupnames = args.get('groupname', args.get('groups', ''))
|
2012-10-30 22:36:33 +01:00
|
|
|
# add it to the group if that was specified
|
2013-02-17 17:22:50 +01:00
|
|
|
if groupnames != '':
|
|
|
|
for group_name in groupnames.split(","):
|
|
|
|
if not inventory.get_group(group_name):
|
|
|
|
new_group = Group(group_name)
|
|
|
|
inventory.add_group(new_group)
|
|
|
|
grp = inventory.get_group(group_name)
|
|
|
|
grp.add_host(new_host)
|
2013-07-08 11:18:33 +02:00
|
|
|
vv("added host to group via add_host module: %s" % group_name)
|
2013-02-17 17:22:50 +01:00
|
|
|
result['new_groups'] = groupnames.split(",")
|
2012-10-30 22:36:33 +01:00
|
|
|
|
2013-06-19 06:11:52 +02:00
|
|
|
result['new_host'] = new_name
|
2012-10-30 22:36:33 +01:00
|
|
|
|
|
|
|
return ReturnData(conn=conn, comm_ok=True, result=result)
|
|
|
|
|
|
|
|
|
|
|
|
|