手把手开发ansible模块(一)

发布于 2023-12-14  136 次阅读


安装python3.11.5

由于python3.11 所以来的openssl版本过高,centos7.9 无法支持,所以需要再centos中安装python3.11,需要安装高版本的openssl11

1、安装依赖

# yum -y install gcc gcc-c++ zlib-devel bzip2-devel  sqlite-devel readline-devel libffi-devel unzip gdbm gdbm-devel sqlite openssl11 openssl11-devel ncurses-devel zlib libffi-devel readline readline-devel

2、准备安装包

# wget https://www.python.org/ftp/python/3.11.5/Python-3.11.5.tar.xz

# tar xf Python-3.11.5.tar.xz
# export CFLAGS=$(pkg-config --cflags openssl11)
# export LDFLAGS=$(pkg-config --libs openssl11)

# ./configure --prefix=/usr/local/python3 --with-ssl
# make && make install

--prefix :选项指定Python 3的安装目录为 /usr/local/mydev/python/python3,你可以根据自己的需要修改安装目录。(可选)。用法:--prefix=/usr/local/mydev/python/python3。

--enable-optimizations: 选项启用优化选项,以提高Python解释器的性能。这个选项会使用一些编译器优化和调整来提高Python的执行速度,但是会增加编译时间和内存使用。(可选)

--with-openssl :选项指定使用哪个OpenSSL库版本编译Python。在这个例子中,Python将使用 /usr/bin/openssl 路径下的OpenSSL库,这个路径下通常是OpenSSL库的二进制可执行文件的安装位置。(可选)。用法:--with-openssl=/usr/bin/openssl。

--with-ssl :与 --with-openssl二选一,此方式就是不指定ssl

3、配置aliyun pip源并安装ansible

# mkdir ~/.pip
# vim ~/.pip/pip.conf
[global]
index-url = http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com

4、安装ansible

# pip3 install ansible
# ansible --version
ansible [core 2.16.1]
  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/python3/lib/python3.11/site-packages/ansible
  ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/local/python3/bin/ansible
  python version = 3.11.5 (main, Dec  9 2023, 06:53:57) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] (/usr/local/python3/bin/python3.11)
  jinja version = 3.1.2
  libyaml = True

5、配置ansible基础环境

# vim /etc/ansible/ansible.cfg
[defaults]
inventory = /etc/ansible/hosts
forks = 5
become = root
remote_port  = 22
host_key_checking = False
timeout = 10
log_path = /var/log/ansible.log
private_key_file = /root/.ssh/id_rsa

# vim /etc/ansible/hosts
# 配置测试用的主机清单
[linux]
192.168.75.207
192.168.75.208

6、测试ansible连接

# 对每个被控端传递公钥
# ansible all -m ping
192.168.75.207 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
192.168.75.208 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}

到这里,ansible模块开发环境就准备完成了