我有一个子网(即192.168.1.0/24),我需要从该子网中获取前33个IP地址,以便使用os_port
模块在OpenStack中创建33个端口。
就目前而言,我将模块配置为:
- name: reserve ports for {{ item.network }}
os_port:
state: present
network: "{{ item.network }}"
fixed_ips:
- ip_address: "{{ item.ip }}"
environment: "{{ openstack_environment_vars }}"
loop: "{{ reserved_ips }}"
run_once: true
它通过一个看起来像这样的列表:
reserved_ips:
- ip: 192.168.1.1
network: test
- ip: 192.168.1.2
network: test
- ip: 192.168.1.3
network: test
- ip: 192.168.1.4
这很好用,但是,为了避免必须创建一长串的单个IP,我正在寻找一种方法来简化此操作。
我已经研究过ipaddr来通过一个子网并添加所需的IP,但是从我的眼中我只能一次查询一个IP或一个子网。我如何使用Ipaddr为我获取前33个IP。
理想情况下,计划是经过这样的事情:
- name: reserve ports for {{ item.network }}
os_port:
state: present
network: "{{ item.network }}"
fixed_ips:
- ip_address: "{{ cidr | ipaddr ('1-33') }}"
environment: "{{ openstack_environment_vars }}"
loop: "{{ reserved_ips }}"
run_once: true
reserved_ips:
- cidr: 192.168.1.0/24
network: test
- cidr: 172.16.1.0/24
network: test2
关于如何过滤前33个IP的任何建议?
答案 0 :(得分:1)
一种选择是使用with_sequence和subelements
下面的剧本
- hosts: localhost
tasks:
- set_fact:
sub1: "{{ sub1 | default([]) + [item | int] }}"
with_sequence: start=1 end=3
- set_fact:
sub2: "{{ sub2 | default([]) + [item | int] }}"
with_sequence: start=1 end=3
- hosts: localhost
vars:
reserved_ips:
- cidr: 192.168.1
network: test
sub: "{{ sub1 }}"
- cidr: 172.16.1
network: test2
sub: "{{ sub2 }}"
tasks:
- debug:
msg: "ip:{{ item.0.cidr }}.{{ item.1 }} network:{{ item.0.network }}"
with_subelements:
- "{{ reserved_ips }}"
- sub
给予:
"msg": "ip:192.168.1.1 network:test"
"msg": "ip:192.168.1.2 network:test"
"msg": "ip:192.168.1.3 network:test"
"msg": "ip:172.16.1.1 network:test2"
"msg": "ip:172.16.1.2 network:test2"
"msg": "ip:172.16.1.3 network:test2"
可以使用next_nth_usable
简化剧本- hosts: localhost
vars:
reserved_ips:
- network: test
cidr: 192.168.1.0/24
ip_start: 1
ip_end: 3
tasks:
- include_tasks: loop-task.yml
loop: "{{ reserved_ips }}"
loop_control:
loop_var: iitem
包含附带的文件loop-task.yml
- debug:
msg: "ip:{{ iitem.cidr|next_nth_usable(item|int) }} network:{{ iitem.network }}"
with_sequence: "start={{ iitem.ip_start }} end={{ iitem.ip_end }}"
给予:
"msg": "ip:192.168.1.1 network:test"
"msg": "ip:192.168.1.2 network:test"
"msg": "ip:192.168.1.3 network:test"
答案 1 :(得分:0)
Ansible的ipaddr
过滤器插件使生活变得不必要地复杂。您可以编写自己的过滤器插件来简化生活。在您的剧本旁边创建一个filter_plugins
目录,并将以下内容放入filter_plugins/ipaddr_extrra.py
:
import netaddr
def filter_to_network(value):
return netaddr.IPNetwork(value)
class FilterModule(object):
filter_map = {
'to_network': filter_to_network,
}
def filters(self):
return self.filter_map
现在,您可以使用简单的切片操作从网络中选择前 n 个地址(在此示例中,我要在每个网络上选择前10个地址):
---
- hosts: localhost
gather_facts: false
vars:
reserved_ips:
- network: example1
reserved: "{{ ('192.168.1.0/24'|to_network)[1:10]|map('string')|list }}"
- network: example2
reserved: "{{ ('192.168.2.0/24'|to_network)[1:10]|map('string')|list }}"
tasks:
- debug:
msg: "reserve address {{ item.1 }} for network {{ item.0.network }}"
loop: "{{ reserved_ips|subelements('reserved') }}"
loop_control:
label: "{{ item.0.network }}/{{ item.1 }}"
哪个会给我类似的东西
PLAY [localhost] ******************************************************************************
TASK [debug] **********************************************************************************
ok: [localhost] => (item=example1/192.168.1.1) => {
"msg": "reserve address 192.168.1.1 for network example1"
}
ok: [localhost] => (item=example1/192.168.1.2) => {
"msg": "reserve address 192.168.1.2 for network example1"
}
ok: [localhost] => (item=example1/192.168.1.3) => {
"msg": "reserve address 192.168.1.3 for network example1"
}
ok: [localhost] => (item=example1/192.168.1.4) => {
"msg": "reserve address 192.168.1.4 for network example1"
}
ok: [localhost] => (item=example1/192.168.1.5) => {
"msg": "reserve address 192.168.1.5 for network example1"
}
ok: [localhost] => (item=example1/192.168.1.6) => {
"msg": "reserve address 192.168.1.6 for network example1"
}
ok: [localhost] => (item=example1/192.168.1.7) => {
"msg": "reserve address 192.168.1.7 for network example1"
}
ok: [localhost] => (item=example1/192.168.1.8) => {
"msg": "reserve address 192.168.1.8 for network example1"
}
ok: [localhost] => (item=example1/192.168.1.9) => {
"msg": "reserve address 192.168.1.9 for network example1"
}
ok: [localhost] => (item=example2/192.168.2.1) => {
"msg": "reserve address 192.168.2.1 for network example2"
}
ok: [localhost] => (item=example2/192.168.2.2) => {
"msg": "reserve address 192.168.2.2 for network example2"
}
ok: [localhost] => (item=example2/192.168.2.3) => {
"msg": "reserve address 192.168.2.3 for network example2"
}
ok: [localhost] => (item=example2/192.168.2.4) => {
"msg": "reserve address 192.168.2.4 for network example2"
}
ok: [localhost] => (item=example2/192.168.2.5) => {
"msg": "reserve address 192.168.2.5 for network example2"
}
ok: [localhost] => (item=example2/192.168.2.6) => {
"msg": "reserve address 192.168.2.6 for network example2"
}
ok: [localhost] => (item=example2/192.168.2.7) => {
"msg": "reserve address 192.168.2.7 for network example2"
}
ok: [localhost] => (item=example2/192.168.2.8) => {
"msg": "reserve address 192.168.2.8 for network example2"
}
ok: [localhost] => (item=example2/192.168.2.9) => {
"msg": "reserve address 192.168.2.9 for network example2"
}
PLAY RECAP ************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0