我从容器中运行了剧本,但失败并显示以下错误消息:
"Failed to import the required Python library (python-ldap) on rocky9.localdomain's Python /usr/bin/python3.9. 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"
容器文件:
FROM alpine:3.20
RUN apk add --update --no-cache \
ansible=9.5.1-r0 \
git \
openssh-client \
sshpass \
python3 \
py3-podman \
py3-pip \
openldap-clients \
build-base \
python3-dev \
openldap-dev \
py3-ldap && \
rm -rf /var/cache/apk/* \
RUN ansible-galaxy collection install community.general
RUN mkdir -p /etc/ansible && \
echo "localhost" > /etc/ansible/hosts
WORKDIR /srv/ansible
CMD [ "ansible-playbook", "--version" ]
ansible.cfg:
interpreter_python = auto
stdout(前几行):
ansible-playbook [core 2.17.0]
config file = /srv/ansible/ansible.cfg
configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3.12/site-packages/ansible
ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
executable location = /usr/bin/ansible-playbook
python version = 3.12.7 (main, Oct 7 2024, 11:30:19) [GCC 13.2.1 20240309] (/usr/bin/python3)
jinja version = 3.1.4
libyaml = True
容器:
/srv/ansible # ls -la /usr/bin/python*
lrwxrwxrwx 1 root root 7 Oct 18 09:20 /usr/bin/python -> python3
lrwxrwxrwx 1 root root 14 Oct 18 09:21 /usr/bin/python-config -> python3-config
lrwxrwxrwx 1 root root 10 Oct 18 09:20 /usr/bin/python3 -> python3.12
lrwxrwxrwx 1 root root 17 Oct 18 09:21 /usr/bin/python3-config -> python3.12-config
-rwxr-xr-x 1 root root 14080 Oct 7 11:30 /usr/bin/python3.12
-rwxr-xr-x 1 root root 3014 Oct 7 11:30 /usr/bin/python3.12-config
测试虚拟机:
[vagrant@rocky9 ~]$ ls /usr/bin/python*
/usr/bin/python /usr/bin/python3 /usr/bin/python3.9
然后我有一个角色任务:
- name: Modify LDAP attributes
community.general.ldap_attrs:
dn: "xxx"
attributes:
servicePrincipalName:
- "host/xxx"
- "RestrictedKrbHost/xxx"
state: present
server_uri: "ldap://xx"
bind_dn: "cn=xxx,OU=aaa,DC=bbb,DC=ccc"
bind_pw: "xxx"
但是任务失败并出现以下错误消息:
"msg": "Failed to import the required Python library (python-ldap) on rocky9.localdomain's Python /usr/bin/python3.9. 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.builtin模块可以正常工作。
所以我认为在构建容器并尝试导入我的第一个集合时我做错了什么。也许 delegate_to localhost 有一些魔力?
我通过添加修复了它
- name: Modify LDAP attributes
community.general.ldap_attrs:
dn: "xxx"
attributes:
servicePrincipalName:
- "host/xxx"
- "RestrictedKrbHost/xxx"
state: present
server_uri: "ldap://xx"
bind_dn: "cn=xxx,OU=aaa,DC=bbb,DC=ccc"
bind_pw: "xxx"
delegate_to: localhost # <<<<<<<<<<<<<<<<
connection: local # <<<<<<<<<<<<<<<<