summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHerman van Rink <rink@initfour.nl>2024-12-06 17:15:17 +0100
committerGitHub <noreply@github.com>2024-12-06 17:15:17 +0100
commit363c57b311c93f9a7e4f457c37e121244d01c1e5 (patch)
treed4632d6556b24b813f5445f249991911ff440f30
parentlookup_template: added trim_blocks option (#84254) (diff)
downloadansible-363c57b311c93f9a7e4f457c37e121244d01c1e5.tar.xz
ansible-363c57b311c93f9a7e4f457c37e121244d01c1e5.zip
csvfile - let the config system do the typecasting (#82263)
Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
-rw-r--r--changelogs/fragments/csvfile-col.yml3
-rw-r--r--lib/ansible/plugins/lookup/csvfile.py5
-rw-r--r--test/integration/targets/lookup_csvfile/tasks/main.yml14
3 files changed, 19 insertions, 3 deletions
diff --git a/changelogs/fragments/csvfile-col.yml b/changelogs/fragments/csvfile-col.yml
new file mode 100644
index 0000000000..9e371010c0
--- /dev/null
+++ b/changelogs/fragments/csvfile-col.yml
@@ -0,0 +1,3 @@
+---
+minor_changes:
+ - csvfile - let the config system do the typecasting (https://github.com/ansible/ansible/pull/82263).
diff --git a/lib/ansible/plugins/lookup/csvfile.py b/lib/ansible/plugins/lookup/csvfile.py
index 9dd98938ef..57f7c51d4f 100644
--- a/lib/ansible/plugins/lookup/csvfile.py
+++ b/lib/ansible/plugins/lookup/csvfile.py
@@ -16,7 +16,8 @@ DOCUMENTATION = r"""
options:
col:
description: column to return (0 indexed).
- default: "1"
+ default: 1
+ type: int
keycol:
description: column to search in (0 indexed).
default: 0
@@ -164,7 +165,7 @@ class LookupModule(LookupBase):
for row in creader:
if len(row) and row[keycol] == key:
- return row[int(col)]
+ return row[col]
except Exception as e:
raise AnsibleError("csvfile: %s" % to_native(e))
diff --git a/test/integration/targets/lookup_csvfile/tasks/main.yml b/test/integration/targets/lookup_csvfile/tasks/main.yml
index bc330e7377..0279f55981 100644
--- a/test/integration/targets/lookup_csvfile/tasks/main.yml
+++ b/test/integration/targets/lookup_csvfile/tasks/main.yml
@@ -82,7 +82,19 @@
assert:
that:
- lookup('csvfile', 'notfound file=people.csv delimiter=, col=2') == []
- - lookup('csvfile', 'notfound file=people.csv delimiter=, col=2, default=what?') == "what?"
+ - lookup('csvfile', 'notfound file=people.csv delimiter=, col=2 default=what?') == "what?"
+
+- name: Pass wrong terms value fails parse_kv
+ set_fact:
+ people_col_2: '{{ lookup("csvfile", "notfound file=people.csv delimiter=, col=2, default=what?") }}'
+ ignore_errors: yes
+ register: people_col_2_r
+
+- name: Check if wrong terms value fails parse_kv
+ assert:
+ that:
+ - people_col_2_r.failed
+ - "'Invalid type for configuration option' in people_col_2_r.msg"
# NOTE: For historical reasons, this is correct; quotes in the search field must
# be treated literally as if they appear (escaped as required) in the field in the