如何设置ansible python3模块目录?

问题描述 投票:0回答:1

使用 python3 安装 pip 包时,如何让 Ansible 读取正确的路径?

我有一个 docker 文件,用于安装 Ansible 和 hvac 软件包,需要与 hashcorpVault 进行交互。

Dockerfile:

FROM ubuntu:22.04
ENV DEBIAN_FRONTEND noninteractive

WORKDIR /ansible

RUN apt-get update -y \
    && apt-get install -y -q --no-install-recommends \
        curl \
        git \
        python3 \
        bash \
        python3-pip \
    && rm -rf /var/lib/apt/lists/*

RUN pip install --trusted-host pypi.org --trusted-host pypi.python.org --trusted-host files.pythonhosted.org \
        ansible \
        ansible-lint \
        hvac  \
    && ln -sf /usr/bin/python3.10 /usr/bin/python3 \
    && ln -sf /usr/bin/python3.10 /usr/bin/python

当我运行剧本时,错误告诉我 Ansible 无法加载包(hvac)

剧本:

---
- name: Test
  hosts: all
  gather_facts: true
  vars_files:
    - ../defaults/all.yml

- name: install hvac pip package
  delegate_to: localhost
  pip:
    name: hvac
    state: present

- name: Create directories
  become: true
  ansible.builtin.file:
    dest: "{{ item.path }}"
    state: directory
    owner: vault
    group: vault
    mode: "{{ item.mode }}"
  with_items:
    - path: "{{ vault_config_file_path }}"
      mode: "0777"
    - path: "{{ vault_token_path }}"
      mode: "0777"

- name: Generate a secret-id for the given approle
  community.hashi_vault.vault_write:
    url: https://vault.emirates.group
    namespace: "{{ namespace }}"
    path: "{{ approle_path }}"
    token: "{{ lookup('ansible.builtin.env', 'VAULT_TOKEN') }}"
  register: secret_id
  no_log: false
  delegate_to: localhost


- name: Set fact the AppRole SecretID
  ansible.builtin.set_fact:
    secret_id: "{{ secret_id.data.data.secret_id }}"
  no_log: true

错误:

"msg": "Failed to import the required Python library (hvac) on master-135-1b3z3'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"

我检查了图像中的 pip install 文件夹,它与 Ansible python 模块文件夹不匹配。

暖通空调位置:

root@b5dcd7169e39:/ansible# pip show hvac
Name: hvac
Version: 0.11.2
Summary: HashiCorp Vault API client
Home-page: https://github.com/hvac/hvac
Author: Ian Unruh <[email protected]>, Jeffrey Hogan <[email protected]>
Author-email: [email protected]
License: UNKNOWN
Location: /usr/lib/python3/dist-packages
Requires:
Required-by:
root@b5dcd7169e39:/ansible#

我可以在容器中加载 python3 中的模块而不会出现错误....所以这部分没问题

root@b5dcd7169e39:/ansible# python3
Python 3.10.12 (main, Jun 11 2023, 05:26:28) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import hvac
>>>

但是,我想知道如何更改此路径“ansible python 模块位置”?

root@b5dcd7169e39:/srv/ansible#  ansible-playbook --version -e "ansible_python_interpreter=/usr/bin/python3"
ansible-playbook [core 2.15.3]
  config file = None
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.10/dist-packages/ansible
  ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/local/bin/ansible-playbook
  python version = 3.10.12 (main, Jun 11 2023, 05:26:28) [GCC 11.4.0] (/usr/bin/python3)
  jinja version = 3.1.2
  libyaml = True
root@b5dcd7169e39:/srv/ansible#

我可以使用什么变量来纠正 Ansible 并将其放在正确的路径中?

我尝试在

ansible.cfg
文件中设置库变量,但仍然找不到用pip安装的模块。

ansible.cfg 文件:

[defaults]
inventory = inventory/inventory.ini
host_key_checking = false
interpreter_python = auto
timeout = 60
ansible_parent_role_paths = roles
library = /usr/local/lib/python3.10/dist-packages

我尝试在 Ansible 文档中查找此内容,但无济于事......以前有人必须这样做吗?

python-3.x ansible
1个回答
0
投票

“msg”:“无法在 master-135-1b3z3 的 Python /usr/bin/python3 上导入所需的 Python 库 (hvac)。” <-- here it is using /usr/bin/python3

您的 Ansible Python 很可能位于“/usr/local/bin/”中的某个位置。

想知道如果将 /usr/bin/python3 链接到“/usr/local/bin/python3”是否会成功运行?我最近不得不为我的 Ansible 运行执行此操作,我的 Ansible 版本是 2.14.7。

© www.soinside.com 2019 - 2024. All rights reserved.