12

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
asked Sep 1, 2020 at 2:38
6
  • 2
    Dependencies (like python3-apt) must be installed on the remote hosts you are targeting with your playbook, not on the local host where ansible is running. Commented Sep 1, 2020 at 3:06
  • @larsks - I did not know about this dependency on target machine, tried it - Did not work. Commented Sep 1, 2020 at 3:11
  • Does this answer your question? Ansible requires python-apt but it's already installed Commented Sep 1, 2020 at 6:56
  • @toydarian - not really. I installed python3-apt on the controlled/node machine also - but still same error. Commented Sep 1, 2020 at 12:32
  • I resolved this by adding : vars: ansible_python_interpreter: "/usr/bin/python3" This then looks for the python3-apt library in the correct python path Commented Sep 7, 2020 at 14:57

4 Answers 4

23

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
answered Sep 8, 2021 at 16:54
Sign up to request clarification or add additional context in comments.

5 Comments

you safe few hours of my life...appreciate
if you custom install python3.9 where would the dist-packages folder be ?
It would be located in the custom folder that you have prefined before your installation. Just in case use this command to find yours: python3.9 -m site
My target machine did not have the above two .so files. I solved this problem today using this command on the target machine (Debian 10): sudo 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.
Good tip! I have python 3.9.5 in order to install ansible 2.14.0 and had apt_inst.cpython-38-x86_64-linux-gnu.so, et al, so I just set up the links to those.
2

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.

answered Sep 4, 2021 at 14:07

1 Comment

how did you uninstalled older version ? Even after uninstalling python3.5 I am still able to use python3.5 but retrying again it says python3.5 has been removed. I used apt remove python3.5 and apt purge python3.5
0

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

robe007
4,0074 gold badges41 silver badges62 bronze badges
answered Sep 16, 2020 at 3:00

Comments

0

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

answered Feb 6, 2023 at 1:57

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.