运维自动化工具-ansible篇1-安装

ansible是自动化运维的基础工具,可以执行批量的配置管理、应用部署和任务编排,通过调用远程服务器的ssh无客户端模式或调用python执行批量管理任务。

适用于大规模 IT 基础设施的自动化操作,如批量分发软件包,配置文件,执行脚本等。
ansible是由python开发的,ansible版本与python版本匹配,需要先在系统安装python,通过yum/apt安装的python版本较低,会导致ansible版本也低,所以通过编译安装方式先安装python3,如python3.9.17

1.环境准备(修改yum源为阿里源,安装编译环境等软件,安装lrzsz软件)

mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup

curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo

yum makecache

yum install eple-release -y

yum install wget lrzsz -y

yum install  gcc libffi-devel zlib* openssl-devel -y

2.下载python3.9.17并安装

wget https://www.python.org/ftp/python/3.9.17/Python-3.9.17.tgz 

tar xf Python-3.9.17.tgz 

cd Python-3.9.17

./configure prefix=/usr/local/python3 

make altinstall

ls /usr/local/

ln -s /usr/local/python3/bin/python3.9 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3.9 /usr/bin/pip3
python3 -V
pip3 -V

ll /usr/bin |grep python  # 查看软连接指向

3.安装ansible

pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple ansible 

检查ansible目录和配置文件,如果没有自动生成,则创建

mkdir /etc/ansible -p 

vim /etc/ansible/ansible.cfg  

[defaults]
inventory = /etc/ansible/hosts
host_key_checking = False
deprecation_warnings = False
#interpreter_python = /usr/bin/python3
[inventory]
[privilege_escalation]
[paramiko_connection]
[ssh_connection]
[persistent_connection]
[accelerate]
[selinux]
[colors]
[diff]

##以上为ansible.cfg配置文件,并将配置文件复制到

scp ansible.cfg /usr/local/python3/lib/python3.9/site-packages/ansible/

编辑ansible的主机清单 /etc/ansible/hosts

[root@ans-188 scripts]# cat /etc/ansible/hosts
[backup]
10.10.105.41
[nfs]
10.10.105.31
[shuzihua]
10.10.106.39
10.10.106.40
10.10.106.41
10.10.106.42
10.10.106.43
10.10.106.44

4.测试

生成ssh免证登录密钥,

ssh-keygen

ssh-copy-id 10.10.105.31  #将公钥复制到目标服务器

 测试成功。

 

posted on 2025-02-18 00:42  包工头日记  阅读(40)  评论(0)    收藏  举报

导航