|
|
|
@ -20,11 +20,13 @@
|
|
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
|
|
from ansible.errors import AnsibleError
|
|
|
|
|
from ansible.errors import AnsibleActionFail
|
|
|
|
|
from ansible.module_utils.common._collections_compat import Mapping
|
|
|
|
|
from ansible.module_utils.six import string_types
|
|
|
|
|
from ansible.plugins.action import ActionBase
|
|
|
|
|
from ansible.parsing.utils.addresses import parse_address
|
|
|
|
|
from ansible.utils.display import Display
|
|
|
|
|
from ansible.utils.vars import combine_vars
|
|
|
|
|
|
|
|
|
|
display = Display()
|
|
|
|
|
|
|
|
|
@ -43,13 +45,18 @@ class ActionModule(ActionBase):
|
|
|
|
|
result = super(ActionModule, self).run(tmp, task_vars)
|
|
|
|
|
del tmp # tmp no longer has any effect
|
|
|
|
|
|
|
|
|
|
# Parse out any hostname:port patterns
|
|
|
|
|
new_name = self._task.args.get('name', self._task.args.get('hostname', self._task.args.get('host', None)))
|
|
|
|
|
args = self._task.args
|
|
|
|
|
raw = args.pop('_raw_params', {})
|
|
|
|
|
if isinstance(raw, Mapping):
|
|
|
|
|
# TODO: create 'conflict' detection in base class to deal with repeats and aliases and warn user
|
|
|
|
|
args = combine_vars(raw, args)
|
|
|
|
|
else:
|
|
|
|
|
raise AnsibleActionFail('Invalid raw parameters passed, requires a dictonary/mapping got a %s' % type(raw))
|
|
|
|
|
|
|
|
|
|
# Parse out any hostname:port patterns
|
|
|
|
|
new_name = args.get('name', args.get('hostname', args.get('host', None)))
|
|
|
|
|
if new_name is None:
|
|
|
|
|
result['failed'] = True
|
|
|
|
|
result['msg'] = 'name or hostname arg needs to be provided'
|
|
|
|
|
return result
|
|
|
|
|
raise AnsibleActionFail('name, host or hostname needs to be provided')
|
|
|
|
|
|
|
|
|
|
display.vv("creating host via 'add_host': hostname=%s" % new_name)
|
|
|
|
|
|
|
|
|
@ -61,9 +68,9 @@ class ActionModule(ActionBase):
|
|
|
|
|
port = None
|
|
|
|
|
|
|
|
|
|
if port:
|
|
|
|
|
self._task.args['ansible_ssh_port'] = port
|
|
|
|
|
args['ansible_ssh_port'] = port
|
|
|
|
|
|
|
|
|
|
groups = self._task.args.get('groupname', self._task.args.get('groups', self._task.args.get('group', '')))
|
|
|
|
|
groups = args.get('groupname', args.get('groups', args.get('group', '')))
|
|
|
|
|
# add it to the group if that was specified
|
|
|
|
|
new_groups = []
|
|
|
|
|
if groups:
|
|
|
|
@ -72,7 +79,7 @@ class ActionModule(ActionBase):
|
|
|
|
|
elif isinstance(groups, string_types):
|
|
|
|
|
group_list = groups.split(",")
|
|
|
|
|
else:
|
|
|
|
|
raise AnsibleError("Groups must be specified as a list.", obj=self._task)
|
|
|
|
|
raise AnsibleActionFail("Groups must be specified as a list.", obj=self._task)
|
|
|
|
|
|
|
|
|
|
for group_name in group_list:
|
|
|
|
|
if group_name not in new_groups:
|
|
|
|
@ -81,9 +88,9 @@ class ActionModule(ActionBase):
|
|
|
|
|
# Add any variables to the new_host
|
|
|
|
|
host_vars = dict()
|
|
|
|
|
special_args = frozenset(('name', 'hostname', 'groupname', 'groups'))
|
|
|
|
|
for k in self._task.args.keys():
|
|
|
|
|
for k in args.keys():
|
|
|
|
|
if k not in special_args:
|
|
|
|
|
host_vars[k] = self._task.args[k]
|
|
|
|
|
host_vars[k] = args[k]
|
|
|
|
|
|
|
|
|
|
result['changed'] = False
|
|
|
|
|
result['add_host'] = dict(host_name=name, groups=new_groups, host_vars=host_vars)
|
|
|
|
|