代码拉取完成,页面将自动刷新
#!/usr/bin/env bash
ansible='192.168.1.100'
image='192.168.1.100'
mip='192.168.1.20'
nip='192.168.1.21 192.168.1.22 192.168.1.23'
port=22
user=root
password=2000
pwd=$(pwd)
echo "初始化安装环境!"
if [ -e /root/.ssh/id_rsa ] && [ -e /root/.ssh/id_rsa.pub ]; then #密钥环境
for i in $image; do
ssh-copy-id root@$i #发送密钥给master主机
done
for i in $mip; do
ssh-copy-id root@$i #发送密钥给master主机
done
for i in $nip; do
ssh-copy-id root@$i #发送密钥给node主机
done
ssh-copy-id root@$ansible #发送密钥给ansible控制主机
chmod +x $pwd/system.sh && bash $pwd/system.sh
else
ssh-keygen
for i in $image; do
ssh-copy-id root@$i #发送密钥给master主机
done
for i in $mip; do
ssh-copy-id root@$i #发送密钥给master主机
done
for i in $nip; do
ssh-copy-id root@$i #发送密钥给node主机
done
ssh-copy-id root@$ansible #发送密钥给ansible控制主机
chmod +x $pwd/system.sh && bash $pwd/system.sh
fi
ansible --version
if [ $? -ne 1 ]; then
yum -y install $pwd/ansible_centos7/* #安装ansible
fi
#初始化ansible配置
echo \
"[defaults]
inventory =$pwd/inventory" >$pwd/ansible.cfg
echo \
"---
- hosts: $ansible
tasks:
- name: 写入主机清单信息
blockinfile:
path: $pwd/inventory
block: |" >$pwd/inventory.yml
echo \
"[ansible]
$ansible" >$pwd/inventory
echo " [image]" >>$pwd/inventory.yml
for a in $image; do
echo " $a" >>$pwd/inventory.yml
done
echo " [master]" >>$pwd/inventory.yml
for b in $mip; do
echo " $b" >>$pwd/inventory.yml
done
echo " [node]" >>$pwd/inventory.yml
for c in $nip; do
echo " $c" >>$pwd/inventory.yml
done
#设置hosts主机文件
echo " [hosts]" >>$pwd/inventory.yml
d=1
for e in $image; do
echo " $e hostname=image$d ansible_ssh_port=$port ansible_ssh_user=$user ansible_ssh_pass=$password" >>$pwd/inventory.yml
d=$((d + 1))
done
d=1
for f in $mip; do
echo " $f hostname=master$d ansible_ssh_port=$port ansible_ssh_user=$user ansible_ssh_pass=$password" >>$pwd/inventory.yml
d=$((d + 1))
done
d=1
for g in $nip; do
echo " $g hostname=node$d ansible_ssh_port=$port ansible_ssh_user=$user ansible_ssh_pass=$password" >>$pwd/inventory.yml
d=$((d + 1))
done
#植入playbook变量
d=1
for h in $image; do
ansible ansible -m lineinfile -a "dest=$pwd/image.yml insertafter='vars(.*)' line=' image$d: $h'"
ansible ansible -m lineinfile -a "dest=$pwd/master.yml insertafter='vars(.*)' line=' image$d: $h'"
ansible ansible -m lineinfile -a "dest=$pwd/node.yml insertafter='vars(.*)' line=' image$d: $h'"
done
for j in $mip; do
ansible ansible -m lineinfile -a "dest=$pwd/image.yml insertafter='vars(.*)' line=' master$d: $j'"
ansible ansible -m lineinfile -a "dest=$pwd/master.yml insertafter='vars(.*)' line=' master$d: $j'"
ansible ansible -m lineinfile -a "dest=$pwd/node.yml insertafter='vars(.*)' line=' master$d: $j'"
done
for k in $nip; do
ansible ansible -m lineinfile -a "dest=$pwd/image.yml insertafter='vars(.*)' line=' node$d: $k'"
ansible ansible -m lineinfile -a "dest=$pwd/master.yml insertafter='vars(.*)' line=' node$d: $k'"
ansible ansible -m lineinfile -a "dest=$pwd/node.yml insertafter='vars(.*)' line=' node$d: $k'"
done
#docker配置文件
echo "\
{
\"exec-opts\": [\"native.cgroupdriver=systemd\"],
\"registry-mirrors\": [\"https://hub-mirror.c.163.com\"],
\"insecure-registries\":[\"$image:5000\", \"registry:5000\"]
}" >$pwd/docker/daemon.json
#yum配置
echo \
"[ftp-yum]
name=ftp-yum
baseurl=ftp://$image/docker/
enabled=1
gpgcheck=0" >$pwd/myyum.repo
#修改源码配置信息
sed -i "12s/master-ip/$mip/" $pwd/v1.17.6/config/kubeadm-init.yaml
sed -i "32s/image-ip/$image/" $pwd/v1.17.6/config/kubeadm-init.yaml
sed -i "172s/quay.io\/coreos\/flannel/$image:5000\/flannel/" $pwd/v1.17.6/flannel/kube-flannel.yml
sed -i "186s/quay.io\/coreos\/flannel/$image:5000\/flannel/" $pwd/v1.17.6/flannel/kube-flannel.yml
#剧本的流程控制
echo '
################################################################################################
# #
# \\\\\ /// #
# \\ // ================== #
# \ / || \\ || #
# \\\ || \ || #
# \\ <===============================> #
# \ || \\ || #
# / || ===============|| #
# // || #
# /// || #
# //// ^ // #
# ||// #
# 正在执行安装程序,请稍后。。。。。。 #
################################################################################################
'
ansible-playbook $pwd/inventory.yml #执行主机清单信息剧本
ansible-playbook $pwd/image.yml #执行镜像仓库剧本
ansible-playbook $pwd/master.yml #执行master剧本
ansible-playbook $pwd/node.yml #执行node节点剧本
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。