mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-23 21:30:22 -07:00
Fortios ipv4 policy (#21849)
* New module fortios_address_group * New module fortios_ipv4_policy * New module fortios_ipv4_policy * Fix pep8 * Fix alias doc problem * Fix string format for 2.5 compat + close cnx * Forgoten if string != "" * Fix doc, change action to policy_action & add action as alias * fix doc + bug in timeout + duplicate code for config compare * Create class AnsibleFortios in module_utils/forios.py + use in ipv4_policy module * remove json import * python3 error handling compatibility bad examples for srcadd or dstaddr s/any/all/ remove pyFG dependency in module (moved to module_utils) id type is int but casted as string call fortiosansible object sooner typo in doc
This commit is contained in:
parent
17a4a655a3
commit
2ee3a5aa07
2 changed files with 400 additions and 0 deletions
|
@ -29,6 +29,17 @@
|
|||
import os
|
||||
import time
|
||||
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
from ansible.module_utils.pycompat24 import get_exception
|
||||
|
||||
|
||||
#check for pyFG lib
|
||||
try:
|
||||
from pyFG import FortiOS, FortiConfig
|
||||
from pyFG.exceptions import CommandExecutionException, FailedCommit
|
||||
HAS_PYFG=True
|
||||
except:
|
||||
HAS_PYFG=False
|
||||
|
||||
fortios_argument_spec = dict(
|
||||
host = dict(required=True ),
|
||||
|
@ -45,6 +56,13 @@ fortios_required_if = [
|
|||
['backup', True , ['backup_path'] ],
|
||||
]
|
||||
|
||||
|
||||
fortios_error_codes = {
|
||||
'-3':"Object not found",
|
||||
'-61':"Command error"
|
||||
}
|
||||
|
||||
|
||||
def backup(module,running_config):
|
||||
backup_path = module.params['backup_path']
|
||||
if not os.path.exists(backup_path):
|
||||
|
@ -58,3 +76,96 @@ def backup(module,running_config):
|
|||
open(filename, 'w').write(running_config)
|
||||
except:
|
||||
module.fail_json(msg="Can't create backup file {0} Permission denied ?".format(filename))
|
||||
|
||||
|
||||
|
||||
|
||||
class AnsibleFortios(object):
|
||||
def __init__(self, module):
|
||||
if not HAS_PYFG:
|
||||
module.fail_json(msg='Could not import the python library pyFG required by this module')
|
||||
|
||||
self.result = {
|
||||
'changed': False,
|
||||
}
|
||||
self.module = module
|
||||
|
||||
|
||||
def _connect(self):
|
||||
host = self.module.params['host']
|
||||
username = self.module.params['username']
|
||||
password = self.module.params['password']
|
||||
timeout = self.module.params['timeout']
|
||||
vdom = self.module.params['vdom']
|
||||
|
||||
self.forti_device = FortiOS(host, username=username, password=password, timeout=timeout, vdom=vdom)
|
||||
|
||||
try:
|
||||
self.forti_device.open()
|
||||
except Exception:
|
||||
e = get_exception()
|
||||
self.module.fail_json(msg='Error connecting device. %s' % e)
|
||||
|
||||
|
||||
def load_config(self, path):
|
||||
self._connect()
|
||||
self.path = path
|
||||
#get config
|
||||
try:
|
||||
self.forti_device.load_config(path=path)
|
||||
self.result['running_config'] = self.forti_device.running_config.to_text()
|
||||
except Exception:
|
||||
self.forti_device.close()
|
||||
e = get_exception()
|
||||
self.module.fail_json(msg='Error reading running config. %s' % e)
|
||||
|
||||
#backup if needed
|
||||
if self.module.params['backup']:
|
||||
backup(self.module, self.result['running_config'])
|
||||
|
||||
self.candidate_config = self.forti_device.candidate_config
|
||||
|
||||
|
||||
def apply_changes(self):
|
||||
change_string = self.forti_device.compare_config()
|
||||
if change_string:
|
||||
self.result['change_string'] = change_string
|
||||
self.result['changed'] = True
|
||||
|
||||
#Commit if not check mode
|
||||
if change_string and not self.module.check_mode:
|
||||
try:
|
||||
self.forti_device.commit()
|
||||
except FailedCommit:
|
||||
#Something's wrong (rollback is automatic)
|
||||
self.forti_device.close()
|
||||
e = get_exception()
|
||||
error_list = self.get_error_infos(e)
|
||||
self.module.fail_json(msg_error_list=error_list, msg="Unable to commit change, check your args, the error was %s" % e.message )
|
||||
|
||||
self.forti_device.close()
|
||||
self.module.exit_json(**self.result)
|
||||
|
||||
|
||||
def del_block(self, block_id):
|
||||
self.forti_device.candidate_config[self.path].del_block(block_id)
|
||||
|
||||
|
||||
def add_block(self, block_id, block):
|
||||
self.forti_device.candidate_config[self.path][block_id] = block
|
||||
|
||||
|
||||
def get_error_infos(self, cli_errors):
|
||||
error_list = []
|
||||
for errors in cli_errors.args:
|
||||
for error in errors:
|
||||
error_code = error[0]
|
||||
error_string = error[1]
|
||||
error_type = fortios_error_codes.get(error_code,"unknown")
|
||||
error_list.append(dict(error_code=error_code, error_type=error_type, error_string= error_string))
|
||||
|
||||
return error_list
|
||||
|
||||
def get_empty_configuration_block(self, block_name, block_type):
|
||||
return FortiConfig(block_name, block_type)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue