dig lookup exception: 'list' object has no attribute 'startswith'

23 views
Skip to first unread message

rjwagn...@gmail.com

unread,
Dec 10, 2018, 12:39:03 PM12/10/18
to Ansible Project
Hey all-

I'm having more problems w/ the dig lookup.  If I statically define a list of hosts to lookup as a variable, it works.  But if I return the list of hosts from another script, it fails.  Here's a simple example:

$ cat test.py
#!/usr/bin/env python

import json

hostList
= []
hostList
.append(('www.google.com'))
j
= {}
j
['hosts'] = hostList
print json.dumps(j)

$ cat test
.yml
---
- hosts: all
  vars
:
    ldap_names
:
     
- { cn: 'bogus', default: yes }

  tasks
:
 
- name: lookup ldap_names in directory
    shell
: ". /etc/env_ansible ; /home/rowagn/data-platform/oracle/ansible/client12c/test.py -j {{ item.cn }}"
    with_items
: "{{ ldap_names }}"
   
register: oidLookup_output
    delegate_to
: localhost
    become
: no

 
- name: debug
    debug
: msg="result is {{ (item.stdout | from_json).hosts }}"
    with_items
: "{{ oidLookup_output.results }}"

 
- name: lookup
    debug
: msg="IP is {{ lookup('dig', (item.stdout | from_json).hosts, wantlist=True) }}"
    with_items
: "{{ oidLookup_output.results }}"

$ ansible
-playbook  -clocal test.yml

PLAY
[all] *********************************************************************

TASK
[setup] *******************************************************************
ok
: [10.239.211.85]

TASK
[lookup ldap_names in directory] ******************************************
changed
: [10.239.211.85 -> localhost] => (item={u'default': True, u'cn': u'bogus'})

TASK
[debug] *******************************************************************
ok
: [10.239.211.85] => (item={'_ansible_parsed': True, '_ansible_item_result': True, u'end': u'2018-12-10 12:34:26.177251', '_ansible_no_log': False, u'stdout': u'{"hosts": ["www.google.com"]}', u'cmd': u'. /etc/env_ansible ; /home/rowagn/data-platform/oracle/ansible/client12c/test.py -j bogus', u'changed': True, 'invocation': {'module_name': u'command', u'module_args': {u'creates': None, u'executable': None, u'_uses_shell': True, u'_raw_params': u'. /etc/env_ansible ; /home/rowagn/data-platform/oracle/ansible/client12c/test.py -j bogus', u'removes': None, u'warn': True, u'chdir': None}}, 'item': {u'default': True, u'cn': u'bogus'}, u'delta': u'0:00:00.013173', u'stderr': u'', u'rc': 0, '_ansible_delegated_vars': {'ansible_host': u'localhost'}, 'stdout_lines': [u'{"hosts": ["www.google.com"]}'], u'start': u'2018-12-10 12:34:26.164078', u'warnings': []}) => {
   
"item": {
       
"changed": true,
       
"cmd": ". /etc/env_ansible ; /home/rowagn/data-platform/oracle/ansible/client12c/test.py -j bogus",
       
"delta": "0:00:00.013173",
       
"end": "2018-12-10 12:34:26.177251",
       
"invocation": {
           
"module_args": {
               
"_raw_params": ". /etc/env_ansible ; /home/rowagn/data-platform/oracle/ansible/client12c/test.py -j bogus",
               
"_uses_shell": true,
               
"chdir": null,
               
"creates": null,
               
"executable": null,
               
"removes": null,
               
"warn": true
           
},
           
"module_name": "command"
       
},
       
"item": {
           
"cn": "bogus",
           
"default": true
       
},
       
"rc": 0,
       
"start": "2018-12-10 12:34:26.164078",
       
"stderr": "",
       
"stdout": "{\"hosts\": [\"www.google.com\"]}",
       
"stdout_lines": [
           
"{\"hosts\": [\"www.google.com\"]}"
       
],
       
"warnings": []
   
},
   
"msg": "result is [u'www.google.com']"
}

TASK
[lookup] ******************************************************************
fatal
: [10.239.211.85]: FAILED! => {"failed": true, "msg": "An unhandled exception occurred while running the lookup plugin 'dig'. Error was a <type 'exceptions.AttributeError'>, original message: 'list' object has no attribute 'startswith'"}

PLAY RECAP
*********************************************************************
10.239.211.85              : ok=3    changed=1    unreachable=0    failed=1  


I'm sure I'm overlooking something simple, but I've been staring at this for two hours.  Can anyone spot my error?

Rob

Kai Stian Olstad

unread,
Dec 10, 2018, 1:35:08 PM12/10/18
to ansible...@googlegroups.com
On Monday, 10 December 2018 18:39:02 CET rjwagn...@gmail.com wrote:
> - name: debug
> debug: msg="result is {{ (item.stdout | from_json).hosts }}"
> with_items: "{{ oidLookup_output.results }}"

Your hosts is a list with one element www.google.com

>
> - name: lookup
> debug: msg="IP is {{ lookup('dig', (item.stdout | from_json).hosts,
> wantlist=True) }}"
> with_items: "{{ oidLookup_output.results }}"

So here you are feeding dig a list, it doesn't take a list, only a string.

> TASK [debug]
> *******************************************************************
> ok: [10.239.211.85] => (item={'_ansible_parsed': True,
> '_ansible_item_result': True, u'end': u'2018-12-10 12:34:26.177251',

</snip>

> "stdout": "{\"hosts\": [\"www.google.com\"]}",
> "stdout_lines": [
> "{\"hosts\": [\"www.google.com\"]}"
> ],
> "warnings": []
> },
> "msg": "result is [u'www.google.com']"

Here you clearly see that it's a list and not a string.


--
Kai Stian Olstad


Rob Wagner

unread,
Dec 10, 2018, 1:53:38 PM12/10/18
to ansible...@googlegroups.com
Thanks Kai. That's it.  I thought I had read somewhere that dig will accept a list, but clearly not.

--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/j-Adyf2eeWw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/2980419.Iyv8cOm4ia%40x1.
For more options, visit https://groups.google.com/d/optout.

Kai Stian Olstad

unread,
Dec 10, 2018, 1:59:23 PM12/10/18
to ansible...@googlegroups.com
On Monday, 10 December 2018 19:53:15 CET Rob Wagner wrote:
> Thanks Kai. That's it. I thought I had read somewhere that dig will accept
> a list, but clearly not.

Doesn't accept a list, but do return a list if wantlist=True.

--
Kai Stian Olstad


Reply all
Reply to author
Forward
0 new messages