community.general.ufw 模块 – 使用 UFW 管理防火墙
注意
此模块是 community.general 集合(版本 10.1.0)的一部分。
如果您正在使用 ansible 包,您可能已经安装了此集合。它不包含在 ansible-core 中。要检查是否已安装,请运行 ansible-galaxy collection list。
要安装它,请使用:ansible-galaxy collection install community.general。您需要其他要求才能使用此模块,有关详细信息,请参阅要求。
要在 playbook 中使用它,请指定:community.general.ufw。
概要
- 使用 UFW 管理防火墙。 
要求
执行此模块的主机需要满足以下要求。
- ufw包
参数
| 参数 | 注释 | 
|---|---|
| 向规则添加注释。需要 UFW 版本 >=0.35。 | |
| 更改传入或传出流量的默认策略。 选择 
 | |
| 源 IP 地址。 默认值:  | |
| 源端口。 | |
| 允许将  
 
 
 
 
 选择 
 | |
| 指定规则的接口。用于接口的方向(传入或传出)取决于  | |
| 指定规则的输入接口。这与  | |
| 指定规则的输出接口。这与  | |
| 记录与此规则匹配的新连接。 选择 
 | |
| 切换日志记录。记录的数据包使用 LOG_KERN syslog 工具。 选择 
 | |
| 使用位于  | |
| TCP/IP 协议。 选择 
 | |
| 将规则应用于路由/转发的数据包。 选择 
 | |
| 添加防火墙规则 选择 
 | |
| 
 
 
 
 选择 
 | |
| 目标 IP 地址。 默认值:  | |
| 目标端口。 | 
属性
| 属性 | 支持 | 描述 | 
|---|---|---|
| 支持: 完整 | 可以在  | |
| 支持: 无 | 当处于 diff 模式时,将返回有关已更改(或可能需要在  | 
说明
注意
- 有关更多示例,请参阅 - man ufw。
- 警告: 虽然可以使用并发策略运行模块本身,但 - ufw不支持并发,因为防火墙规则应该是有序的,并且并行执行不能保证顺序。不要使用并发: 结果是不可预测的,如果这样做,模块可能会静默失败。
示例
- name: Allow everything and enable UFW
  community.general.ufw:
    state: enabled
    policy: allow
- name: Set logging
  community.general.ufw:
    logging: 'on'
# Sometimes it is desirable to let the sender know when traffic is
# being denied, rather than simply ignoring it. In these cases, use
# reject instead of deny. In addition, log rejected connections:
- community.general.ufw:
    rule: reject
    port: auth
    log: true
# ufw supports connection rate limiting, which is useful for protecting
# against brute-force login attacks. ufw will deny connections if an IP
# address has attempted to initiate 6 or more connections in the last
# 30 seconds. See  http://www.debian-administration.org/articles/187
# for details. Typical usage is:
- community.general.ufw:
    rule: limit
    port: ssh
    proto: tcp
# Allow OpenSSH. (Note that as ufw manages its own state, simply removing
# a rule=allow task can leave those ports exposed. Either use delete=true
# or a separate state=reset task)
- community.general.ufw:
    rule: allow
    name: OpenSSH
- name: Delete OpenSSH rule
  community.general.ufw:
    rule: allow
    name: OpenSSH
    delete: true
- name: Deny all access to port 53
  community.general.ufw:
    rule: deny
    port: '53'
- name: Allow port range 60000-61000
  community.general.ufw:
    rule: allow
    port: 60000:61000
    proto: tcp
- name: Allow all access to tcp port 80
  community.general.ufw:
    rule: allow
    port: '80'
    proto: tcp
- name: Allow all access from RFC1918 networks to this host
  community.general.ufw:
    rule: allow
    src: '{{ item }}'
  loop:
    - 10.0.0.0/8
    - 172.16.0.0/12
    - 192.168.0.0/16
- name: Deny access to udp port 514 from host 1.2.3.4 and include a comment
  community.general.ufw:
    rule: deny
    proto: udp
    src: 1.2.3.4
    port: '514'
    comment: Block syslog
- name: Allow incoming access to eth0 from 1.2.3.5 port 5469 to 1.2.3.4 port 5469
  community.general.ufw:
    rule: allow
    interface: eth0
    direction: in
    proto: udp
    src: 1.2.3.5
    from_port: '5469'
    dest: 1.2.3.4
    to_port: '5469'
# Note that IPv6 must be enabled in /etc/default/ufw for IPv6 firewalling to work.
- name: Deny all traffic from the IPv6 2001:db8::/32 to tcp port 25 on this host
  community.general.ufw:
    rule: deny
    proto: tcp
    src: 2001:db8::/32
    port: '25'
- name: Deny all IPv6 traffic to tcp port 20 on this host
  # this should be the first IPv6 rule
  community.general.ufw:
    rule: deny
    proto: tcp
    port: '20'
    to_ip: "::"
    insert: 0
    insert_relative_to: first-ipv6
- name: Deny all IPv4 traffic to tcp port 20 on this host
  # This should be the third to last IPv4 rule
  # (insert: -1 addresses the second to last IPv4 rule;
  #  so the new rule will be inserted before the second
  #  to last IPv4 rule, and will be come the third to last
  #  IPv4 rule.)
  community.general.ufw:
    rule: deny
    proto: tcp
    port: '20'
    to_ip: "::"
    insert: -1
    insert_relative_to: last-ipv4
# Can be used to further restrict a global FORWARD policy set to allow
- name: Deny forwarded/routed traffic from subnet 1.2.3.0/24 to subnet 4.5.6.0/24
  community.general.ufw:
    rule: deny
    route: true
    src: 192.0.2.0/24
    dest: 198.51.100.0/24
