> 文章列表 > 使用两个简单的shell脚本实现搭建ansible

使用两个简单的shell脚本实现搭建ansible

使用两个简单的shell脚本实现搭建ansible

目录

1.环境规划:

2.脚本my_ssh.sh

3.脚本my_ansible.sh


1.环境规划:

暂时没有写入地址映射和配置主机名的配置,如需要请自行添加!!!

角色 主机名 ip地址
控制主机 master 192.168.178.101
受控主机/被管节点 node1 192.168.178.151
受控主机/被管节点 node2 192.168.178.201

2.脚本my_ssh.sh

完成ssh公钥的创建和复制公钥文件以及ssh密钥登录的测试

#echo "开始修改地址映射"
#
#for ((i=0;i<${#ssh_hosts[*]};i++));do
#	sed -i '/^'"${ssh_hosts[$i]}"'/d' /etc/hosts
#	echo "尝试连接: ${ssh_networkname[$i]}"
#	timeout 5 ssh root@${ssh_networkname[$i]} "echo ${ssh_networkname[$i]}: 'This is success!'"
#	if [[ $? -ne 0 ]];then
#		echo "添加地址映射:${ssh_hosts[$i]} ${ssh_networkname[$i]}"
#		echo "${ssh_hosts[$i]} ${ssh_networkname[$i]}" >> /etc/hosts
#	fi
#doneexpect -v
if [ `echo $?` -ne 0 ];thenecho "安装expect命令"yum install -y expect
fi 
create_ssh_pub(){
echo "生成本地ssh公钥"
/usr/bin/expect << eof
# 设置捕获字符串后,期待回复的超时时间
set timeout 30spawn ssh-keygen -t rsa -b 1024## 开始进连续捕获
expect	{".ssh/id_rsa)"      { send "\\n";  exp_continue }"Overwrite (y/n)?"  { send "y\\n"; exp_continue }"no passphrase):"   { send "\\n";  exp_continue }"passphrase again:" { send "\\n";  exp_continue }
}
eof
}
if [ ! -f /root/.ssh/id_rsa.pub ];thencreate_ssh_pub
fi# 定义复制ssh公钥方法
copy_ssh(){
echo "复制公钥到对应的主机上"
/usr/bin/expect << eof
# 设置捕获字符串后,期待回复的超时时间
set timeout 30spawn ssh-copy-id -i /root/.ssh/id_rsa.pub $1@$2## 开始进连续捕获
expect	{"connecting (yes/no)?" { send "yes\\n";  exp_continue }"s password:"          { send "${ssh_passwd}\\n"; exp_continue }
}
eof
}
for ip in ${ssh_hosts[*]};dotimeout 5 ssh root@${ip} "echo ${ip}: 'This is success!'"if [[ $? -ne 0 ]];thenecho "复制文件到: ${ip}"copy_ssh root ${ip} > /dev/nullfidone

3.脚本my_ansible.sh

完成chrony时间服务的配置和ansible工具的安装

#! /bin/bashallows_hosts=192.168.178.0
allows_mask=24systemctl restart chronyd
if [[ $? -ne 0 ]];thenecho "本地安装chrony"yum install -y chrony &> /dev/null && systemctl restart chronydif [[ $? -ne 0 ]];thenecho "安装失败,请排错!"fi
fi
echo "本地配置chrony"
sed -i '/^server/d' /etc/chrony.conf
sed -i '2a\\server ntp.aliyun.com iburst\\' /etc/chrony.conf
sed -i 's/#allow 192.168.0.0\\/16/allow '"${allows_hosts}"'\\/'"${allows_mask}"'/' /etc/chrony.conf
sed -i 's/#local stratum 10/local stratum 10/' /etc/chrony.conf
sleep 2
systemctl restart chronyd && systemctl enable chronyd &> /dev/null 
sleep 5
timedatectl set-ntp true && chronyc sources -v | sed -n '/^\\^\\*/p'ssh_master=192.168.178.101
ssh_hosts=(192.168.178.151 192.168.178.201)echo "开始配置chrony"
for ip in ${ssh_hosts[*]};doecho "配置 ${ip}"ssh root@${ip} "systemctl restart chronyd"if [[ $? -ne 0 ]];thenecho "${ip} 安装chrony"ssh root@${ip} "yum install -y chrony &> /dev/null && systemctl restart chronyd"if [[ $? -ne 0 ]];thenecho "安装失败,请排错!"fifissh root@${ip} "sed -i '/^server/d' /etc/chrony.conf;sed -i '2a\\server '"${ssh_master}"' iburst\\' /etc/chrony.conf"ssh root@${ip} "systemctl restart chronyd && systemctl enable chronyd &> /dev/null"sleep 5ssh root@${ip} "timedatectl set-ntp true && chronyc sources -v | sed -n '/^\\^\\*/p'"
doneecho "安装ansible"
ansible --version
if [[ $? -ne 0 ]];thenyum install -y epel-release &> /dev/null && yum install -y ansible &> /dev/null && ansible --versionif [[ $? -ne 0 ]];thenecho "安装失败,请排错!"fi
fi