运维自动化工具-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   SRE-基础-进阶-实践  阅读(4)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示