Failed to import the required Python library (botocore or boto3)

Viewed 11779

I'm running a playbook on localhost, and I've already installed ansible and the required packaged like boto3. The playbook works fine when it is performing tasks on a remote host, but outputs the following error when running locally.

Command:

ansible-playbook app.yaml

Error:

fatal: [localhost]: FAILED! => {"changed": false, "msg": "Failed to import the required Python library (botocore or boto3) on DESKTOP-9NTDHK1's Python /usr/bin/python3. 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"}

Ansible version:

ansible 2.10.3
  config file = /home/user/ansible/ansible.cfg
  configured module search path = ['/home/user/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/user/ansible/venv/lib/python3.8/site-packages/ansible
  executable location = /home/user/ansible/venv/bin/ansible
  python version = 3.8.5 (default, Jul 28 2020, 12:59:40) [GCC 9.3.0]

Boto3 version:

(venv) user@DESKTOP-9NTDHK1:~/ansible$ pip show boto3ersion"
Name: boto3
Version: 1.16.18
Summary: The AWS SDK for Python
Home-page: https://github.com/boto/boto3
Author: Amazon Web Services
Author-email: UNKNOWN
License: Apache License 2.0
Location: /home/user/ansible/venv/lib/python3.8/site-packages
Requires: s3transfer, botocore, jmespath
Required-by:

app.yaml

---
- hosts: localhost
  connection: local
  gather_facts: no
  vars:
    user: *****
    AWS_PREFIX: *****
    
  tasks:
    
    # provision AWS
    
    - name: Provision VPC
      ec2_vpc_net:
        cidr_block: *****
        region: *****
        name: *****
        state: present
      register: vpc_data
1 Answers

Here is an extract of a playbook: note the vars section. ansible is not aware of the venv in which it is launched.

- hosts: all
  connection: local
  become_user: tim
  vars: #local connection defaults to using the system python
    ansible_python_interpreter: /home/tim/pycharm_projects/django_api_sync/ansible/venv/bin/python3
  vars_files:
    - includes/secret_variables.yml

  tasks:
    - name: create a DigitalOcean Droplet
      community.digitalocean.digital_ocean_droplet:
        state: present
        name: "{{droplet_name}}"
        oauth_token: "{{digital_ocean_token}}"
        size: "s-2vcpu-2gb"
        region: SGP1
        monitoring: yes
        unique_name: yes
        image: ubuntu-20-04-x64
        wait_timeout: 500
        ssh_keys: [ "{{digital_ocean_ssh_fingerprint}}"]
      register: my_droplet

    - name: Print IP address
      ansible.builtin.debug:
        msg: Droplet IP address is {{ my_droplet.data.ip_address }}
Related