ansible - TuPengXiong/TuPengXiong.github.io GitHub Wiki
#!/bin/bash
#
#############################################
# author:ellen
# describes:自动化安装和配置ansible
# version:v1.0
# updated:20170531
#############################################
#
# 主机列表文件
hostfile='/root/hosts'
# 错误信息以红色显示
function _err
{
echo -e "\033[1;31m[ERROR] $@\033[0m"
}
# 一般信息以绿色显示
function _info
{
echo -e "\033[1;32m[Info] $@\033[0m"
}
# 仅限指定用户运行本脚本
if [ $EUID != "0" ];then
echo "Please use root run script!!!"
exit 1
fi
rpm -qa|grep ansible
if [ $? -eq 0 ];then
_err "ansible 已存在,无需重复安装!退出..."
exit 1
fi
if [ -e $hostfile ];then
yum list|grep ansible
if [ $? -ne 0 ];then
_err "仓库不存在ansible的rpm包,退出..."
exit 1
else
yum install ansible -y
if [ $? -eq 0 ];then
_info "ansible 安装完毕..."
sed -i "s@\#host_key_checking = False@host_key_checking = False@g" /etc/ansible/ansible.cfg
sed -i "s@\#log_path = \/var\/log\/ansible.log@log_path = \/var\/log\/ansible.log@g" /etc/ansible/ansible.cfg
cp $hostfile /etc/ansible/hosts
_info "$hostfile 已拷贝至 /etc/ansible/目录"
ssh-keygen -t rsa -P '' -f /root/.ssh/id_rsa
_info "请按以下提示输入 ${hostfile} 列表中的主机密码:"
ansible all -m authorized_key -a "user=root key='{{ lookup('file', '/root/.ssh/id_rsa.pub') }}' path=/root/.ssh/authorized_keys manage_dir=no" --ask-pass -c paramiko
sed -i "s@\#private_key_file = \/path\/to\/file@private_key_file = \/root\/.ssh\/id_rsa@g" /etc/ansible/ansible.cfg
_info "ansible 已部署完毕!"
else
_err "ansible 安装失败!"
fi
fi
else
_err "$hostfile 主机列表文件不存在,请检查!"
exit 1
fi