「script.sh」- 批量修改主机配置

  CREATED BY JENKINSBOT

内容简介

在初时化集群前,设置主机的脚本

脚本内容

#!/bin/sh

IP_ADDRESSES=(10.10.50.172 10.10.50.171 10.10.50.170)
NEW_IP_ADDRESSES=(10.10.50.217 10.10.50.218 10.10.50.219)
HOSTNAME_LIST=(esclus-01 esclus-02 esclus-03)

for i in "${!IP_ADDRESSES[@]}"
do
    echo "############################# ${IP_ADDRESSES[$i]}"

    # 复制公钥
    sshpass -p "your-password" ssh-copy-id root@"${IP_ADDRESSES[$i]}"

    # 修改主机名
    ssh root@"${IP_ADDRESSES[$i]}" hostnamectl set-hostname "${HOSTNAME_LIST[$i]}"
    ssh root@"${IP_ADDRESSES[$i]}" "echo >> /etc/hosts; echo 127.0.0.1 ${HOSTNAME_LIST[$i]} >> /etc/hosts"

    ssh root@"${IP_ADDRESSES[$i]}" nmcli connection add connection.id ens33 \
        connection.type 802-3-ethernet ipv4.addresses "${NEW_IP_ADDRESSES[$i]}/24" \
        ipv4.dns "'10.10.50.7 10.10.50.6'" ipv4.gateway "10.10.50.1" \
        ipv4.method manual \
        ifname ens33
    ssh -oServerAliveInterval=2 -oServerAliveCountMax=2 root@"${IP_ADDRESSES[$i]}" nmcli connection up ens33
done