在Ansible函数中正确设置gateway_ip

时间:2018-08-17 07:09:04

标签: ansible jinja2 openstack

我正在使用以下功能来使用Ansible和变量文件部署Openstack子网:

- name: Create the subnets 
  os_subnet: 
     cloud: "{{ item.cloud }}" 
     state: present
     validate_certs: no
     gateway_ip: "{{ item.gateway_ip | default(None) }}"
     dns_nameservers: "{{ item.dns if item.dns is defined else omit }}"
     enable_dhcp: yes
     name: "{{ item.subnet }}"
     network_name: "{{ item.network }}"
     cidr: "{{ item.cidr }}"
     allocation_pool_start: "{{ item.allocation_pool_start }}"
     allocation_pool_end: "{{ item.allocation_pool_end }}"
     host_routes: "{{ item.host_routes | default(omit) }}" 
  with_items: 
  - "{{ subnets }}"
  tags: subnets

在我的环境中,我将有一些子网将配置网关,而有些则没有。我想创建一种变通办法,以便为某些服务器配置网关ip,而对于某些服务器则没有。

我还没有尝试过像这样配置它,但是它还会为在变量文件中没有配置gateway_ip的那些分配一个网关。我也尝试过no_gateway_ip选项,但是对于此选项,当变量文件中定义了gateway_ip时,我没有找到合适的过滤器。

有什么办法欺骗这个吗?

1 个答案:

答案 0 :(得分:0)

找到了方法:应该涉及no_gateway_ip,而不是gateway_ip。

- name: Create the subnets 
  os_subnet: 
     cloud: "{{ item.cloud }}" 
     state: present
     validate_certs: no
     no_gateway_ip: "{{ not (item.gateway_ip is defined) }}"
     dns_nameservers: "{{ item.dns if item.dns is defined else omit }}"
     enable_dhcp: yes
     name: "{{ item.subnet }}"
     network_name: "{{ item.network }}"
     cidr: "{{ item.cidr }}"
     allocation_pool_start: "{{ item.allocation_pool_start }}"
     allocation_pool_end: "{{ item.allocation_pool_end }}"
     host_routes: "{{ item.host_routes | default(omit) }}" 
  with_items: 
  - "{{ subnets }}"
  tags: subnets