Skip to content

fortinet/ansible-fortios-generic

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 

Repository files navigation

fortios_json_generic Config Fortinet's FortiOS and FortiGate with json generic method.

source:fortios_json_generic.py
orphan:

fortios_json_generic -- Config Fortinet's FortiOS and FortiGate with json generic method.

This module is able to configure a FortiGate or FortiOS (FOS) device by allowing the user to set any category supported by FortiAPI with raw json. Examples include all parameters and values need to be adjusted to datasources before usage. Tested with FOS v6.0.4.

  • Copy fortios_json_generic.py to ANSIBLE_PATH/lib/ansible/modules/network/fortios/
  • Copy fortios.py to ANSIBLE_PATH/lib/ansible/module_utils/network/fortios/ (Attention: Backup your ANSIBLE_PATH/lib/ansible/module_utils/network/fortios/fortios.py file first)

You can also use the script patch.sh to complete the above steps under Linux/Unix.

  • host - FortiOS or FortiGate IP address.  |  type: str required: false
  • username - FortiOS or FortiGate username.  |  type: str required: false
  • password - FortiOS or FortiGate password.  |  type: str default: ""
  • vdom - Virtual domain, among those defined previously. A vdom is a virtual instance of the FortiGate that can be configured and used as a different unit.  |  type: str default: root
  • https - Indicates if the requests towards FortiGate must use HTTPS protocol.  |  type: bool default: true
  • ssl_verify - Ensures FortiGate certificate must be verified by a proper CA.  |  type: bool default: true
  • json_generic - json generic default: null  |  type: dict
    • dictbody - Body with YAML list of key/value format  |  type: dict
    • jsonbody - Body with JSON string format, will always give priority to jsonbody  |  type: str
    • method - HTTP methods  |  type: str choices: GET, PUT, POST, DELETE
    • path - URL path, e.g./api/v2/cmdb/firewall/address  |  type: str
    • specialparams - Extra URL parameters, e.g.start=1&count=10  |  type: str

host

[fortigates]
fortigate01 ansible_host=192.168.52.177 ansible_user="admin" ansible_password="admin"

[fortigates:vars]
ansible_network_os=fortios

sample1.yml

---
- hosts: fortigates
  connection: httpapi
  vars:
   vdom: "root"
   ansible_httpapi_use_ssl: yes
   ansible_httpapi_validate_certs: no
   ansible_httpapi_port: 443

  tasks:
  - name: test add with string
    fortios_json_generic:
      vdom:  "{{ vdom }}"
      json_generic:
        method: "POST"
        path: "/api/v2/cmdb/firewall/address"
        jsonbody: |
          {
          "name": "111",
          "type": "geography",
          "fqdn": "",
          "country": "AL",
          "comment": "ccc",
          "visibility": "enable",
          "associated-interface": "port1",
          "allow-routing": "disable"
          }
    register: info

  - name: display vars
    debug: msg="{{info}}"

sample2.yml

---
- hosts: fortigates
  connection: httpapi
  vars:
   vdom: "root"
   ansible_httpapi_use_ssl: yes
   ansible_httpapi_validate_certs: no
   ansible_httpapi_port: 443

  tasks:
  - name: test delete
    fortios_json_generic:
      vdom:  "{{ vdom }}"
      json_generic:
        method: "DELETE"
        path: "/api/v2/cmdb/firewall/address/111"
        specialparams: "testpara1=1&testpara2=2"
    register: info

  - name: display vars
    debug: msg="{{info}}"

  - name: test add with dict
    fortios_json_generic:
      vdom:  "{{ vdom }}"
      json_generic:
        method: "POST"
        path: "/api/v2/cmdb/firewall/address"
        dictbody:
          name: "111"
          type: "geography"
          fqdn: ""
          country: "AL"
          comment: "ccc"
          visibility: "enable"
          associated-interface: "port1"
          allow-routing: "disable"
    register: info

  - name: display vars
    debug: msg="{{info}}"

  - name: test delete
    fortios_json_generic:
      vdom:  "{{ vdom }}"
      json_generic:
        method: "DELETE"
        path: "/api/v2/cmdb/firewall/address/111"
    register: info

  - name: display vars
    debug: msg="{{info}}"

  - name: test add with string
    fortios_json_generic:
      vdom:  "{{ vdom }}"
      json_generic:
        method: "POST"
        path: "/api/v2/cmdb/firewall/address"
        jsonbody: |
          {
          "name": "111",
          "type": "geography",
          "fqdn": "",
          "country": "AL",
          "comment": "ccc",
          "visibility": "enable",
          "associated-interface": "port1",
          "allow-routing": "disable"
          }
    register: info

  - name: display vars
    debug: msg="{{info}}"

sample3.yml

---
- hosts: fortigates
  connection: httpapi
  vars:
   vdom: "root"
   ansible_httpapi_use_ssl: yes
   ansible_httpapi_validate_certs: no
   ansible_httpapi_port: 443

  tasks:
  - name: test firewall policy order modification
    fortios_json_generic:
      vdom:  "{{ vdom }}"
      json_generic:
            method: "PUT"
            path: "/api/v2/cmdb/firewall/policy/1"
            specialparams: "action=move&after=2"
    register: info

  - name: display vars
    debug: msg="{{info}}"

Common return values are documented: https://docs.ansible.com/ansible/latest/reference_appendices/common_return_values.html#common-return-values, the following are the fields unique to this module:

  • build - Build number of the fortigate image returned: always  |  type: str sample: '1547'
  • http_method - Last method used to provision the content into FortiGate returned: always  |  type: str sample: 'PUT'
  • http_status - Last result given by FortiGate on last operation applied returned: always  |  type: str sample: 200
  • mkey - Master key (id) used in the last call to FortiGate returned: success  |  type: str sample: id
  • name - Name of the table used to fulfill the request returned: always  |  type: str sample: urlfilter
  • path - Path of the table used to fulfill the request returned: always  |  type: str sample: webfilter
  • revision - Internal revision number returned: always  |  type: str sample: 17.0.2.10658
  • serial - Serial number of the unit returned: always  |  type: str sample: FGVMEVYYQT3AB5352
  • status - Indication of the operation's result returned: always  |  type: str sample: success
  • vdom - Virtual domain used returned: always  |  type: str sample: root
  • version - Version of the FortiGate returned: always  |  type: str sample: v5.6.3
  • This module is not guaranteed to have a backwards compatible interface.
  • Frank Shen (@fshen01)
  • Hongbin Lu (@fgtdev-hblu)

The feature is only supported in HTTPAPI. It is not supported in FortiOS API. According to Ansible's official recommendations, FortiOS API will be dreprecated in Ansible.

It's preferable to use FortiOS Ansible Collection instead unless some features are not available there.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •