ansible报错解决:Failed to import the required Python library (netaddr)

embedded/2024/9/22 16:48:59/

ansibleFailed_to_import_the_required_Python_library_netaddr_0">ansible报错解决:Failed to import the required Python library (netaddr)

问题情况

今天我在做实验的时候出现了一个问题,以下是我的playbook,这个playbook是验证变量ip_address是否是一个合法的IP地址,并打印相应的信息的一个剧本

[root@localhost ansible]# vim test6.yml 
- hosts: testvars:ip_address: "192.168.2455.1"tasks:- name: "判断{{ip_address}}是否为合法ip"debug:msg: "{{ ip_address | ipaddr}}"
# 执行结果
[root@localhost ansible]# ansible-playbook test6.yml PLAY [test] *************************************************************************************************************************TASK [Gathering Facts] **************************************************************************************************************
ok: [192.168.200.20]TASK [判断192.168.2455.1是否为合法ip] *********************************************************************************************
fatal: [192.168.200.20]: FAILED! => {"msg": "template error while templating string: Could not load \"ipaddr\": 'Invalid plugin FQCN (ansible.netcommon.ipaddr): unable to locate collection ansible.netcommon'. String: {{ ip_address | ipaddr}}. Could not load \"ipaddr\": 'Invalid plugin FQCN (ansible.netcommon.ipaddr): unable to locate collection ansible.netcommon'"}PLAY RECAP **************************************************************************************************************************
192.168.200.20             : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0 

这里报错没有识别ipaddr这个集合,并告诉你没有安装ansible.netcommon.ipaddr集合,这时候我们安装集合就好了对吧

# 安装集合
[root@localhost ansible]# ansible-galaxy collection install /root/ansible-netcommon-6.1.1.tar.gz 
Starting galaxy collection install process
Process install dependency map
Starting collection install process
Downloading https://galaxy.ansible.com/api/v3/plugin/ansible/content/published/collections/artifacts/ansible-utils-4.1.0.tar.gz to /root/.ansible/tmp/ansible-local-4770j3qwrbe5/tmptvw6n2g6/ansible-utils-4.1.0-_5mvcxlt
Installing 'ansible.netcommon:6.1.1' to '/root/ansible/mycollection/ansible_collections/ansible/netcommon'
ansible.netcommon:6.1.1 was installed successfully
Installing 'ansible.utils:4.1.0' to '/root/ansible/mycollection/ansible_collections/ansible/utils'
ansible.utils:4.1.0 was installed successfully
# 查看集合
[root@localhost ansible]# ansible-galaxy collection list
# 这里是我自己设置的集合路径,显示已经安装了对吧
# /root/ansible/mycollection/ansible_collections
Collection        Version
----------------- -------
ansible.netcommon 6.1.1  
ansible.utils     4.1.0  

再次执行这个playbook

[root@localhost ansible]# ansible-playbook test6.yml PLAY [test] *************************************************************************************************************************TASK [Gathering Facts] **************************************************************************************************************
ok: [192.168.200.20]TASK [Set the IP address192.168.2455.1] *********************************************************************************************
[DEPRECATION WARNING]: Use 'ansible.utils.ipaddr' module instead. This feature will be removed from ansible.netcommon in a release 
after 2024-01-01. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
fatal: [192.168.200.20]: FAILED! => {"msg": "Failed to import the required Python library (netaddr) on localhost.localdomain's Python /usr/bin/python3.11. 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"}
# 这个时候他告诉我们没有netaddr这个python库
# 这里我们安装这个python库
# 也是我自己出错的点,你ansible使用的是哪个python版本
# 你就要使用哪个版本来安装netaddr这个python库才行
PLAY RECAP **************************************************************************************************************************
192.168.200.20             : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   

解决问题

先查看ansible版本

[root@localhost ansible]# ansible --version
ansible [core 2.14.2]config file = /root/ansible/ansible.cfgconfigured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']ansible python module location = /usr/lib/python3.11/site-packages/ansibleansible collection location = /root/ansible/mycollection:/root/ansible/{{ ANSIBLE_HOME ~ "/collections:/usr/share/ansible/collections" }}executable location = /usr/bin/ansiblepython version = 3.11.2 (main, Feb 16 2023, 00:00:00) [GCC 11.3.1 20221121 (Red Hat 11.3.1-4)] (/usr/bin/python3.11)jinja version = 3.1.2libyaml = True
# 用的python3.11对吧
# 使用python3.11安装netaddr

安装netaddr这个python库

# 安装pip3.11
[root@localhost ansible]# yum install -y python3.11-pip.noarch 
# 创建python虚拟环境
python3.11 -m venv python_env
source python_env/bin/activate
(python_env) [root@localhost ansible]# pip3.11 install netaddr
Collecting netaddrUsing cached netaddr-1.2.1-py3-none-any.whl (2.3 MB)
Installing collected packages: netaddr
Successfully installed netaddr-1.2.1[notice] A new release of pip available: 22.3.1 -> 24.0
[notice] To update, run: pip install --upgrade pip

执行成功

(python_env) [root@localhost ansible]# ansible-playbook test6.yml PLAY [test] *************************************************************************************************************************TASK [Gathering Facts] **************************************************************************************************************
ok: [192.168.200.20]TASK [判断192.168.2455.1是否为合法ip] ***********************************************************************************************
[DEPRECATION WARNING]: Use 'ansible.utils.ipaddr' module instead. This feature will be removed from ansible.netcommon in a release 
after 2024-01-01. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
# 这里告诉我们要使用完整的模块名字
ok: [192.168.200.20] => {"msg": false
}PLAY RECAP **************************************************************************************************************************
192.168.200.20             : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

http://www.ppmy.cn/embedded/34188.html

相关文章

libmodbus使用

安装可以看这个博客&#xff1a; https://blog.csdn.net/hanhui22/article/details/105786762 它的安装可以&#xff0c;但是编译测试看不太懂&#xff0c;我没跟着它的编译&#xff0c;完了后把/lib下的 放到开发板的/usr/lib下 编写代码: #include <stdio.h> #inclu…

Grafana:云原生时代的数据可视化与监控王者

&#x1f407;明明跟你说过&#xff1a;个人主页 &#x1f3c5;个人专栏&#xff1a;《Grafana&#xff1a;让数据说话的魔术师》 &#x1f3c5; &#x1f516;行路有良友&#xff0c;便是天堂&#x1f516; 目录 一、引言 1、Grafana简介 2、Grafana的重要性与影响力 …

android native开发

framwork 一些重要的流程都是要放到native中做的 原因也很简单&#xff0c;效率&#xff0c;尤其是针对性能优化方面的&#xff0c;更离不开native开发 目前针对native开发也回顾下&#xff0c;总结下经验 1 jni开发有两种&#xff0c;app端一般是静态模式&#xff0c;要有jav…

主成分分析在R语言中的简单应用:使用mvstats包

在数据科学领域&#xff0c;主成分分析&#xff08;PCA&#xff09;是一种广泛使用的技术&#xff0c;主要用于数据降维和探索性数据分析。PCA可以帮助我们发现数据中的模式&#xff0c;减少数据集的复杂性&#xff0c;同时保持数据中最重要的特征。本文将介绍如何在R语言中使用…

如何面对并发下的bug

整理总结自蒋炎岩老师的b站课程&#xff0c;https://jyywiki.cn/OS/2022/index.html 并发bug与应对 应对bug的方法 在代码里边增加很多检查(加断言) #include "thread.h"unsigned long balance 100;void Alipay_withdraw(int amt) {if (balance > amt) {usleep(…

使用 FFmpeg 从音视频中提取音频

有时候我们需要从视频文件中提取音频&#xff0c;并保存为一个单独的音频文件&#xff0c;我们可以借助 FFmpeg 来完成这个工作。 一、提取音频&#xff0c;保存为 mp3 文件: 要使用 FFmpeg 从音视频文件中提取音频&#xff0c;并将 ACC 编码的音频转换为 MP3 格式&#xff0…

基于 docker-compose 部署 LNMP 架构

目录 前言 1、任务要求 2、Nginx 2.1 建立工作目录并上传相关安装包 2.2 编写 Nginx Dockerfile 脚本 2.3 准备 nginx.conf 配置文件 3、Mysql 3.1 建立工作目录并上传相关安装包 3.2 编写 Mysql Dockerfile 脚本 3.3 编写 my.cnf 配置文件 4、PHP 4.1 建立工作目录…

PDF解析--PyMuPDF (python 文档解析提取)

1.介绍 PyMuPDF 和Fitz 是用于Python中处理PDF文件的相关模块。Fitz是P有MuPDF的字模块。提供一个简化和封装版本的P有MuPDF功能。 关系&#xff1a; PyMuPDF&#xff1a; 提供广泛的功能&#xff0c;用于操作PDF文档&#xff0c; 包括方便的高级函数与底层操作Fitz &#x…