Add intg test to repro #36045 (add_host traceback) (#69912)

* Add intg test to repro #36045 (add_host traceback)

* fix raw_params usage in add_host

Co-authored-by: Adrian Likins <alikins@redhat.com>
pull/21071/head
Brian Coca 5 years ago committed by GitHub
parent 03f8bf46c3
commit ce199ef0e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,2 @@
bugfixes:
- Correctly process raw_params in add_hosts.

@ -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)

@ -16,6 +16,38 @@
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
# See https://github.com/ansible/ansible/issues/36045
- set_fact:
inventory_data:
ansible_ssh_common_args: "-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
# ansible_ssh_host: "127.0.0.3"
ansible_host: "127.0.0.3"
ansible_ssh_pass: "foobar"
# ansible_ssh_port: "2222"
ansible_port: "2222"
ansible_ssh_private_key_file: "/tmp/inventory-cloudj9cGz5/identity"
ansible_ssh_user: "root"
hostname: "newdynamichost2"
- name: Show inventory_data for 36045
debug:
msg: "{{ inventory_data }}"
- name: Add host from dict 36045
add_host: "{{ inventory_data }}"
- name: show newly added host
debug:
msg: "{{hostvars['newdynamichost2'].group_names}}"
- name: ensure that dynamically-added newdynamichost2 is visible via hostvars, groups 36045
assert:
that:
- hostvars['newdynamichost2'] is defined
- hostvars['newdynamichost2'].group_names is defined
# end of https://github.com/ansible/ansible/issues/36045 related tests
- name: add a host to the runtime inventory
add_host:
name: newdynamichost
@ -115,3 +147,13 @@
- "'testhost01' in groups['testhostgroup']"
- "'testhost02' in groups['testhostgroup']"
- hostvars['testhost01']['foo'] == 'bar'
- name: Give invalid input
add_host: namenewdynamichost groupsnewdynamicgroup a_varfromadd_host
ignore_errors: true
register: badinput
- name: verify we detected bad input
assert:
that:
- badinput is failed

Loading…
Cancel
Save