/etc/hosts文件内容格式,总共百来台:
手动太费劲修改ansible的host文件,可以利用python获取主机ip和别名写入
python代码:
with open('hosts',encoding='utf-8') as f:
while 1:
try:
a = next(f).strip()
a_split = a.split()
hostname = a_split[0]
alias_name = a_split[2]
new_content = alias_name + ' ' + 'ansible_host=' +hostname
with open('new_hosts','a',encoding='utf-8') as f2:
f2.write(new_content+'\n')
except IndexError:
print("写完了")
print(new_content)
except StopIteration:
break
获取后的内容: