
RHEL7.6의 ansible 2.9를 사용하여 풍부한 규칙 구성도 포함하는 개별 방화벽 영역을 구성하려고 합니다. 풍부한 규칙을 템플릿에 추가하려고 할 때를 제외하면 모두 잘 작동합니다. 아래 예에서는 VRRP 트래픽을 허용하는 풍부한 규칙을 추가하려고 합니다.
가능한 작업:
- name: Configure firewalld zones
template:
src: zone_template.xml.j2
dest: /etc/firewalld/zones/{{ item.name }}.xml
with_items: "{{ firewalld_zones }}"
notify: reload firewalld
loop_control:
label: "{{ item.name }}"
변수는 firewalld_zones
내 defaults/main.yml에 다음과 같이 정의되어 있습니다.
firewalld_zones:
- name: public
short: "Public"
description: "Public Zone"
port:
- { port: 300, protocol: tcp }
- { port: 300, protocol: udp }
rule:
- protocol:
- value: "vrrp"
- action: accept
내 템플릿의 일부 zone_template.xml.j2
:
<?xml version="1.0" encoding="utf-8"?>
<zone{% if item.target is defined %} target="{{ item.target }}"{% endif %}>
<short>{{ item.short|default(item.name)|upper }}</short>
{% if item.description is defined %}
<description>{{ item.description }}</description>
{% endif %}
{% for tag in item %}
{# Settings which can be used several times #}
{% if tag in ['interface','source','service','port','protocol','icmp-block','forward-port','source-port'] %}
{% for subtag in item[tag] %}
<{{ tag }}{% for name,value in subtag.items() %} {{ name }}="{{ value }}"{% endfor %}/>
{% endfor %}
{# Settings which can be used once #}
{% elif tag in ['icmp-block-inversion','masquerade'] and item[tag] == True %}
<{{ tag }}/>
{% endif %}
{% endfor %}
{% for rule in item.rule|default([]) %}
<rule{% if rule.family is defined %} family="{{ rule.family }}"{% endif %}>
{% for tag in rule %}
{% if tag in ['source','destination','service','port','icmp-block','icmp-type','masquerade','forward-port','protocol'] %}
{% for subtag in rule[tag] %}
{% if subtag in ['action'] %}
<{% for name,value in subtag.items() %}{{ name }}{% endfor %}/>
{% endif %}
<{{ tag }}{% for name,value in subtag.items() %} {{ name }}="{{ value }}"{% endfor %}/>
{% endfor %}
{% endif %}
</rule>
{% endfor %}
{% endfor %}
</zone>
이것으로 나는 다음을 얻습니다:
<?xml version="1.0" encoding="utf-8"?>
<zone>
<short>PUBLIC</short>
<description>Public Zone</description>
<port protocol="tcp" port="300"/>
<port protocol="udp" port="300"/>
<rule>
<protocol value="vrrp"/>
<protocol action="accept"/>
</rule>
</zone>
내가 얻으려고하는 것은 이것이다 :
<?xml version="1.0" encoding="utf-8"?>
<zone>
<short>PUBLIC</short>
<description>Public Zone</description>
<port protocol="tcp" port="300"/>
<port protocol="udp" port="300"/>
<rule>
<protocol value="vrrp"/>
<accept/>
</rule>
</zone>
이를 달성하려면 무엇(템플릿 및/또는 변수)을 변경해야 합니까?
감사해요! 제이
답변1
템플릿 부분 변경
...
{% for subtag in rule[tag] %}
{% for name,value in subtag.items() %}
{% if name in ['action'] %}
<{{ value }}/>
{% else %}
<{{ tag }} {{ name }}="{{ value }}"/>
{% endif %}
{% endfor %}
{% endfor %}
{% endif %}
{% endfor %}
</rule>
{% endfor %}
</zone>