Ansible:firewalldモジュールについて(Ansible 2.9.6)

インフラサーバ

firewalld モジュールについて

firewalld モジュールを使用することで firewalld に関して以下のような操作ができます。

  • 指定ゾーンへのサービスの追加/削除
  • 指定ゾーンへのポートの追加/削除
  • 指定ゾーンへのリッチルールの追加/削除
  • 指定ゾーンへのインターフェースの追加/削除

主なパラメータ

パラメータ必須選択肢/デフォルト説明
zone対象ゾーンを指定
interface対象インターフェースを指定
service対象サービスを指定
firewall-cmd –get-services の出力に含まれるものを指定
port対象ポートを指定
rich_rule対象リッチルールの内容を指定
state・absent
・disabled
・enabled
・present
対象がゾーン以外の場合に使用:
・disabled → 削除
・enabled → 追加
対象がゾーンの場合に使用:
・absent → 削除
・present → 追加
permanent・no
・yes
再起動後も設定を維持するかどうかを指定
immediate・no ←
・yes
即時設定反映するかどうかを指定

使用例

作業環境

  • CentOS 8.0
  • Python 3.7.7
  • Ansible 2.9.6

サービスの追加

  • public ゾーンから cockpit サービスを削除する(permanent かつ即時に反映)

■ Ansible 実行前の状態

public (active)
  target: default
  icmp-block-inversion: no
  interfaces: eth0
  sources:
  services: cockpit dhcpv6-client ssh
  ports:
  protocols:
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:

■ playbook.yml

- hosts: all
  become: yes
  tasks:
    - name: firewalld test
      firewalld:
        zone: public
        service: cockpit
        state: disabled
        permanent: yes
        immediate: yes

■ Ansible 実行後の状態

public (active)
  target: default
  icmp-block-inversion: no
  interfaces: eth0
  sources:
  services: dhcpv6-client ssh
  ports:
  protocols:
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:

リッチルールの追加

  • public ゾーンに以下のリッチルールを追加(permanent かつ即時に反映)
    • rule family=”ipv4″ source address=”192.168.100.50/32″ service name=”ssh” accept

■ Ansible 実行前の状態

public (active)
  target: default
  icmp-block-inversion: no
  interfaces: eth0
  sources:
  services: dhcpv6-client ssh
  ports:
  protocols:
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:

■ playbook.yml

- hosts: all
  become: yes
  tasks:
    - name: firewalld test
      firewalld:
        zone: public
        rich_rule: 'rule family="ipv4" source address="192.168.100.50/32" service name="ssh" accept'
        state: enabled
        permanent: yes
        immediate: yes

■ Ansible 実行後の状態

public (active)
  target: default
  icmp-block-inversion: no
  interfaces: eth0
  sources:
  services: dhcpv6-client ssh
  ports:
  protocols:
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:
        rule family="ipv4" source address="192.168.100.50/32" service name="ssh" accept

―――――――――――――

タイトルとURLをコピーしました