2000 - 2016 年,维护的小型机、linux刚开始的2台增加到上千台,手工检查、日常版本升级需要管理太多设备,必须通过运维自动化实现
特别是版本升级,需要到同类机器部署代码、起停设备,必须在一台主控机上完成代码分发、远程服务起停、服务验证验证
2016年开始使用 pssh ,后改用 ansible ,ansible 配置如下
一、配置ssh免密码登录
#cd /root/.ssh
免交互生成密钥
#echo -e "\n" |ssh-keygen -t rsa -N ""
发送锁头给 web1机器
#ssh-copy-id -i /root/.ssh/id_rsa.pub root@web1 ,如下
[root@fox .ssh]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@web1
The authenticity of host 'web1 (192.168.80.131)' can't be established.RSA key fingerprint is 9c:c0:e2:2b:6f:88:c8:b1:25:47:f6:4b:fa:8f:cd:00.Are you sure you want to continue connecting (yes/no)? yesWarning: Permanently added 'web1,192.168.80.131' (RSA) to the list of known hosts.root@web1's password:[root@fox ~]# sed -i 's#fox#web01#g' /etc/sysconfig/network
[root@fox ~]# more /etc/sysconfig/network注:如果机器不同,需要自动应答时,可采用 expect 软件实现 ,expect 用法查见
https://www.cnblogs.com/yangmingxianshen/p/7967040.html
二、安装ansible
1. Master端安装EPEL第三方yum源
# rpm -Uvh http://ftp.linux.ncsu.edu/pub/epel/6/i386/epel-release-6-8.noarch.rpm2.安装Ansible# yum install ansible -y出现 Error: Cannot retrieve metalink for repository: epel. Please verify its path and try again解决方法: 一句话:把/etc/yum.repos.d/epel.repo,文件第3行注释去掉,把第四行注释掉。打开/etc/yum.repos.d/epel.repo,将[epel]name=Extra Packages for Enterprise Linux 6 - $basearch#baseurl=http://download.fedoraproject.org/pub/epel/6/$basearchmirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-6&arch=$basearch修改为[epel]name=Extra Packages for Enterprise Linux 6 - $basearchbaseurl=http://download.fedoraproject.org/pub/epel/6/$basearch#mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-6&arch=$basearch3。再清理源,重新安装yum clean all这一次安装成功# yum install ansible -y注:问题解决参考了https://www.cnblogs.com/dadong616/p/5062727.html
三、ansible 使用
1.远程命令模块 command: 执行远程主机SHELL命令 (这是在远程机器上执行命令)
# ansible webservers -m command -a "free -m"
2.script: 远程执行MASTER本地SHELL脚本.(类似scp+shell) 注意,这个脚本要放在 MASTER ,很实用的命令
#echo "hostname" > /tmp/test1.sh
#chmod 700 /tmp/test1.sh# ansible web -m script -a "/tmp/test1.sh"
web1 | SUCCESS => {"changed": true, "rc": 0, "stderr": "Shared connection to web1 closed.\r\n", "stderr_lines": ["Shared connection to web1 closed."], "stdout": "web01\r\n", "stdout_lines": ["web01"]}
其他模块和用法,详见 https://www.cnblogs.com/cheyunhua/p/8151603.html
总结:
1.工具使用提高效率
2 脚本整理好,随时使用
3 用于日常版本升级,一键部署环境、快速定位诊断等场合