patternMinor
Using `dig` Ansible module?
Viewed 0 times
moduleansibleusingdig
Problem
I am trying to look up the IPv4 address (a single one, which I will
An MWE looks like this:
... and fails as follows (relevant excerpt):
So I thought to myself I'd install the
```
---
# vim: set autoindent smartindent tabstop=2 shiftwidth=2 softtabstop=2 expandtab filetype=ansible:
hosts: localhost
connection: local
tasks:
- name: Install dnspython package on Ansible host
block:
- name: Installing python-dnspython package
apt:
pkg:
- python-dnspython
update_cache: yes
install_recommends: no
state: present
when: ansible_python.version.major == 2
- name: Installing python3-dnspython package
apt:
pkg:
- python3-dnspython
update_cache: yes
install_recommends: no
state: present
when: ansible_python.version.major == 3
become: true
hosts: localhost
connection: local
vars:
host2lookup: "example.com"
tasks:
- getent:
database: ahostsv4
key: "{{host2lookup}}"
- debug:
assert on!) for a given name using Ansible and use that as a fact "down the line".An MWE looks like this:
---
# vim: set autoindent smartindent tabstop=2 shiftwidth=2 softtabstop=2 expandtab filetype=ansible:
- name: Preparations
hosts: localhost
connection: local
vars:
host2lookup: "example.com"
tasks:
- getent:
database: ahostsv4
key: "{{host2lookup}}"
- debug:
var: getent_ahostsv4
- name: Lookup for {{host2lookup}}
debug:
msg: "{{lookup('dig', '{{host2lookup}}./A')}}"... and fails as follows (relevant excerpt):
localhost failed | msg: An unhandled exception occurred while running the lookup plugin 'dig'. Error was a , original message: The dig lookup requires the python 'dnspython' library and it is not installedSo I thought to myself I'd install the
dnspython library for the appropriate Python version, making the playbook look as follows:```
---
# vim: set autoindent smartindent tabstop=2 shiftwidth=2 softtabstop=2 expandtab filetype=ansible:
- name: Preparing Ansible host
hosts: localhost
connection: local
tasks:
- name: Install dnspython package on Ansible host
block:
- name: Installing python-dnspython package
apt:
pkg:
- python-dnspython
update_cache: yes
install_recommends: no
state: present
when: ansible_python.version.major == 2
- name: Installing python3-dnspython package
apt:
pkg:
- python3-dnspython
update_cache: yes
install_recommends: no
state: present
when: ansible_python.version.major == 3
become: true
- name: Preparations
hosts: localhost
connection: local
vars:
host2lookup: "example.com"
tasks:
- getent:
database: ahostsv4
key: "{{host2lookup}}"
- debug:
Solution
See:
-
Ansible is using the wrong version of Python
-
How can I specify the version of Python to use in an Ansible playbook?
-
How to choose Python interpreter for Ansible playbook?
See also Setting the Python interpreter that explains various configurations' options, variables, and debugging.
Q: "None of that explains why - and that is/was my point - ansible_python wrongly claims to be running on Python 3.x on the local node. Because all I do by setting interpreter_python in ansible.cfg is to give Ansible free rein on how it interprets things."
Short answer: You can't configure the version of Python the Ansible utils use ($ ansible --version). INTERPRETER_PYTHON configures the Python version used by the Ansible modules on the remote hosts.
A: Configuration option INTERPRETER_PYTHON works as expected. There is nothing wrong when Python3 is discovered when asked by
See implementation details of the function discover_interpreter. When the version of the distro is not in _INTERPRETER_PYTHON_DISTRO_MAP
"return nearest previous version we're newer than"
"The default value of auto_legacy provides all the same behavior, but for backward compatibility with older Ansible releases that always defaulted to /usr/bin/python, will use that interpreter if present (and issue a warning that the default behavior will change to that of auto in a future Ansible release."
See the notes and examples below.
Q: "What does the ansible command line tool have to do with the behavior I see with ansible-playbook?"
A: Both ansible command line tool and ansible-playbook will report the same value of ansible_python_version. For example,
Notes
Path to the Python interpreter to be used for module execution on remote targets
See How to choose a python interpreter for Ansible playbook?
The below requirements are needed on the local master node that executes this lookup: dnspython
-
See Ansible packages in Ubuntu what version of Python was used.
-
ansible_python_interpreter works as expected
On master(Ubuntu) the playbook
gives
ansible python module location = /usr/lib/python2.7/dist-packages/ansible
python version = 2.7.16 (default, Oct 7 2019, 17:36:04) [GCC 8.3.0]
...
SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o Pas
-
Ansible is using the wrong version of Python
-
How can I specify the version of Python to use in an Ansible playbook?
-
How to choose Python interpreter for Ansible playbook?
See also Setting the Python interpreter that explains various configurations' options, variables, and debugging.
Q: "None of that explains why - and that is/was my point - ansible_python wrongly claims to be running on Python 3.x on the local node. Because all I do by setting interpreter_python in ansible.cfg is to give Ansible free rein on how it interprets things."
Short answer: You can't configure the version of Python the Ansible utils use ($ ansible --version). INTERPRETER_PYTHON configures the Python version used by the Ansible modules on the remote hosts.
A: Configuration option INTERPRETER_PYTHON works as expected. There is nothing wrong when Python3 is discovered when asked by
interpreter_python=auto.- auto (default)
See implementation details of the function discover_interpreter. When the version of the distro is not in _INTERPRETER_PYTHON_DISTRO_MAP
"return nearest previous version we're newer than"
def _version_fuzzy_match(version, version_map):
...
# slot match; return nearest previous version we're newer than
kpos = bisect.bisect(sorted_looseversions, find_looseversion)
interpreter_python=auto
shell> ansible localhost -m setup -a 'filter=ansible_python_version'
localhost | SUCCESS => {
"ansible_facts": {
"ansible_python_version": "3.7.3",
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false
}
- auto_legacy (default is /usr/bin/python)
"The default value of auto_legacy provides all the same behavior, but for backward compatibility with older Ansible releases that always defaulted to /usr/bin/python, will use that interpreter if present (and issue a warning that the default behavior will change to that of auto in a future Ansible release."
interpreter_python=auto_legacy
shell> ansible localhost -m setup -a 'filter=ansible_python_version'
[DEPRECATION WARNING]: Distribution Ubuntu 19.04 on host localhost should use /usr/bin/python3, but is using /usr/bin/python for backward compatibility with prior Ansible releases. A future
Ansible release will default to using the discovered platform python for this host. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more
information. This feature will be removed in version 2.12. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
localhost | SUCCESS => {
"ansible_facts": {
"ansible_python_version": "2.7.16",
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false
}
See the notes and examples below.
Q: "What does the ansible command line tool have to do with the behavior I see with ansible-playbook?"
A: Both ansible command line tool and ansible-playbook will report the same value of ansible_python_version. For example,
shell> ansible localhost -m setup | grep ansible_python_version
"ansible_python_version": "2.7.16",
shell> cat playbook.yml
- hosts: localhost
tasks:
- debug:
var: ansible_python_version
shell> ansible-playbook playbook.yml
ok: [localhost] => {
"ansible_python_version": "2.7.16"
}
Notes
- INTERPRETER_PYTHON configuration parameter says:
Path to the Python interpreter to be used for module execution on remote targets
See How to choose a python interpreter for Ansible playbook?
- Ansible will tell what Python is used on master
shell> ansible --version
ansible 2.9.2
config file = /home/vlado/.ansible.cfg
configured module search path = [u'/home/vlado/.ansible/my_modules']
ansible python module location = /usr/lib/python2.7/dist-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.16 (default, Oct 7 2019, 17:36:04) [GCC 8.3.0]
- dig says
The below requirements are needed on the local master node that executes this lookup: dnspython
- It's possible to install both versions
shell> dpkg -l | grep dnspython
ii python-dnspython 1.16.0-1 all DNS toolkit for Python
ii python3-dnspython 1.16.0-1 all DNS toolkit for Python 3
-
See Ansible packages in Ubuntu what version of Python was used.
-
ansible_python_interpreter works as expected
On master(Ubuntu) the playbook
shell> cat playbook.yml
- hosts: test_01
tasks:
- getent:
database: hosts
shell> cat hosts
test_01 ansible_python_interpreter=/usr/local/bin/python3.6
shell> ansible-playbook playbook.yml -i hosts -vvv | grep python
gives
ansible python module location = /usr/lib/python2.7/dist-packages/ansible
python version = 2.7.16 (default, Oct 7 2019, 17:36:04) [GCC 8.3.0]
...
SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o Pas
Context
StackExchange DevOps Q#10142, answer score: 2
Revisions (0)
No revisions yet.