LoginSignup
8

More than 5 years have passed since last update.

Organization

Ansible で Fortigate のコンフィグを取得する

1. Ansible のFortiOS対応

Ansible 2.3 では、FortiOSに対応した以下2つのモジュールが導入されました。

  • fortios_config: コンフィグのバックアップを取得したりファイルからコンフィグを投入する
  • fortios_ipv4_policy: IPv4ポリシーを管理する

今回は fortios_config を利用して、Fortigate のコンフィグのバックアップを取得してみます。
(fortios_ipv4_policy についてはこちら)

2. インストール

2.1. Asnbile

pip install ansible

今回はCentOS6.8上にインストールしました。

2.2. pyFG

fortOS 関連のモジュールは内部でpyFGというPythonのモジュールを利用しているためpyFGもインストールしておきます。

pyFGnoのインストール
pip install pyFG

2.3. バージョン確認

バージョン確認
[root@localhost ansible]# ansible --version
ansible 2.3.0.0
  config file =
  configured module search path = Default w/o overrides
  python version = 2.7.8 (default, Oct 22 2016, 09:02:55) [GCC 4.4.7 20120313 (Red Hat 4.4.7-17)]

ansible.cfg は特に作成していません。

3. インベントリファイルの作成

インベントリファイル例(/etc/ansible/hosts)
[forti]
192.168.0.254  # 今回は1台だけ

[forti:vars]
ansible_user=admin
ansible_password=passwordpassword

4. Plyaybookの作成

以下のファイルを forti.yml として作成します。

Playbook
---
- hosts: forti
  gather_facts: no
  connection: local

  tasks:
    - name: Backup current config
      fortios_config:
        host: "{{ inventory_hostname }}"
        username: "{{ ansible_user }}"
        password: "{{ ansible_password }}"
        backup_path: /root/ansible/config_backup/  # バックアップ先ディレクトリ
        backup: yes  # バックアップする指定

本モジュールの公式ドキュメントが参考になります。

5. 実行

実行結果例
[root@localhost ansible]# ansible-playbook forti.yml

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

TASK [Backup current config] *********************************************************************************
ok: [192.168.0.254]

PLAY RECAP ***************************************************************************************************
192.168.0.254               : ok=1    changed=0    unreachable=0    failed=0

[root@localhost ansible]#

6. バックアップコンフィグの確認

6.1. ファイルの存在確認

存在確認
[root@localhost ansible]# ls -al /root/ansible/config_backup/ 
-rw-r--r--.  1 root root 48996  4月  1 16:05 2017 192.168.1.81_config.2017-04-01@16:05:30

6.2. ファイルの内容確認

内容確認
[root@localhost ansible]# ls -al /root/ansible/config_backup/192.168.0.254_config.2017-04-01@16:05:30
    config system global
      set timezone 04
      set fgd-alert-subscription advisory latest-threat
      set hostname "fortigate01"
      set internal-switch-mode interface
    end
    config system accprofile
        edit prof_admin
          set vpngrp read-write
          set utmgrp read-write
 (~略~)

無事に取得できました。

7. おわりに

コンフィグのバックアップを自動で取得する仕組みはいろいろありますが、Ansibleを導入済みであったり、「検討していたがAnsibleはFortiOSが未対応だからやめた」という方がいらっしゃいましたら、この方法も候補に入れてもよいのではないでしょうか。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
8