目录
1.环境规划:
暂时没有配置主机名的配置,如需要请自行添加!!!
角色 | 主机名 | ip地址 |
---|---|---|
控制主机 | master | 192.168.178.101 |
受控主机/被管节点 | node1 | 192.168.178.151 |
受控主机/被管节点 | node2 | 192.168.178.201 |
2.脚本my_ssh.sh
完成地址映射,ssh公钥的创建和复制公钥文件以及ssh密钥登录的测试
#! /bin/bash
# 控制主机和被控主机的IP数组,第一个元素是控制主机的IP地址
ssh_hosts=(192.168.178.101 192.168.178.151 192.168.178.201)
# 控制主机和被控主机的IP数组,第一个元素是控制主机的域名地址
ssh_networkname=(master node1 node2)
ssh_passwd=110119
# 定义修改/etc/hosts文件的方法
address_hosts(){
hosts_num=0
for name in ${ssh_networkname[*]};do
hosts_num=$(($hosts_num+`grep -c ''"${name}"'$' /etc/hosts`))
done
# 判断hosts文件中的域名行是否为ssh_hosts的长度
if [ ${hosts_num} -eq ${#ssh_networkname[*]} ];then
echo "/etc/hosts已经配置!"
return
fi
echo "本地开始修改地址映射"
cat << eof > /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
eof
for ((i=0;i<${#ssh_hosts[*]};i++));do
sed -i '$a\'"${ssh_hosts[$i]}"' '"${ssh_networkname[$i]}"'' /etc/hosts
done
}
address_hosts
expect -v &> /dev/null
if [ `echo $?` -ne 0 ];then
echo "没有expect,安装expect命令"
yum install -y expect
fi
create_ssh_pub(){
echo "生成本地ssh公钥"
/usr/bin/expect << eof
# 设置捕获字符串后,期待回复的超时时间
set timeout 30
spawn 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 ];then
create_ssh_pub
fi
# 定义复制ssh公钥方法
copy_ssh(){
echo "复制公钥到对应的主机上"
/usr/bin/expect << eof
# 设置捕获字符串后,期待回复的超时时间
set timeout 30
spawn 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 ((j=0;j<${#ssh_networkname[*]};j++));do
timeout 5 ssh root@${ssh_networkname[$j]} "echo ${ssh_networkname[$j]}: 'This is success!'"
if [ $? -ne 0 ];then
echo "复制文件到: ${ssh_networkname[$j]}"
copy_ssh root ${ssh_networkname[$j]} > /dev/null
fi
scp /etc/hosts root@${ssh_networkname[$j]}:/etc
done
3.脚本my_ansible.sh
完成chrony时间服务的配置和ansible工具的安装
#! /bin/bash
allows_hosts=192.168.178.0
allows_mask=24
ssh_manager=192.168.178.52
ssh_networkname=(master node1 node2)
# 节点开始配置chrony
for name in ${ssh_networkname[*]};do
echo "开始配置 ${name}"
ssh root@${name} "systemctl restart chronyd"
if [ $? -ne 0 ];then
echo "${name} 安装chrony"
ssh root@${name} "yum install -y chrony &> /dev/null && systemctl restart chronyd"
if [ $? -ne 0 ];then
echo "安装失败,请排错!"
fi
fi
if [ ${name} == ${ssh_networkname[0]} ];then
echo "${name}配置chrony"
ssh root@${name} "sed -i '/^server/d' /etc/chrony.conf"
ssh root@${name} "sed -i '2a\server ntp.aliyun.com iburst\' /etc/chrony.conf"
ssh root@${name} "sed -i 's/#allow 192.168.0.0\/16/allow '"${allows_hosts}"'\/'"${allows_mask}"'/' /etc/chrony.conf"
ssh root@${name} "sed -i 's/#local stratum 10/local stratum 10/' /etc/chrony.conf"
sleep 2
ssh root@${name} "systemctl restart chronyd && systemctl enable chronyd &> /dev/null"
sleep 5
ssh root@${name} "timedatectl set-ntp true && chronyc sources -v | sed -n '/^\^\*/p'"
else
echo "${name}配置chrony"
ssh root@${name} "sed -i '/^server/d' /etc/chrony.conf;sed -i '2a\server '"${ssh_networkname[0]}"' iburst\' /etc/chrony.conf"
ssh root@${name} "systemctl restart chronyd && systemctl enable chronyd &> /dev/null"
sleep 5
ssh root@${name} "timedatectl set-ntp true && chronyc sources -v | sed -n '/^\^\*/p'"
fi
done
# 安装ansible
ansible --version &> /dev/null
if [ $? -ne 0 ];then
echo "没有ansible,安装ansible"
yum install -y epel-release &> /dev/null && yum install -y ansible &> /dev/null && ansible --version
if [ $? -ne 0 ];then
echo "安装失败,请排错!"
fi
fi
echo "开始配置主机清单"
for name in ${ssh_networkname[*]};do
sed -i '/^'"${name}"'/d' /etc/ansible/hosts
sed -i '$a\'"${name}"'' /etc/ansible/hosts
done
echo "测试结果,请和设置的数据变量对比"
ansible all -m ping | sed -n '/.*SUCCESS/p'