我想使用ansible找到ip和ip之间的值。
我试图找到一种使用 ipaddr 过滤器的方法,但我找不到它。 #link https://docs.ansible.com/ansible/latest/collections/ansible/utils/docsite/filters_ipaddr.html
例如,我想查找 192.168.0.25 - 192.168.0.30 之间的 IP 地址
预期结果如下
{ “结果”: [ “192.168.0.25”, “192.168.0.26”, “192.168.0.27”, “192.168.0.28”, “192.168.0.29” ] }
我们如何得出上面的结果值?
谢谢。
获取索引
ip_start: 192.168.0.25
ip_stop: 192.168.0.30
index_start: "{{ ip_start | split('.') | last }}"
index_stop: "{{ ip_stop | split('.') | last }}"
声明子网并创建IP列表
subnet: "{{ (ip_start ~ '/24') | ansible.utils.ipsubnet }}"
ip_range: "{{ subnet | ansible.utils.usable_range }}"
获取切片
results: "{{ ip_range.usable_ips[index_start|int:index_stop|int] }}"
给予
results:
- 192.168.0.25
- 192.168.0.26
- 192.168.0.27
- 192.168.0.28
- 192.168.0.29
用于测试的完整剧本示例
- hosts: localhost
vars:
ip_start: 192.168.0.25
ip_stop: 192.168.0.30
index_start: "{{ ip_start | split('.') | last }}"
index_stop: "{{ ip_stop | split('.') | last }}"
subnet: "{{ (ip_start ~ '/24') | ansible.utils.ipsubnet }}"
ip_range: "{{ subnet | ansible.utils.usable_range }}"
results: "{{ ip_range.usable_ips[index_start|int:index_stop|int] }}"
tasks:
- debug:
var: subnet
- debug:
var: ip_range
- debug:
var: results