使用Ansible来获取Fortigate的配置

1. Ansible 对FortiOS的支持

在Ansible 2.3中,引入了适用于FortiOS的以下两个模块。

fortios_config: コンフィグのバックアップを取得したりファイルからコンフィグを投入する

fortios_ipv4_policy: IPv4ポリシーを管理する

这次我们将使用fortios_config来获取Fortigate配置的备份。
(关于fortios_ipv4_policy,请参阅此处)

2. 安装

2.1. 爱斯尼比勒 sī ní bǐ lè)

pip install ansible

这次我在CentOS6.8上进行了安装。

2.2. python焦点组织

由于fortOS相关模块内部使用了名为pyFG的Python模块,因此需要先安装pyFG。

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. 创建库存文件

[forti]
192.168.0.254  # 今回は1台だけ

[forti:vars]
ansible_user=admin
ansible_password=passwordpassword

创建Plyaybook

请将以下文件创建为 forti.yml。

---
- 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,或者已经考虑但因为FortiOS不支持而放弃了Ansible,那么备份配置的自动化机制有很多选择可以考虑,这种方法也可以作为一个备选方案。

bannerAds