Could not import python modules: apt, apt_pkg Ansible playbook error

Solved3.48K viewsansible apache2 python3

Could not import python modules: apt, apt_pkg Ansible playbook error

I am trying to install apache2 on my slave machine using an ansible playbook. But the below error is thrown in the installation task.

TASK [Install Apache 2] ********************************************************
[WARNING]: Updating cache and auto-installing missing dependency: python3-apt
fatal: [testserver]: FAILED! => {"changed": false, "msg": "Could not import python modules: apt, apt_pkg. Please install python3-apt package."}

python3 is already installed in the target machine. In the controller machine, ansible 2.9.17 is installed. How to resolve this ?

Share:
vito Selected answer as best September 21, 2021
2

Ansible is not pointing to the python3 module in your target machine. You will have to provide the ansible python interpreter path in the playbook using ansible vars module.

- hosts: testserver
  become: true
  vars:
    ansible_python_interpreter: "/usr/bin/python3"

Share:
vito Selected answer as best September 21, 2021
1