Using Ubuntu 18.04, Ansible 2.9, Python 3.6.9, have installed python3-apt
On a basic ansible command ansible -b all -m apt -a "name=apache2 state=latest"
Get Error:
FAILED! => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"msg": "Could not import python modules: apt, apt_pkg. Please install python3-apt package."
}
$ sudo apt-get install python3-apt
$ ansible --version
ansible 2.9.12
python version = 3.6.9
$ python --version
Python 3.7.6
4 Answers 4
Go to python3 dist-package directory
cd /usr/lib/python3/dist-packages
And then link these two files
sudo ln -s apt_inst.cpython-35m-x86_64-linux-gnu.so apt_inst.so
sudo ln -s apt_pkg.cpython-35m-x86_64-linux-gnu.so apt_pkg.so
5 Comments
python3.9 -m sitesudo apt install software-properties-common gpg python3 -y That created the missing files that Saravit posted above. Ansible started working once those two files were created.apt_inst.cpython-38-x86_64-linux-gnu.so, et al, so I just set up the links to those.I had the same problem when I had two versions of python3 installed - python3.7 and python3.9. After uninstalling the older version the problem went away.
1 Comment
apt remove python3.5 and apt purge python3.5/usr/bin/python on the remote node is probably python 2.x version rather than python3. As pointed by @Chuck above, changing the interpreter to python3 should do the trick.
Comments
on my managed host, i made some debugging, i had python2 and python3.9 installed but in /usr/bin/ i had a symbolic link
python -> python2
after removing this link and creating a new link
ln -s python3 python
and calling ansible from the managing device, it works but it still gives the warning, it seems that ansible does not like to see the python binary, but it wants to see python3 only
python3-apt) must be installed on the remote hosts you are targeting with your playbook, not on the local host where ansible is running.python3-apton the controlled/node machine also - but still same error.