Sam-T
Sam-T

Reputation: 1985

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

Upvotes: 11

Views: 29010

Answers (4)

Sherif O.
Sherif O.

Reputation: 514

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

Ansible does not like to see python but python3

Upvotes: 0

Saravit Soeng
Saravit Soeng

Reputation: 561

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

Upvotes: 21

dtoux
dtoux

Reputation: 1884

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.

Upvotes: 2

Alexei Z
Alexei Z

Reputation: 129

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

Upvotes: 0

Related Questions