From 52cd7c7cc37e9869b2adac3462a61d0ae18a9f93 Mon Sep 17 00:00:00 2001 From: Sloane Hertel <19572925+s-hertel@users.noreply.github.com> Date: Wed, 28 Aug 2024 13:51:44 -0400 Subject: [PATCH] [2.17] csvfile lookup - fix giving an error when no search term is provided (#83710) (#83732) * csvfile lookup - fix giving an error when no search term is provided (#83710) Fixes #83689 (cherry picked from commit 26c8a28d050422553df229743ffc86380dc50b81) * Fix csvfile test - quote file argument (#83751) file was intended to be a string, not an undefined variable (cherry picked from commit 97a60c1e86eebaef3b28dbb84e62fd6d790619fe) --- ...onsistent-csvfile-missing-search-error.yml | 2 ++ lib/ansible/plugins/lookup/csvfile.py | 20 +++++++++++++++++++ .../targets/lookup_csvfile/tasks/main.yml | 9 +++++++++ 3 files changed, 31 insertions(+) create mode 100644 changelogs/fragments/fix-inconsistent-csvfile-missing-search-error.yml diff --git a/changelogs/fragments/fix-inconsistent-csvfile-missing-search-error.yml b/changelogs/fragments/fix-inconsistent-csvfile-missing-search-error.yml new file mode 100644 index 00000000000..9d0dcf935c6 --- /dev/null +++ b/changelogs/fragments/fix-inconsistent-csvfile-missing-search-error.yml @@ -0,0 +1,2 @@ +bugfixes: + - csvfile lookup - give an error when no search term is provided using modern config syntax (https://github.com/ansible/ansible/issues/83689). diff --git a/lib/ansible/plugins/lookup/csvfile.py b/lib/ansible/plugins/lookup/csvfile.py index 9d199d82190..37f927121b7 100644 --- a/lib/ansible/plugins/lookup/csvfile.py +++ b/lib/ansible/plugins/lookup/csvfile.py @@ -12,6 +12,7 @@ DOCUMENTATION = r""" - The csvfile lookup reads the contents of a file in CSV (comma-separated value) format. The lookup looks for the row where the first column matches keyname (which can be multiple words) and returns the value in the O(col) column (default 1, which indexed from 0 means the second column in the file). + - At least one keyname is required, provided as a positional argument(s) to the lookup. options: col: description: column to return (0 indexed). @@ -63,6 +64,22 @@ EXAMPLES = """ vars: csvline: "{{ lookup('ansible.builtin.csvfile', bgp_neighbor_ip, file='bgp_neighbors.csv', delimiter=',') }}" delegate_to: localhost + +# Contents of debug.csv +# test1 ret1.1 ret2.1 +# test2 ret1.2 ret2.2 +# test3 ret1.3 ret2.3 + +- name: "Lookup multiple keynames in the first column (index 0), returning the values from the second column (index 1)" + debug: + msg: "{{ lookup('csvfile', 'test1', 'test2', file='debug.csv', delimiter=' ') }}" + +- name: Lookup multiple keynames using old style syntax + debug: + msg: "{{ lookup('csvfile', term1, term2) }}" + vars: + term1: "test1 file=debug.csv delimiter=' '" + term2: "test2 file=debug.csv delimiter=' '" """ RETURN = """ @@ -150,6 +167,9 @@ class LookupModule(LookupBase): # populate options paramvals = self.get_options() + if not terms: + raise AnsibleError('Search key is required but was not found') + for term in terms: kv = parse_kv(term) diff --git a/test/integration/targets/lookup_csvfile/tasks/main.yml b/test/integration/targets/lookup_csvfile/tasks/main.yml index 370dc0510b8..bc330e73771 100644 --- a/test/integration/targets/lookup_csvfile/tasks/main.yml +++ b/test/integration/targets/lookup_csvfile/tasks/main.yml @@ -4,6 +4,12 @@ ignore_errors: yes register: no_keyword +- name: using modern syntax but missing keyword + set_fact: + this_will_error: "{{ lookup('csvfile', file='people.csv', delimiter=' ', col=1) }}" + ignore_errors: yes + register: modern_no_keyword + - name: extra arg in k=v syntax (deprecated) set_fact: this_will_error: "{{ lookup('csvfile', 'foo file=people.csv delimiter=, col=1 thisarg=doesnotexist') }}" @@ -27,6 +33,9 @@ - no_keyword is failed - > "Search key is required but was not found" in no_keyword.msg + - modern_no_keyword is failed + - > + "Search key is required but was not found" in modern_no_keyword.msg - invalid_arg is failed - invalid_arg2 is failed - >