Ansible - Install Python3-Apt Package

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.

1

/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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

David Miller

David Miller

Executive Financial & Market Analyst

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.

Share this article
Twitter Facebook Pinterest