ansible软件的部署流程
ansible软件自动化环境架构规划
管理主机1台:
10.0.0.61 m01
受控主机3台:
10.0.0.41 backup
10.0.0.31 nfs01
10.0.0.7 web01
第一个里程:免交互方式创建密钥对
[root@m01 /]# ssh-keygen -t dsa -f /root/.ssh/id_dsa -N ""
Generating public/private dsa key pair.
Your identification has been saved in /root/.ssh/id_dsa.
Your public key has been saved in /root/.ssh/id_dsa.pub.
The key fingerprint is:
47:78:c8:4a:5f:70:ae:8a:28:f0:91:b2:de:5a:ce:c3 root@m01
The key's randomart image is:
+--[ DSA 1024]----+
| . . |
| . * |
| . + = |
| . . o = |
|o o . S . |
|.+ o . . . |
|o +.. . |
|..=E |
| o.+. |
+-----------------+
第二个里程:批量分发密钥
1.安装sshpass
[root@m01 scripts]# yum -y reinstall sshpass
Loaded plugins: fastestmirror, security
Setting up Reinstall Process
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package sshpass.x86_64 0:1.06-1.el6 will be reinstalled
--> Finished Dependency Resolution
Dependencies Resolved
=====================================================================================
Package Arch Version Repository Size
=====================================================================================
Reinstalling:
sshpass x86_64 1.06-1.el6 epel 20 k
Transaction Summary
=====================================================================================
Reinstall 1 Package(s)
Total download size: 20 k
Installed size: 34 k
Downloading Packages:
sshpass-1.06-1.el6.x86_64.rpm | 20 kB 00:00
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : sshpass-1.06-1.el6.x86_64 1/1
Verifying : sshpass-1.06-1.el6.x86_64 1/1
Installed:
sshpass.x86_64 0:1.06-1.el6
Complete!
2.编写脚本,使用shell脚步进行分发
[root@m01 scripts]# cat ansible.sh
#!/bin/bash
#服务器的ip地址
ip='7 31 41'
#密钥的类型
key='dsa'
if [ -f "/root/.ssh/id_$key" ]
then
rm -f /root/.ssh/$id_key
fi
#创建免交互密钥对
ssh-keygen -t $key -f /root/.ssh/id_dsa -N ""
for ip in $ip
do
#命名用sshpass进行批量分发公钥
sshpass -pliuyang ssh-copy-id -i /root/.ssh/id_$key.pub "-p10000 -o StrictHostKeyChecking=no 1
72.16.1.$ip "
done
#3.使用测试脚本来确定分发是否完成
[root@m01 scripts]# cat check_ansible.sh
#!/bin/bash
ip='7 31 41'
if [ $# -ne 1 ]
then
echo "请输入一个参数!"
exit 1
fi
for ip in $ip
do
echo =====172.16.1.$ip==========
ssh -p10000 172.16.1.$ip $1
echo ==================
done
[root@m01 scripts]# sh check_ansible.sh
=====172.16.1.7==========
web01
==================
=====172.16.1.31==========
nfs01
==================
=====172.16.1.41==========
backup
==================
[root@m01 scripts]#
第三个里程:ansible软件安装
服务端:
yum -y install ansible
客户端:(可选)
yum -y install libselinux-python
第四个里程:在/etc/ansible/hosts文件中添加受控主机ip
[root@m01 ansible]# cat /etc/ansible/hosts
#设置主机分组,可以根据服务器类型进行分组
[oldboy]
172.16.1.7
172.16.1.31
172.16.1.41
#设置连接的端口号用户名,密码等信息
[oldboy:vars]
ansible_ssh_port=10000
ansible_ssh_user=root
ansible_ssh_password=12345
[root@m01 ansible]#
第五个里和:测试
[root@m01 ansible]# ansible oldboy -m command -a "hostname"
172.16.1.41 | SUCCESS | rc=0 >>
backup
172.16.1.31 | SUCCESS | rc=0 >>
nfs01
172.16.1.7 | SUCCESS | rc=0 >>
web01
[root@m01 ansible]#