|
|
@ -16,6 +16,11 @@ DOCUMENTATION = r"""
|
|
|
|
col:
|
|
|
|
col:
|
|
|
|
description: column to return (0 indexed).
|
|
|
|
description: column to return (0 indexed).
|
|
|
|
default: "1"
|
|
|
|
default: "1"
|
|
|
|
|
|
|
|
keycol:
|
|
|
|
|
|
|
|
description: column to search in (0 indexed).
|
|
|
|
|
|
|
|
default: 0
|
|
|
|
|
|
|
|
type: int
|
|
|
|
|
|
|
|
version_added: "2.17"
|
|
|
|
default:
|
|
|
|
default:
|
|
|
|
description: what to return if the value is not found in the file.
|
|
|
|
description: what to return if the value is not found in the file.
|
|
|
|
delimiter:
|
|
|
|
delimiter:
|
|
|
@ -122,14 +127,14 @@ class CSVReader:
|
|
|
|
|
|
|
|
|
|
|
|
class LookupModule(LookupBase):
|
|
|
|
class LookupModule(LookupBase):
|
|
|
|
|
|
|
|
|
|
|
|
def read_csv(self, filename, key, delimiter, encoding='utf-8', dflt=None, col=1):
|
|
|
|
def read_csv(self, filename, key, delimiter, encoding='utf-8', dflt=None, col=1, keycol=0):
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
f = open(to_bytes(filename), 'rb')
|
|
|
|
f = open(to_bytes(filename), 'rb')
|
|
|
|
creader = CSVReader(f, delimiter=to_native(delimiter), encoding=encoding)
|
|
|
|
creader = CSVReader(f, delimiter=to_native(delimiter), encoding=encoding)
|
|
|
|
|
|
|
|
|
|
|
|
for row in creader:
|
|
|
|
for row in creader:
|
|
|
|
if len(row) and row[0] == key:
|
|
|
|
if len(row) and row[keycol] == key:
|
|
|
|
return row[int(col)]
|
|
|
|
return row[int(col)]
|
|
|
|
except Exception as e:
|
|
|
|
except Exception as e:
|
|
|
|
raise AnsibleError("csvfile: %s" % to_native(e))
|
|
|
|
raise AnsibleError("csvfile: %s" % to_native(e))
|
|
|
@ -172,7 +177,7 @@ class LookupModule(LookupBase):
|
|
|
|
paramvals['delimiter'] = "\t"
|
|
|
|
paramvals['delimiter'] = "\t"
|
|
|
|
|
|
|
|
|
|
|
|
lookupfile = self.find_file_in_search_path(variables, 'files', paramvals['file'])
|
|
|
|
lookupfile = self.find_file_in_search_path(variables, 'files', paramvals['file'])
|
|
|
|
var = self.read_csv(lookupfile, key, paramvals['delimiter'], paramvals['encoding'], paramvals['default'], paramvals['col'])
|
|
|
|
var = self.read_csv(lookupfile, key, paramvals['delimiter'], paramvals['encoding'], paramvals['default'], paramvals['col'], paramvals['keycol'])
|
|
|
|
if var is not None:
|
|
|
|
if var is not None:
|
|
|
|
if isinstance(var, MutableSequence):
|
|
|
|
if isinstance(var, MutableSequence):
|
|
|
|
for v in var:
|
|
|
|
for v in var:
|
|
|
|