Skip to content

Commit

Permalink
Add intg test to repro ansible#36045 (add_host traceback) (ansible#69912
Browse files Browse the repository at this point in the history
)

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

* fix raw_params usage in add_host

Co-authored-by: Adrian Likins <[email protected]>
  • Loading branch information
bcoca and alikins authored Jun 9, 2020
1 parent 03f8bf4 commit ce199ef
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 11 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/add_hosts_fix.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- Correctly process raw_params in add_hosts.
29 changes: 18 additions & 11 deletions lib/ansible/plugins/action/add_host.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -43,13 +45,18 @@ def run(self, tmp=None, task_vars=None):
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)

Expand All @@ -61,9 +68,9 @@ def run(self, tmp=None, task_vars=None):
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:
Expand All @@ -72,7 +79,7 @@ def run(self, tmp=None, task_vars=None):
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:
Expand All @@ -81,9 +88,9 @@ def run(self, tmp=None, task_vars=None):
# 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)
Expand Down
42 changes: 42 additions & 0 deletions test/integration/targets/add_host/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit ce199ef

Please sign in to comment.