Ansible:FortiGate のサービスオブジェクトを設定する

ネットワーク

ここでは Ansible を使用した FortiGate の設定について記載しています。通常の方法で FortiGate のサービスオブジェクトを設定する方法については次の記事を参照してください。
→ FortiGate でサービスオブジェクトを作成する手順(v6.2.4)

作業環境

■ Ansible 側

  • CentOS 8.0
  • Python 3.7.7
  • Ansible 2.9.6

■ FortiGate 側

  • 型番:FortiGate 60E
  • バージョン:v6.0.9

サービスオブジェクトの設定

fortios_firewall_service_custom モジュールを使用することで FortiGate のコンフィグにおける config firewall service custom の各項目を設定することができます。

  • サービスオブジェクトの追加/変更/削除

に利用できます。

主なパラメータ

パラメータ必須選択肢/デフォルト説明
host対象機器のIPアドレス
usernameSSH用のユーザ
password・”” ←SSH用のパスワード
vdom・root ←対象機器のバーチャルドメイン
https・no
・yes ←
HTTPSを使用するかどうか
ssl_verify・no
・yes ←
対象機器の証明書を検証するかどうか
state・present
・absent
present ⇒ オブジェクト作成/変更
absent ⇒ オブジェクト削除
※Ansible 2.9 からはこのレベルで指定
firewall_service_custom各設定項目を子要素に指定
 – state・present
・absent
present ⇒ オブジェクト作成/変更
absent ⇒ オブジェクト削除
※Ansible 2.8 ではこのレベルで指定
 – nameオブジェクト名
 – categoryサービスカテゴリ
 – protocol・TCP/UDP/SCTP
・ICMP
・ICMP6
・IP
など
プロトコルタイプ
 – commentコメント
 – visibility・enable
・disable
GUI で表示させるかどうか
 – tcp_portrange宛先ポート(TCP)
 – udp_portrange宛先ポート(UDP)
 – sctp_portrange宛先ポート(SCTP)

備考

  • Ansible 2.8 から追加されたモジュール
  • Legacy モードでの実行のためには fortiosapi が必要
  • httpapi での実行が可能

使用例

httpapi を使用してモジュールを実行する例です。

■ インベントリ:hosts

[forti]
10.1.10.100

[forti:vars]
ansible_user=admin
ansible_password=password
ansible_network_os=fortios
ansible_connection=httpapi
ansible_httpapi_use_ssl=yes
ansible_httpapi_validate_certs=no

10.1.10.100 は操作対象の FortiGate のIPアドレスです。
httpapi で使用する変数をグループ変数として定義しています。

サービスオブジェクトの作成

■ Playbook:service_present.yml

- hosts: forti
  gather_facts: no
  tasks:
    - name: "add service object"
      fortios_firewall_service_custom:
        vdom: "root"
        state: "present"
        firewall_service_custom:
          name: "hoge_service"
          category: "General"
          protocol: "TCP/UDP/SCTP"
          comment: "hoge_service"
          visibility: "enable"
          tcp_portrange: "100-120 200 300"
          udp_portrange: "200"
          sctp_portrange: "500"

tcp(/udp/sctp)_portrange については値を空にすると unset の意味になります。項目自体記載なしの場合は設定変更はされません。

■ Playbook 実行

# ansible-playbook -i hosts service_present.yml

PLAY [forti] ********************************************************************************************************

TASK [add service object] *******************************************************************************************
changed: [10.1.10.100]

PLAY RECAP **********************************************************************************************************
10.1.10.100                : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

■ 実行後の FortiGate のコンフィグ

FGT # show full-configuration firewall service custom
config firewall service custom
    #略
    edit "hoge_service"
        set proxy disable
        set category "General"
        set protocol TCP/UDP/SCTP
        set helper auto
        set check-reset-range default
        set comment "hoge_service"
        set color 0
        set visibility enable
        set iprange 0.0.0.0
        set fqdn ''
        set tcp-portrange 100-120 200 300
        set udp-portrange 200
        set sctp-portrange 500
        set tcp-halfclose-timer 0
        set tcp-halfopen-timer 0
        set tcp-timewait-timer 0
        set udp-idle-timer 0
        set session-ttl 0
    next
end

サービスオブジェクトの削除

■ Playbook:service_absent.yml

- hosts: forti
  gather_facts: no
  tasks:
    - name: "delete service object"
      fortios_firewall_service_custom:
        vdom: "root"
        state: "absent"
        firewall_service_custom:
          name: "hoge_service"

■ Playbook 実行

# ansible-playbook -i hosts service_absent.yml

PLAY [forti] ********************************************************************************************************

TASK [delete service object] *******************************************************************************************
changed: [10.1.10.100]

PLAY RECAP **********************************************************************************************************
10.1.10.100                : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

サービスグループオブジェクトの設定

fortios_firewall_service_group モジュールを使用することで FortiGate のコンフィグにおける config firewall service group の各項目を設定することができます。

  • サービスグループオブジェクトの追加/変更/削除

に利用できます。

主なパラメータ

パラメータ必須選択肢/デフォルト説明
host対象機器のIPアドレス
usernameSSH用のユーザ
password・”” ←SSH用のパスワード
vdom・root ←対象機器のバーチャルドメイン
https・no
・yes ←
HTTPSを使用するかどうか
ssl_verify・no
・yes ←
対象機器の証明書を検証するかどうか
state・present
・absent
present ⇒ オブジェクト作成/変更
absent ⇒ オブジェクト削除
※Ansible 2.9 からはこのレベルで指定
firewall_service_group各設定項目を子要素に指定
 – state・present
・absent
present ⇒ オブジェクト作成/変更
absent ⇒ オブジェクト削除
※Ansible 2.8 ではこのレベルで指定
 – nameオブジェクト名
 – member所属サービスをリストで指定
  – name所属サービス名
 – commentコメント

備考

  • Ansible 2.8 から追加されたモジュール
  • Legacy モードでの実行のためには fortiosapi が必要
  • httpapi での実行が可能

使用例

httpapi を使用してモジュールを実行する例です。

■ インベントリ:hosts

[forti]
10.1.10.100

[forti:vars]
ansible_user=admin
ansible_password=password
ansible_network_os=fortios
ansible_connection=httpapi
ansible_httpapi_use_ssl=yes
ansible_httpapi_validate_certs=no

10.1.10.100 は操作対象の FortiGate のIPアドレスです。
httpapi で使用する変数をグループ変数として定義しています。

サービスグループオブジェクトの作成

■ Playbook:service_group_present.yml

- hosts: forti
  gather_facts: no
  tasks:
    - name: "add service group object"
      fortios_firewall_service_group:
        vdom: "root"
        state: "present"
        firewall_service_group:
          name: "Service_Group"
          member:
            - name: "HTTP"
            - name: "HTTPS"
            - name: "FTP"
          comment: "test group"

■ Playbook 実行

# ansible-playbook -i hosts service_group_present.yml

PLAY [forti] ********************************************************************************************************

TASK [add service group object] *******************************************************************************************
changed: [10.1.10.100]

PLAY RECAP **********************************************************************************************************
10.1.10.100                : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

■ 実行後の FortiGate のコンフィグ

FGT # show full-configuration firewall service group
config firewall service group
    #略
    edit "Service_Group"
        set member "HTTP" "HTTPS" "FTP"
        set proxy disable
        set comment "test group"
        set color 0
    next
end

サービスグループオブジェクトの削除

■ Playbook:address_iprange.yml

- hosts: forti
  gather_facts: no
  tasks:
    - name: "delete service group object"
      fortios_firewall_service_group:
        vdom: "root"
        state: "absent"
        firewall_service_group:
          name: "Service_Group"

■ Playbook 実行

# ansible-playbook -i hosts service_group_absent.yml

PLAY [forti] ********************************************************************************************************

TASK [delete service group object] **********************************************************************************
changed: [10.1.10.100]

PLAY RECAP **********************************************************************************************************
10.1.10.100                : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

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

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