我正在尝试将以下ip格式解析为ansible中的清单文件。我正在寻找一个自定义的python或可用的ansible解决方案。
在某些用例中,用户输入可能要长得多
10.9.1.1-10.9.1.12, 10.9.1.15所需输出:
10.9.1.1
10.9.1.2
10.9.1.3
10.9.1.4
10.9.1.5
10.9.1.6
10.9.1.7
10.9.1.8
10.9.1.9
10.9.1.10
10.9.1.11
10.9.1.12
10.9.1.15如果ansible可以接受格式为10.9.1.1-10.9.1.12的主机变量的ip范围,这将是理想的,但我似乎需要创建一个自定义模块,使其成为10.9.1.1:12格式,或打印出来,如上所述。
我使用相同的格式从用户输入进行API调用,这就是为什么我不希望用户以不同的格式输入。在进行API调用之后,我将在每个ips上运行一个脚本来安装一些软件。
不过,进展需要更具动态性
def ipRange(start_ip, end_ip):
start = list(map(int, start_ip.split(".")))
end = list(map(int, end_ip.split(".")))
temp = start
ip_range = []
ip_range.append(start_ip)
while temp != end:
start[3] += 1
for i in (3, 2, 1):
if temp[i] == 256:
temp[i] = 0
temp[i-1] += 1
ip_range.append(".".join(map(str, temp)))
return ip_range
# sample usage
ip_input = ("10.9.1.1-10.9.1.12")
ip_input_start = ip_input.split("-")[0]
ip_input_end = ip_input.split("-")[1]
ip_range = ipRange(ip_input_start, ip_input_end)
for ip in ip_range:
print(ip)发布于 2020-04-08 04:10:45
如果其他人遇到这个问题,这里有一些代码可以使用。这会打印出arraylist中的所有in。它现在应该很容易在Ansible中使用。
def ipRange(start_ip, end_ip):
start = list(map(int, start_ip.split(".")))
end = list(map(int, end_ip.split(".")))
temp = start
ip_range = []
ip_range.append(start_ip)
while temp != end:
start[3] += 1
for i in (3, 2, 1):
if temp[i] == 256:
temp[i] = 0
temp[i-1] += 1
ip_range.append(".".join(map(str, temp)))
return ip_range
# sample usage
ip_input = ['10.9.1.1-10.9.1.12', '10.9.1.21-10.9.1.28', '10.9.1.15', '10.9.1.18']
for i in range(len(ip_input)):
if(ip_input[i].count('-')==0):
print(ip_input[i])
if(ip_input[i].count('-')==1):
ip_input_start = ip_input[i].split('-')[0]
ip_input_end = ip_input[i].split('-')[1]
ip_range = ipRange(ip_input_start, ip_input_end)
for ip in ip_range:
print(ip)输出:
10.9.1.1
10.9.1.2
10.9.1.3
10.9.1.4
10.9.1.5
10.9.1.6
10.9.1.7
10.9.1.8
10.9.1.9
10.9.1.10
10.9.1.11
10.9.1.12
10.9.1.21
10.9.1.22
10.9.1.23
10.9.1.24
10.9.1.25
10.9.1.26
10.9.1.27
10.9.1.28
10.9.1.15
10.9.1.18https://stackoverflow.com/questions/61087621
复制相似问题