ansible手动添加模块

 

 

由于使用pip安装的ansible,自带的模块会比较少,有的模块会不存在,需要自己手动添加

yum安装的ansible,基本上不会缺少模块,如果有缺少,操作的方式也是一样的

安装ansible

CentOS 系列

pip的方式安装ansible,需要先安装pip,并且ansible也需要用到python-devel

python2

yum install -y python-devel pip

python3

yum install -y python3-devel pip3

-i参数指定pip源,默认为官方源,速度比较慢,这里使用的是阿里源

pip3 install ansible -i https://mirrors.aliyun.com/pypi/simple/

ModuleNotFoundError: No module named 'setuptools_rust'

如果出现这个报错,执行如下命令,然后再次执行上面的ansible安装命令即可

pip3 install setuptools_rust -i https://mirrors.aliyun.com/pypi/simple/

distutils.errors.DistutilsError: Command '['/usr/bin/python3', '-m', 'pip', '--disable-pip-version-check', 'wheel', '--no-deps', '-w', '/tmp/tmpnzvqpzk3', '--quiet', 'cffi>=1.12']' returned non-zero exit status 1

如果出现这个报错,执行如下命令,然后再次执行上面的ansible安装命令即可

pip3 install cffi -i https://mirrors.aliyun.com/pypi/simple/

To update pip, run:

​ pip install --upgrade pip

如果出现这个报错,执行如下命令,然后再次执行上面的ansible安装命令即可

pip3 install --upgrade pip -i https://mirrors.aliyun.com/pypi/simple

看到Successfully installed ansible-xxx,则安装成功

yum的方式安装ansible

ansible在epel源里面,因此,需要先安装epel源

yum install -y epel-release.noarch && yum install -y ansible

验证ansible版本

ansible --version

这是pip安装的

ansible [core 2.11.2]
  config file = None
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/bin/ansible
  python version = 2.7.13 (default, Jan 11 2017, 10:56:06) [GCC]
  jinja version = 2.11.3
  libyaml = True

这是yum安装的

ansible [core 2.11.2]
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.6/site-packages/ansible
  ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/local/bin/ansible
  python version = 3.6.8 (default, Nov 16 2020, 16:55:22) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]
  jinja version = 3.0.1
  libyaml = True

可以看出,yum安装的,默认配置文件在/etc/ansible/ansible.cfg,而pip安装的,默认没有配置文件,需要自己定义

定义ansible配置文件路径

export ANSIBLE_CONFIG=/etc/ansible/ansible.cfg

可以写在/etc/profile文件内,然后执行source /etc/profile,使配置生效

为ansible添加模块

ansible的模块,存储在ansible --versionansible python module location字段给出的目录下的modules目录下面

也就是上面的/usr/lib/python2.7/site-packages/ansible/modules或者/usr/local/lib/python3.6/site-packages/ansible/modules目录下

我的ansible缺少了synchronize这个模块

wget -O /usr/lib/python2.7/site-packages/ansible/modules/synchronize.py https://github.com/ansible/ansible-modules-core/blob/devel/files/synchronize.py

这样,我就有了synchronize这个模块了

posted @ 2022-02-19 23:33  月巴左耳东  阅读(756)  评论(0编辑  收藏  举报