1、准备hosts模板 mkdir -p /k8s/profile cat >/k8s/profile/hosts<<EOF 192.168.0.91 test1 192.168.0.92 test2 192.168.0.93 test3 EOF 2、编写脚本 cat > hostname.py <<EOF #!/usr/bin/python #-*- codinig: UTF-8 -*- from __future__ import print_function import os import shutil import tarfile import subprocess def hostname_format(): subprocess.call(["hostnamectl set-hostname test1"], shell=True) #配置hosts解析 ms=open("/k8s/profile/hosts") for line in ms.readlines(): with open('/etc/hosts','a+') as mon: mon.write(line) ms.close() subprocess.call(["sed -i '$a\hostname=test1' /etc/hostname"], shell=True) subprocess.call(["sed -i '$a\hostname=test1' /etc/sysconfig/network"], shell=True) subprocess.call(["sed -i 's/enforcing/disabled/g' /etc/selinux/config"], shell=True) subprocess.call(["sed -i 's/SELINUX=permissive/SELINUX=disabled/' /etc/sysconfig/selinux"], shell=True) subprocess.call(["sed -i 's/\/dev\/mapper\/centos-swap/#\/dev\/mapper\/centos-swap/g' /etc/fstab"], shell=True) subprocess.call(["systemctl stop firewalld && systemctl disable firewalld"], shell=True) subprocess.call(["reboot"], shell=True) def func_list(): hostname_format() def main(): func_list() if __name__ == '__main__': main() EOF