当前位置:网站首页>ansible部署脚本--亲测可用无坑
ansible部署脚本--亲测可用无坑
2022-08-04 08:52:00 【IT大白鼠】
环境:centos7,外网通
脚本如下:
#!/bin/bash
#function:ansible
#author:tommypeng 20220801
#####root判断#####
if
[ "$USER" != "root" ]
then
echo "错误:非root用户,权限不足!"
exit 0
fi
###############防火墙及SElinux############
systemctl stop firewalld && systemctl disable firewalld && echo "防火墙已经关闭"
sed -i 's/SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config && echo "关闭selinux"
##############网络测试##############
ping -c 3 www.baidu.com
if
[ $? = 0 ]
then
echo "外网通讯良好!"
else
echo "丫的你在逗我吗?网都没有安装个毛线!"
exit 1
fi
##############yum源配置################
yum install epel-release -y
mv /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.bak >> /dev/null
yum clean all
rpm -Uvh http://mirrors.ustc.edu.cn/epel/epel-release-latest-7.noarch.rpm
yum makecache
###########ansible安装#########
yum -y install ansible
##########主机组配置#########
read -p "请输入主机组的名称: " ppo
read -p "请输入主机1的IP: " wokao
read -p "请输入主机2的IP: " haoshuai
sleep 5
cat >> /etc/ansible/hosts << EOF
[$ppo]
$wokao
$haoshuai
EOF
###########免密登录#########
rm -rf /root/.ssh/* ##清除已有的密钥对
ssh-keygen -N '' -f /root/.ssh/id_rsa ###创建密钥对(验证码为空)
ssh-copy-id -i /root/.ssh/id_rsa.pub $wokao
ssh-copy-id -i /root/.ssh/id_rsa.pub $haoshuai ####传递公钥
##########测试############
ansible $ppo -m command -a 'ifconfig'
if [ $? -eq 0 ];then
echo -e "\n\033[32m-----------------------------------------------\033[0m"
echo -e "\033[32m测试成功ansible安装成功 !\033[0m"
else
echo -e "\033[32m测试失败ansible安装失败,即将退出 !"
exit 0
fi
默认主机清单包含两台主机,如部署完成后,需要添加其他主机,请编辑文件 /etc/ansible/hosts
在您的主机清单添加相应主机的IP即可。
[[email protected] ~]# vim /etc/ansible/hosts
# Ex 2: A collection of hosts belonging to the 'webservers' group
## [webservers]
## alpha.example.org
## beta.example.org
## 192.168.1.100
## 192.168.1.110
。
。
。
。
[pxg]
192.168.137.213
192.168.137.214
还须配置免密登录,具体如下:
ssh-copy-id -i /root/.ssh/id_rsa.pub 主机IP ###将公钥发送给新加入的主机,请注意需要输入新加入主机的root密码,按提示输入即可,示例:
[[email protected] ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub 192.168.137.216
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.137.216 (192.168.137.216)' can't be established.
ECDSA key fingerprint is SHA256:YejXn/pAjoOSNaOfxVO+TiBPp6JmHfZ5Z8nbiUN5m0U.
ECDSA key fingerprint is MD5:b6:8d:e0:b0:08:5c:29:dc:df:2e:87:4e:51:e7:20:48.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '192.168.137.216'"
and check to make sure that only the key(s) you wanted were added.
拓展:主机清单的写法:
[pxg]
192.168.137.155
192.168.137.162
以上意思为:主机组pxg包含这两个IP的主机
定义嵌套组
通过创建后缀为:children的主机组名称来实现,例子如下:
[web]
web1.example.com
web2.example.com
[db]
db1.example.com
db2.example.com
[all:children]
web
db
上述例子可以改成
[web]
web[1:2].example.com
[db]
db[1:2].example.com
[webdb:children]
web
db
边栏推荐
- inject() can only be used inside setup() or functional components.
- 速速脱单诀窍
- [NOI Simulation Competition] Paper Tiger Game (Game Theory SG Function, Long Chain Division)
- 大佬们,mysql里text类型的字段,FlinkCDC需要特殊处理吗 就像处理bigint uns
- Fiddler(二)-手机抓包502错误解决方法
- 通过GBase 8c Platform安装数据库集群时报错
- About Oracle RAC 11g rebuilding the disk group
- 第一次用postgreSQL,想装主从,用的12.7 tar.gz版本。安装好后没在 share目录下找到样例配置recovery.conf.sample,是安装方式不对,还是路径不对?
- 线程安全问题
- 此时已莺飞草长,愿世间美好与你环环相扣
猜你喜欢
2022年制冷与空调设备运行操作特种作业证考试题库及模拟考试
【高并发基石】多线程、守护线程、线程安全、线程同步、互斥锁
inject() can only be used inside setup() or functional components.
spark算子讲解
经典动态规划问题的递归实现方法——LeetCode39 组合总和
华为设备配置VRRP与NQA联动监视上行链路
The difference between character stream and byte stream
华为设备配置VRRP与路由联动监视上行链路
去掉js代码文件所有注释
经典递归回溯问题之——解数独(LeetCode 37)
随机推荐
csdn图片去水印 | 其他方法无效时的解决方案
从零开始的tensorflow小白使用指北
通过GBase 8c Platform安装数据库集群时报错
BFM模型和Landmarks可视化
速速脱单诀窍
GBase 8c数据库集群中,怎么替换节点呢?比如设置A节点为gtm,换到B节点上。
金仓数据库KingbaseES客户端编程接口指南-JDBC(7. JDBC事务处理)
一道[CSCCTF 2019 Qual]FlaskLight的详解再遇SSTI
思想茶叶蛋 (Jul 31,2022)| 元宇宙(Metaverse)下了一枚什么样的蛋
从底层看 Redis 的五种数据类型
sql在字段重复时 对某个字段根据最新时间取数
Fiddler(二)-手机抓包502错误解决方法
C# DirectoryInfo类
字符流与字节流的区别
注意力机制
async - await
Apache Druid 实时分析数据库入门介绍
inject() can only be used inside setup() or functional components.
经典递归回溯问题之——解数独(LeetCode 37)
学会 Arthas,让你 3 年经验掌握 5 年功力