无法导入所需的Python库(botocore或boto3)

nin*_*Tan 6 ansible boto3

我正在本地主机上运行一个 playbook,并且我已经安装了 ansible 和所需的包(如 boto3)。该剧本在远程主机上执行任务时工作正常,但在本地运行时输出以下错误。

命令:

ansible-playbook app.yaml
Run Code Online (Sandbox Code Playgroud)

错误:

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"}
Run Code Online (Sandbox Code Playgroud)

安斯波版本:

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]
Run Code Online (Sandbox Code Playgroud)

波托3版本:

(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:
Run Code Online (Sandbox Code Playgroud)

应用程序.yaml

ansible-playbook app.yaml
Run Code Online (Sandbox Code Playgroud)

Tim*_*son 0

这是剧本的摘录:注意 vars 部分。ansible 不知道它是在哪个 venv 中启动的。

- 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 }}
Run Code Online (Sandbox Code Playgroud)