csvfile lookup plugin with a space-delimited file

245 views
Skip to first unread message

Anand Buddhdev

unread,
Mar 16, 2016, 9:29:40 PM3/16/16
to Ansible Project
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

Uditha Desilva

unread,
Mar 18, 2016, 4:00:14 PM3/18/16
to Ansible Project
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.

Uditha Desilva

unread,
Mar 18, 2016, 6:01:46 PM3/18/16
to Ansible Project
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

Reply all
Reply to author
Forward
0 new messages