csvfile lookup plugin with a space-delimited file

Hello Ansible folk,

I have a text file of the form:

10.60.43.23 worker22.domain

I was to use the “csvfile” lookup plugin to look up name host name, given an IP address. So I’m trying this:

  • name: hostname lookup
    debug:
    msg: hostname is {{ lookup(“csvfile”,ansible_default_ipv4[“address”]+" file=hostnames.hdp1 delimiter=’ '") }}

However, running this snippet gives me the error:

TASK [hostname lookup] *********************************************************

fatal: [worker22.domain]: FAILED! => {“failed”: true, “msg”: “need more than 1 value to unpack”}

I can’t understand what Ansible is actually failing on. I’ve tried to run with -vvvv, but it seems this error occurs before the debug module is even transferred to the remote host for execution. If anyone knows what this error means, and how I can avoid it, I would greatly appreciate the help.

Regards,

Anand

Check that your file is sorted, and doesn’t have any blank lines. Similar issues led me to abandon that and use an awk invocation via “with_lines:” to pull the data out that I needed.

Here’s what I suggest for your case:

  • name: hostname lookup
    debug:
    msg: “hostname is {{ item }}”
    with_lines: “awk ‘$1=="{{ansible_default_ipv4[‘address’]}}"{print $2;exit}’ hostnames.hdp1”
    changed_when: false