Ansible
1、安装插件传入sshpass包并安装
[root@localhost ~]# yum install -y lrzsz
[root@localhost ~]# rpm -ivh sshpass-1.06-1.el6.x86_64.rpm
2、编写密码文件
[root@localhost ~]# vi serip.txt
3、编写免密登陆脚本
[root@localhost~]# vi ssh_rsa.sh
#!/bin/bash
logname=“date+%Y-%m-%d”
echo “#################################################################分####隔######################################################”>>$logname.log
nl=rpm -qa|grep "sshpass"|wc -l
nm=rpm -qa|grep "openssh"|wc -l
[[ KaTeX parse error: Expected 'EOF', got '&' at position 13: nl -lt 1 ]] &̲& { echo "请安装:s…logname.log;exit; }
[[ KaTeX parse error: Expected 'EOF', got '&' at position 13: nm -lt 1 ]] &̲& { echo "请安装插件…logname.log;exit; }
sed -i ‘/StrictHostKeyChecking/c StrictHostKeyChecking no’ /etc/ssh/ssh_config
[ -e ~/.ssh/id_rsa.pub ] || ssh-keygen -t rsa -N ‘’ -f ~/.ssh/id_rsa -q
for ip in cat serip.txt|awk '{print $1}'
;do
port=cat serip.txt|grep "$ip"|awk '{print $4}'
ps=cat serip.txt|grep "$ip"|awk '{print $3}'
us=cat serip.txt|grep "$ip"|awk '{print $2}'
rsa=awk '{print $2}' ~/.ssh/id_rsa.pub
rsanu=sshpass -p $ps ssh -o StrictHostKeyChecking=no -p $port $us@$ip "if [ -e ~/.ssh/authorized_keys ];then cat ~/.ssh/authorized_keys ; fi"|grep $rsa|wc -l
if [ $rsanu -eq 0 ];then
sshpass -p $ps ssh-copy-id -i ~/.ssh/id_rsa.pub -o StrictHostKeyChecking=no -p $port
u
s
@
us@
us@ip >/dev/null 2>&1
fi
done
4、授权执行
[root@localhost ~]# chmod a+x ssh_rsa.sh
[root@localhost~]# ./ssh_rsa.sh
[root@x4d000005 ~]# sed -i ‘s/\r$//’ ssh_rsa.sh
5、ansible借助公钥批量管理,利用非交换式工具实现批量分发公钥与批量管理服务器
[root@x4d000005 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@77.0.0.7
6、安装ansible(如果无法安装找不到软件包,则先安装第三方源epel-release)
[root@localhost ~]# yum -y install ansible
7、查看版本信息
[root@localhost ~]# ansible --version
8、添加配置主机文件
[root@localhost ~]# vi /etc/ansible/hosts
9、验证ansible
[root@localhost ~]# ansible web -m ping
10、本地服务端安装httpd服务
[root@localhost ~]# yum -y install httpd
11、编写在客户端安装Apache服务的剧本
- hosts: web
remote_user: root
vars:- package: httpd
- service: httpd
tasks: - name: install httpd package
yum: name={{package}} state=latest - name: install configure file
template: src=/etc/httpd/conf/httpd.conf dest=/etc/httpd/conf/httpd.conf
notify:- restart httpd
- name: start httpd server
service: name={{service}} enabled=true state=started
handlers: - name: restart httpd
service: name={{service}} state=restarted
12、检查剧本语法
[root@localhost ~]# ansible-playbook --syntax-check webser.yml
13、执行剧本文件
[root@localhost ~]# ansible-playbook webser.yml
14、在客户端(另一台远程机子)测试安装结果