Skip to content

Commit

Permalink
Handle incorrect data type in list lookup plugin (ansible#35483)
Browse files Browse the repository at this point in the history
handle incorrect data type in list lookup plugin
Fixes ansible#35481
test to ensure that loops properly handle incorrect datatypes

Signed-off-by: Adam Miller <admiller@redhat.com>
  • Loading branch information
maxamillion authored and ilicmilan committed Aug 15, 2018
1 parent 7333295 commit 44df6b1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/ansible/plugins/lookup/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

# Make coding more python3-ish
from __future__ import (absolute_import, division, print_function)

__metaclass__ = type

DOCUMENTATION = """
Expand All @@ -27,10 +28,15 @@
_list:
description: basically the same as you fed in
"""
import collections

from ansible.plugins.lookup import LookupBase
from ansible.errors import AnsibleError


class LookupModule(LookupBase):

def run(self, terms, **kwargs):
if not isinstance(terms, collections.Sequence):
raise AnsibleError("with_list expects a list")
return terms
21 changes: 21 additions & 0 deletions test/integration/targets/loops/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,24 @@
that:
- "output.results[0]['_ansible_item_label'] == 'looped_var foo_label'"
- "output.results[1]['_ansible_item_label'] == 'looped_var bar_label'"

# The following test cases are to ensure that we don't have a regression on
# GitHub Issue https://github.com/ansible/ansible/issues/35481
#
# This should execute and not cause a RuntimeError
- debug:
msg: "with_dict passed a list: {{item}}"
with_dict: "{{ a_list }}"
register: with_dict_passed_a_list
ignore_errors: True
- assert:
that:
- with_dict_passed_a_list is failed
- debug:
msg: "with_list passed a dict: {{item}}"
with_list: "{{ a_dict }}"
register: with_list_passed_a_dict
ignore_errors: True
- assert:
that:
- with_list_passed_a_dict is failed

0 comments on commit 44df6b1

Please sign in to comment.