| 1 |
# (c) 2012-2014, Michael DeHaan <[email protected]> |
| 2 |
# Copyright 2012, Seth Vidal <[email protected]> |
| 3 |
# |
| 4 |
# This file is part of Ansible |
| 5 |
# |
| 6 |
# Ansible is free software: you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU General Public License as published by |
| 8 |
# the Free Software Foundation, either version 3 of the License, or |
| 9 |
# (at your option) any later version. |
| 10 |
# |
| 11 |
# Ansible is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License |
| 17 |
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
|
| 19 |
from __future__ import annotations |
| 20 |
|
| 21 |
from collections.abc import Mapping |
| 22 |
|
| 23 |
from ansible.errors import AnsibleActionFail |
| 24 |
from ansible.plugins.action import ActionBase |
| 25 |
from ansible.parsing.utils.addresses import parse_address |
| 26 |
from ansible.utils.display import Display |
| 27 |
from ansible.utils.vars import combine_vars |
| 28 |
|
| 29 |
display = Display() |
| 30 |
|
| 31 |
|
| 32 |
class ActionModule(ActionBase): |
| 33 |
""" Create inventory hosts and groups in the memory inventory""" |
| 34 |
|
| 35 |
# We need to be able to modify the inventory |
| 36 |
BYPASS_HOST_LOOP = True |
| 37 |
_requires_connection = False |
| 38 |
_supports_check_mode = True |
| 39 |
|
| 40 |
def run(self, tmp=None, task_vars=None): |
| 41 |
|
| 42 |
result = super(ActionModule, self).run(tmp, task_vars) |
| 43 |
del tmp # tmp no longer has any effect |
| 44 |
|
| 45 |
args = self._task.args |
| 46 |
raw = args.pop('_raw_params', {}) |
| 47 |
if isinstance(raw, Mapping): |
| 48 |
# TODO: create 'conflict' detection in base class to deal with repeats and aliases and warn user |
| 49 |
args = combine_vars(raw, args) |
| 50 |
else: |
| 51 |
raise AnsibleActionFail('Invalid raw parameters passed, requires a dictionary/mapping got a %s' % type(raw)) |
| 52 |
|
| 53 |
# Parse out any hostname:port patterns |
| 54 |
new_name = args.get('name', args.get('hostname', args.get('host', None))) |
| 55 |
if new_name is None: |
| 56 |
raise AnsibleActionFail('name, host or hostname needs to be provided') |
| 57 |
|
| 58 |
display.vv("creating host via 'add_host': hostname=%s" % new_name) |
| 59 |
|
| 60 |
try: |
| 61 |
name, port = parse_address(new_name, allow_ranges=False) |
| 62 |
except Exception: |
| 63 |
# not a parsable hostname, but might still be usable |
| 64 |
name = new_name |
| 65 |
port = None |
| 66 |
|
| 67 |
if port: |
| 68 |
args['ansible_ssh_port'] = port |
| 69 |
|
| 70 |
groups = args.get('groupname', args.get('groups', args.get('group', ''))) |
| 71 |
# add it to the group if that was specified |
| 72 |
new_groups = [] |
| 73 |
if groups: |
| 74 |
if isinstance(groups, list): |
| 75 |
group_list = groups |
| 76 |
elif isinstance(groups, str): |
| 77 |
group_list = groups.split(",") |
| 78 |
else: |
| 79 |
raise AnsibleActionFail("Groups must be specified as a list.", obj=groups) |
| 80 |
|
| 81 |
for group_name in group_list: |
| 82 |
if group_name not in new_groups: |
| 83 |
new_groups.append(group_name.strip()) |
| 84 |
|
| 85 |
# Add any variables to the new_host |
| 86 |
host_vars = dict() |
| 87 |
special_args = frozenset(('name', 'hostname', 'groupname', 'groups')) |
| 88 |
for k in args.keys(): |
| 89 |
if k not in special_args: |
| 90 |
host_vars[k] = args[k] |
| 91 |
|
| 92 |
result['add_host'] = dict(host_name=name, groups=new_groups, host_vars=host_vars) |
| 93 |
result['changed'] = self.add_host(host_name=name, parent_group_names=new_groups, host_vars=host_vars) |
| 94 |
|
| 95 |
return result |