脚本简介
- 基于运维统一脚本中,2、Ansible自动化下的Ansible安装部署
- 使用yum安装(yum仓库),系统版本Centos7
脚本注解
- 该脚本为了安装服务的快速简便,提供两种安装方式,在线安装和离线安装
- 在线安装,则执行脚本后,根据提示选择在线安装的方式即可
- 离线安装,则需要提前把下载离线安装包,且存放在指定的路径下
- 修改脚本,最上边的变量即可
ansible_offline="ansible_2.9.tar.gz"
ansible_name="ansible"
ansible_opt="/opt/ansible"
- ansible离线安装包下载地址: 点击链接
执行方式
sh install_anisble.sh
脚本内容
#!/bin/bash
basepath=$(cd `dirname $0`; pwd)
ansible_offline="ansible_2.9.tar.gz"
ansible_name="ansible"
ansible_opt="/opt/ansible"
LAN_IP=$(ip addr| grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|cut -d "/" -f 1 | awk '{print $1}' | head -1)
info(){
echo -e "\033[35m 【`hostname` `date '+%Y-%m-%d %H:%M:%S'`】\033[0m" "\033[36m$1\033[0m "
}
info1(){
echo -e "\033[36m $1 \033[0m "
}
info2(){
echo -e "\033[34m 【`hostname` `date '+%Y-%m-%d %H:%M:%S'`】\033[0m" "\033[35m$1\033[0m "
}
info3(){
echo -e "\033[36m------------------------------------------------------------------------------\033[0m"
}
info5(){
info1 "=============================================================================="
}
info2(){
echo -e "\033[31m 【`hostname` `whoami` `date '+%Y-%m-%d %H:%M:%S'`】\033[0m" "\033[34m$1\033[0m "
}
info(){
echo -e "\033[35m 【`hostname` `whoami` `date '+%Y-%m-%d %H:%M:%S'`】\033[0m" "\033[36m$1\033[0m "
}
info1(){
echo -e "\033[91m 【`hostname` `whoami` `date '+%Y-%m-%d %H:%M:%S'`】\033[0m" "\033[35m$1\033[0m"
}
set_firewalld(){
systemctl stop firewalld && systemctl disable firewalld
getenforce | grep -iq 'Enforcing' && setenforce 0
sed -i 's|SELINUX=\(.*\)|SELINUX=disabled|g' /etc/selinux/config
}
python(){
python2 -V &>/dev/null
if [ $? -ne 0 ];then
if cat /etc/redhat-release &>/dev/null;then
yum install gcc openssl-devel bzip2-devel
wget https://www.python.org/ftp/python/2.7.16/Python-2.7.16.tgz
tar xzf Python-2.7.16.tgz
cd Python-2.7.16
./configure --enable-optimizations
make altinstall
ln -s /usr/bin/python2.7 /usr/bin/python
cd -
else
apt-get install -y python2.7 && ln -s /usr/bin/python2.7 /usr/bin/python
fi
fi
}
off_line_install_ansible(){
if [ -e ${ansible_opt}/${ansible_offline} ];then
info "使用离线安装包,安装ansible,请等待!!!"
info "Check the offline installation package"
info7 "${ansible_opt}/${ansible_offline}"
if [ -e ${ansible_opt}/${ansible_offline} ];then
cd ${ansible_opt}
tar -xf ${ansible_offline}
cd ${ansible_name}
yum -y install ./*.rpm > ${log_dir}/${ansible_name}/yum_install_${ansible_name}.log
[ $? -eq 0 ] && info "Ansible is installed successfully"
cd ${bashpath}
rm -rf ${ansible_opt}/${ansible_name}
else
info2 "Ansible is ${ansible_opt}/${ansible_offline}不存在,请下载"
fi
else
mkdir -p ${ansible_opt}
info6 "${ansible_opt}/${ansible_offline}离线安装包不存在,请下载后存放在指定的路径下重新执行脚本"
info8 "下载地址:https://download.csdn.net/download/qq_44246980/85177886"
exit -1
fi
}
yum_ansible(){
if [ `rpm -qa | grep ansible|wc -l` -eq 1 ];then
info1 "ansible 已经安装,请勿重新安装!!!"
else
info "在线安装ansible,请等待!!!"
if [ `rpm -qa | grep epel-release|wc -l ` -ne 1 ];then
rpm -ivh https://mirror.webtatic.com/yum/el7/epel-release.rpm &>/dev/null
yum makecache >/dev/null
yum install -y ansible sshpass > ${bashpath}/${log_dir_name}/${ansible_name}/yum_install_${ansible_name}.log
[ $? -eq 0 ] && info "Ansible is installed successfully" \
&& info1 "日志: ${log_dir_name}/${ansible_name}/yum_install_${ansible_name}.log"
else
info "epel-release been installed"
info "使用在线安装,请等待!!!"
yum install -y ansible sshpass -y > ${log_dir_name}/${ansible_name}/yum_install_${ansible_name}.log
[ $? -eq 0 ] && info "Ansible is installed successfully" \
&& info1 "日志: ${log_dir_name}/${ansible_name}/yum_install_${ansible_name}.log"
fi
fi
}
set_ansibel_cfg(){
sed -i "s/#host_key_checking = False/host_key_checking = False/g" /etc/ansible/ansible.cfg
sed -i "/deprecation_warnings=False/d" /etc/ansible/ansible.cfg
sed -i '/defaults/adeprecation_warnings=False' /etc/ansible/ansible.cfg
}
ansible_run(){
local log_name="ansible"
info3
read -p "选择${log_name}的安装方式:(在线=0,离线=1):" start_install_ansible
start_install_ansible=${start_install_ansible:-0}
[ -d ${log_dir_name}/${ansible_name} ] || mkdir -p ${log_dir_name}/${ansible_name}
if [ ${start_install_ansible} -eq 0 ];then
yum_ansible
set_ansibel_cfg
else
off_line_install_ansible
set_ansibel_cfg
fi
}
ansible_run