Introduction

Today we’re going to talk about Ansible troubleshooting, specifically about the “Failed to import the required Python library (botocore or boto3)” message and enable Ansible For AWS.

This fatal error message happens when we are trying to execute some code against your AWS EC2 Infrastructure without the necessary Python libraries for the AWS.

These circumstances are usually related to the configuration of your Ansible Controller node and usually are not related to Ansible Playbook.

I’m Luca Berton and welcome to today’s episode of Ansible Pilot.

## Playbook

The best way of talking about Ansible troubleshooting is to jump in a live Playbook to show you practically the “Failed to import the required Python library (botocore or boto3)” and how to solve it!

In this Playbook, I’m going to reproduce the error and fix using the PIP, the Python Package Manager on a demo machine.

error execution

``bash

$ ansible-playbook ami_search.yml

[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit

localhost does not match 'all'

PLAY [AMI search] *

TASK [search for AMI] **

fatal: [localhost]: FAILED! => {"changed": false, "msg": "Failed to import the required Python library (botocore or boto3) on demo.example.com's Python /usr/bin/python3.8. Please read the module documentation and install it in the appropriate location. If the required library is installed, but Ansible is using the wrong Python interpreter, please consult the documentation on ansible_python_interpreter"}

PLAY RECAP **

localhost : ok=0 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0

`

fix code

  • python version

The first step is to determine your Python version (3.8 in this example):

``bash

$ ansible --version

ansible [core 2.12.2]

config file = /etc/ansible/ansible.cfg

configured module search path = ['/home/devops/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']

ansible python module location = /usr/lib/python3.8/site-packages/ansible

ansible collection location = /home/devops/.ansible/collections:/usr/share/ansible/collections

executable location = /usr/bin/ansible

python version = 3.8.12 (default, Sep 16 2021, 10:46:05) [GCC 8.5.0 20210514 (Red Hat 8.5.0-3)]

jinja version = 2.10.3

libyaml = True

[devops@demo aws]$ python --version

-bash: python: command not found

[devops@demo aws]$ python3 --version

Python 3.6.8

[devops@demo aws]$ python3.8 --version

Python 3.8.12

[devops@demo aws]$ whereis python3.8

python3: /usr/bin/python3.6 /usr/bin/python3.6m /usr/bin/python3 /usr/bin/python3.8 /usr/lib/python3.6 /usr/lib/python3.8 /usr/lib64/python3.6 /usr/lib64/python3.8 /usr/local/lib/python3.8 /usr/include/pytho