DNS lookup not working

I have a task that uses the dig lookup to resolve a DNS record, but it fails with the error, “An unhandled exception occurred while running the lookup plugin ‘dig’. Error was a <class ‘ansible.errors.AnsibleError’>, original message: The dig lookup requires the python ‘dnspython’ library and it is not installed”.

However, the dnspython library is definitely installed on the host. Here’s the task definition:

  • blockinfile:
    dest: ~/.ssh/config
    marker: “# {mark} ANSIBLE MANAGED BLOCK - SVN MIRROR”
    block: |
    Host XXXXXXXXXX
    HostName {{ lookup(‘dig’, ‘YYYYYYYYYYYY’) }}
    User XXXXXXX
    Port 22
    IdentityFile ~/.ssh/svn_key
    IdentitiesOnly yes
    RSAAuthentication yes
    StrictHostKeyChecking no
    create: yes

Here’s the output of pip freeze on the host where the task fails:

Cheetah==2.4.4
Landscape-Client==14.12
PAM==0.4.2
PyYAML==3.13
Twisted-Core==13.2.0
Twisted-Names==13.2.0
Twisted-Web==13.2.0
apt-xapian-index==0.45
argparse==1.2.1
awscli==1.16.1
backports.ssl-match-hostname==3.5.0.1
boto==2.49.0
boto3==1.8.1
botocore==1.11.1
certifi==2018.8.13
chardet==3.0.4
cloud-init==0.7.5
colorama==0.3.9
configobj==4.7.2
dnspython==1.15.0
docker-py==1.9.0
docutils==0.14
futures==3.2.0
html5lib==0.999
httplib2==0.8
idna==2.7
iotop==0.6
ipaddress==1.0.22
jmespath==0.9.3
jsonpatch==1.3
jsonpointer==1.0
oauth==1.0.1
prettytable==0.7.2
pyOpenSSL==0.13
pyasn1==0.4.4
pycurl==7.19.3
pyserial==2.6
python-apt==0.9.3.5ubuntu3
python-dateutil==2.7.3
python-debian==0.1.21-nmu2ubuntu2
requests==2.19.1
rsa==3.4.2
s3transfer==0.1.13
six==1.11.0
ssh-import-id==3.21
urllib3==1.23
virtualenv==16.0.0
websocket-client==0.49.0
wheel==0.24.0
wsgiref==0.1.2
zope.interface==4.0.5

Any idea why it fails?

Thanks,
Guy

lookup plugins run on localhost aka Ansible controller not the remote host.

agreed. So you need to add connection: local or delegate_to

That wont work.
The block in file is needed on the remote host, and that is fine.
Since lookup plugins run on localhost, dnspython is needed on Ansible controller not the remote host.

If DNS lookup has to be done on the remote host a command/shell module with dig or some other DNS utilities is required.

Ahh, right I guess didn’t properly read that very first paragraph on the lookups documentation page! Ok, thanks for info everyone.

Cheers,
Guy