话不多说脚本呈上
如果对您有帮助麻烦点个赞和关注
#/bin/bash
#function ansible自动部署
#author Lil Nas X 2021-11-22
systemctl stop firewalld.service
setenforce 0
ssh-keygen -f /root/.ssh/id_rsa -N ''
python -V | grep "python 2.7.*" | grep "python 2.6.*"
if [ $? -eq 0 ]
then
echo "您的主机没有2.6版本以上的python,正在为您安装"
cd /
wget --no-check-certificate https://www.python.org/ftp/python/2.7.9/Python-2.7.9.tar.xz
tar -xf Python-2.7.9.tar.xz
cd Python-2.7.9
./configure --prefix=/usr/local
make && make altinstall
ln -s /usr/local/bin/python2.7 /usr/local/bin/python
source /etc/profile
python -V | grep "python 2.7.*" | grep "python 2.6.*"
if [ $? -eq 0 ]
then
echo "安装python2.7.9成功"
fi
fi
echo "正在安装ansible"
yum install -y epel-release
yum install -y ansible
read -p "输入你要创建组的名称" na
echo "[$na]" >>/etc/ansible/hosts
read -p "你要添加多少台被控主机:" cb
i=1
while [ $i -le $cb ]
do
read -p "请您输入第$i个被控制端的IP:" aa
echo $aa >>/etc/ansible/hosts
ssh-keyscan -H $aa >> ~/.ssh/known_hosts
echo -e "\033[32m 输入对方主机密码 \033[0m"
ssh-copy-id -i /root/.ssh/id_rsa.pub $aa
let i++
done
echo -e "\033[32m 搭建完成 \033[0m"
echo "可以使用ansible web -m command -a 'ifconfig ens33'验证"