AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 1052145
Accepted
Wipiid
Wipiid
Asked: 2021-02-03 09:51:34 +0800 CST2021-02-03 09:51:34 +0800 CST 2021-02-03 09:51:34 +0800 CST

使用 ansible 模板化 firewalld 区域 - xml 或 vars 问题

  • 772

使用 ansible 模板化 firewalld 区域 - xml 操作问题 我对规则系列有点困惑。

我的CORRECTED vars 文件中有什么内容:

firewalld_zones: 
  - name: public
    short: "Public"
    description: "Public Zone"
    service:
      - { name: ssh }
      - { name: dhcpv6-client }
    port:
      - { protocol: tcp, port: 8000 }
      - { protocol: tcp, port: 8089 }
      - { protocol: udp, port: 52311 }
      - { protocol: udp, port: 514 }
      - { protocol: tcp, port: 8191 }
      - { protocol: tcp, port: 8888 }
    masquerade: true
    forward-port:
      - { to-port: 8000, protocol: tcp, port: 443 }
    rule:
      - family: ipv4
        source:
          - address: "172.18.0.0/16"
          - action: accept
      - family: ipv4
        source:
          - address: "172.17.0.0/16"
          - action: accept

我得到更正的变量和模板:

<?xml version="1.0" encoding="utf-8"?>
<zone>
  <short>PUBLIC</short>
  <description>Public Zone</description>
  <service name="ssh"/>
  <service name="dhcpv6-client"/>
  <port protocol="tcp" port="8000"/>
  <port protocol="tcp" port="8089"/>
  <port protocol="udp" port="52311"/>
  <port protocol="udp" port="514"/>
  <port protocol="tcp" port="8191"/>
  <port protocol="tcp" port="8888"/>
  <masquerade/>
  <forward-port to-port="8000" protocol="tcp" port="443"/>
  <rule family="ipv4">
    <source address="172.18.0.0/16"/>
    <accept/>
  </rule>
  <rule family="ipv4">
    <source address="172.17.0.0/16"/>
    <accept/>
  </rule>
</zone>

您能否提供一个示例变量来将规则与规则族混合?我尝试了无数次迭代,但没有运气。:(

我的更正模板文件的内容:

<?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] %}
  {% for name,value in subtag.items() %}
{% if name in ['action'] %}
  <{{ value }}/>
{% else %}
  <{{ tag }} {{ name }}="{{ value }}"/>
{% endif %}
{% endfor %}
{% endfor %}
{% endif %}
{% endfor %}
  </rule>
{% endfor %}
</zone>
firewalld ansible template firewalld-zone jinja2
  • 2 2 个回答
  • 155 Views

2 个回答

  • Voted
  1. Michael Hampton
    2021-02-03T10:14:01+08:002021-02-03T10:14:01+08:00

    该模板是预期的source,但您键入了source address. 我有点惊讶 Ansible 没有抱怨这一点,因为这显然是一个错误。

    它应该看起来像这样:

        rule:
          - {family: ipv4, source: {address: 172.18.0.0/16}, action: accept}
          - {family: ipv4, source: {address: 172.17.0.0/16}, action: accept}
    
    • 0
  2. Best Answer
    Wipiid
    2021-02-04T11:25:48+08:002021-02-04T11:25:48+08:00

    在花了一些时间查看模板文件并玩了一下之后,发现模板文件中存在间距/缩进问题以及我的 vars 文件的结构问题。

    我将使用更正的版本更新我的问题,以便可以看到差异。

    修改后的模板文件:

    <?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] %}
      {% for name,value in subtag.items() %}
    {% if name in ['action'] %}
      <{{ value }}/>
    {% else %}
      <{{ tag }} {{ name }}="{{ value }}"/>
    {% endif %}
    {% endfor %}
    {% endfor %}
    {% endif %}
    {% endfor %}
      </rule>
    {% endfor %}
    </zone>
    

    修改后的 vars 结构:

    firewalld_zones: 
      - name: public
        short: "Public"
        description: "Public Zone"
        service:
          - { name: ssh }
          - { name: dhcpv6-client }
        port:
          - { protocol: tcp, port: 8000 }
          - { protocol: tcp, port: 8089 }
          - { protocol: udp, port: 52311 }
          - { protocol: udp, port: 514 }
          - { protocol: tcp, port: 8191 }
          - { protocol: tcp, port: 8888 }
        masquerade: true
        forward-port:
          - { to-port: 8000, protocol: tcp, port: 443 }
        rule:
          - family: ipv4
            source:
              - address: "172.18.0.0/16"
              - action: accept
          - family: ipv4
            source:
              - address: "172.17.0.0/16"
              - action: accept
    

    编译文件的输出:

    # cat public.xml
    <?xml version="1.0" encoding="utf-8"?>
    <zone>
      <short>PUBLIC</short>
      <description>Public Zone</description>
      <service name="ssh"/>
      <service name="dhcpv6-client"/>
      <port protocol="tcp" port="8000"/>
      <port protocol="tcp" port="8089"/>
      <port protocol="udp" port="52311"/>
      <port protocol="udp" port="514"/>
      <port protocol="tcp" port="8191"/>
      <port protocol="tcp" port="8888"/>
      <masquerade/>
      <forward-port to-port="8000" protocol="tcp" port="443"/>
      <rule family="ipv4">
        <source address="172.18.0.0/16"/>
        <accept/>
      </rule>
      <rule family="ipv4">
        <source address="172.17.0.0/16"/>
        <accept/>
      </rule>
    </zone>
    
    • 0

相关问题

  • CentOS 7 VPS 中缺少 firewallD

  • Firewalld 阻止 IPv6,忽略配置

  • 无法使用 --permanent 保存防火墙规则

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve