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 6 3 Answers
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 3 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.
/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.