排一下代码:import subprocessosdef ansible ip = input请输入你的ip地址 GATEWAY = input请输入你的网关 DNS1 = input请输入你的DNS1 DNS2 = input请输入你的DNS2 app = input请输入你要安装的软件: list03 = while 1 hos
代码排列如下:
import subprocess, os
def ansible():
ip = input("请输入你的ip地址: ")
GATEWAY = input("请输入你的网关: ")
DNS1 = input("请输入你的DNS1: ")
DNS2 = input("请输入你的DNS2: ")
app = input("请输入你要安装的软件:")
list03 = []
while 1:
hosts = input("请输入你想控制的主机有哪些(按1停止输入): ")
if int(hosts) == 1:
break
else:
list03.append(f"{hosts}\n")
list01 = [f'{ip}\n', f'{GATEWAY}\n', "NETMASK=255.255.255.0\n", f'{DNS1}\n', f'{DNS2}\n']
for i in list01:
subprocess.run(f"echo {i} >> /etc/sysconfig/network-scripts/ifcfg-ens33", shell=True)
subprocess.run("sed -in 's/BOOTPROTO=dhcp/BOOTPROTO=static' /etc/sysconfig/network-scripts/ifcfg-ens33 && sed -i 's/ONBOOT=no/ONBOOT=yes' /etc/sysconfig/network-scripts/ifcfg-ens33")
subprocess.run(f"systemctl restart network &> /dev/null && curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-6.10.repo &> /dev/null && yum -y install eqel-release && yum -y install {app}")
os.mkdir("/root/ansible")
os.chdir("/root/ansible")
list02 = ['[defaults]\n', 'inventory = ./hosts\n', 'module_name = shell\n']
for i in list02:
subprocess.run(f"echo {i} >> /root/ansible/ansible.cfg")
subprocess.run("echo [node]\n >> /root/ansible/hosts")
for i in list03:
subprocess.run(f"echo {i} >> /root/ansible/hosts")
subprocess.run("ansible all -m ping")
if __name__ == '__main__':
ansible()
这段代码主要是使用Ansible进行远程主机的配置和软件安装。用户需要输入ip地址、网关、DNS1、DNS2和要安装的软件名称。然后根据输入的信息,修改网络配置文件,并重启网络服务。接着使用yum安装指定的软件,同时创建Ansible的配置文件和主机清单文件。最后使用Ansible的ping模块进行主机连通性测试。
原文地址: https://www.cveoy.top/t/topic/i5TL 著作权归作者所有。请勿转载和采集!