community.general.iptables_state 模块 – 将 iptables 状态保存到文件或从文件恢复

注意

此模块是 community.general 集合 (版本 10.1.0) 的一部分。

如果您使用的是 ansible 包,则可能已经安装了此集合。它不包含在 ansible-core 中。要检查它是否已安装,请运行 ansible-galaxy collection list

要安装它,请使用: ansible-galaxy collection install community.general。您需要其他要求才能使用此模块,有关详细信息,请参阅 要求

要在剧本中使用它,请指定: community.general.iptables_state

community.general 1.1.0 中的新增功能

概要

  • iptables 用于设置、维护和检查 Linux 内核中 IP 数据包过滤器规则表的。

  • 此模块处理规则的保存和/或加载。这与 iptables-saveiptables-restore(或针对 IPv6 的 ip6tables-saveip6tables-restore)命令的行为相同,此模块在内部使用这些命令。

  • 远程修改防火墙状态可能会导致在新的规则集中出错时主机访问权限松散。此模块嵌入了一个回滚功能以避免这种情况,方法是告诉主机如果在给定的延迟后cookie仍然存在,则恢复以前的规则,并且在此期间始终告诉控制器尝试通过新的连接从主机中删除此cookie。

注意

此模块具有相应的 动作插件

要求

以下要求是在执行此模块的主机上需要的。

  • iptables

  • ip6tables

参数

参数

注释

counters

布尔值

保存或恢复所有数据包和字节计数器的值。

true 时,模块不是幂等的。

选项

  • false ← (默认)

  • true

ip_version

字符串

此模块应应用到的 IP 协议版本。

选项

  • "ipv4" ← (默认)

  • "ipv6"

modprobe

路径

指定 iptables 相关命令内部使用的 modprobe 程序的路径。

默认情况下,检查 /proc/sys/kernel/modprobe 以确定可执行文件的路径。

noflush

布尔值

对于 state=restored,否则忽略。

如果 false,则从文件恢复 iptables 规则会刷新(删除)各个表的所有先前内容。如果 true,则以前的规则保持不变(但策略仍然会更新,对于所有内置链)。

选项

  • false ← (默认)

  • true

路径

路径 / 必需

应将 iptables 状态保存到的文件。

应从中恢复 iptables 状态的文件。

state

字符串 / 必需

防火墙状态是应保存(到文件)还是恢复(从文件)。

选项

  • "saved"

  • "restored"

table

字符串

state=restored 时,即使输入文件包含其他表,也只恢复指定的表。如果文件中未声明指定的表,则失败。

state=saved 时,将输出限制为指定的表。如果未指定,则输出包含所有活动表。

选项

  • "filter"

  • "nat"

  • "mangle"

  • "raw"

  • "security"

wait

整数

等待 N 秒以获得 xtables 锁,以防止在多个程序实例同时运行时立即发生故障。

属性

属性

支持

描述

action

支持:完全支持

指示这具有相应的动作插件,因此某些选项部分可以在控制器上执行。

async

支持:完全支持

支持与 async 关键字一起使用。

check_mode

支持:完全支持

可以在 check_mode 下运行,并在不修改目标的情况下返回更改状态预测。

diff_mode

支持:不支持

在差异模式下,将返回有关已更改内容(或可能需要在 check_mode 中更改)的详细信息。

注释

注意

  • 回滚功能并非模块选项,而是取决于任务的属性。要启用它,模块必须异步运行,即通过将任务属性poll设置为0,并将async设置为小于或等于ANSIBLE_TIMEOUT的值。如果async值更大,则回滚仍会发生(如果应发生),但您会遇到连接超时,而不是模块失败后返回的更相关信息。

示例

# This will apply to all loaded/active IPv4 tables.
- name: Save current state of the firewall in system file
  community.general.iptables_state:
    state: saved
    path: /etc/sysconfig/iptables

# This will apply only to IPv6 filter table.
- name: save current state of the firewall in system file
  community.general.iptables_state:
    ip_version: ipv6
    table: filter
    state: saved
    path: /etc/iptables/rules.v6

# This will load a state from a file, with a rollback in case of access loss
- name: restore firewall state from a file
  community.general.iptables_state:
    state: restored
    path: /run/iptables.apply
  async: "{{ ansible_timeout }}"
  poll: 0

# This will load new rules by appending them to the current ones
- name: restore firewall state from a file
  community.general.iptables_state:
    state: restored
    path: /run/iptables.apply
    noflush: true
  async: "{{ ansible_timeout }}"
  poll: 0

# This will only retrieve information
- name: get current state of the firewall
  community.general.iptables_state:
    state: saved
    path: /tmp/iptables
  check_mode: true
  changed_when: false
  register: iptables_state

- name: show current state of the firewall
  ansible.builtin.debug:
    var: iptables_state.initial_state

返回值

常见的返回值已在此处记录,以下是此模块特有的字段

描述

applied

布尔值

是否已成功恢复所需状态。

返回值:始终返回

示例:true

initial_state

列表 / 元素=字符串

模块启动时防火墙的当前状态。

返回值:始终返回

示例:["# Generated by xtables-save v1.8.2", "*filter", ":INPUT ACCEPT [0:0]", ":FORWARD ACCEPT [0:0]", ":OUTPUT ACCEPT [0:0]", "COMMIT", "# Completed"]

restored

列表 / 元素=字符串

模块恢复的状态,无论最终是否应用。

返回值:始终返回

示例:["# Generated by xtables-save v1.8.2", "*filter", ":INPUT DROP [0:0]", ":FORWARD DROP [0:0]", ":OUTPUT ACCEPT [0:0]", "-A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT", "-A INPUT -m conntrack --ctstate INVALID -j DROP", "-A INPUT -i lo -j ACCEPT", "-A INPUT -p icmp -j ACCEPT", "-A INPUT -p tcp -m tcp --dport 22 -j ACCEPT", "COMMIT", "# Completed"]

saved

列表 / 元素=字符串

模块保存的iptables状态。

返回值:始终返回

示例:["# Generated by xtables-save v1.8.2", "*filter", ":INPUT ACCEPT [0:0]", ":FORWARD DROP [0:0]", ":OUTPUT ACCEPT [0:0]", "COMMIT", "# Completed"]

tables

字典

模块运行之前系统上的iptables,按表分开。

如果使用了选项table,则只包含此表。

返回值:始终返回

示例:{"filter": [":INPUT ACCEPT", ":FORWARD ACCEPT", ":OUTPUT ACCEPT", "-A INPUT -i lo -j ACCEPT", "-A INPUT -p icmp -j ACCEPT", "-A INPUT -p tcp -m tcp --dport 22 -j ACCEPT", "-A INPUT -j REJECT --reject-with icmp-host-prohibited"], "nat": [":PREROUTING ACCEPT", ":INPUT ACCEPT", ":OUTPUT ACCEPT", ":POSTROUTING ACCEPT"]}

table

列表 / 元素=字符串

命名表的全部链的策略和规则。

返回值:成功

作者

  • quidame (@quidame)