mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-10-10 18:34:03 -07:00
parent
2f33c1a1a1
commit
5553b20828
206 changed files with 1853 additions and 1870 deletions
|
@ -33,31 +33,31 @@ from ansible.module_utils.basic import AnsibleModule
|
|||
from ansible.module_utils.pycompat24 import get_exception
|
||||
|
||||
|
||||
#check for pyFG lib
|
||||
# check for pyFG lib
|
||||
try:
|
||||
from pyFG import FortiOS, FortiConfig
|
||||
from pyFG.exceptions import CommandExecutionException, FailedCommit
|
||||
HAS_PYFG=True
|
||||
HAS_PYFG = True
|
||||
except ImportError:
|
||||
HAS_PYFG=False
|
||||
HAS_PYFG = False
|
||||
|
||||
fortios_argument_spec = dict(
|
||||
file_mode = dict(type='bool', default=False),
|
||||
config_file = dict(type='path'),
|
||||
host = dict( ),
|
||||
username = dict( ),
|
||||
password = dict(type='str', no_log=True ),
|
||||
timeout = dict(type='int', default=60),
|
||||
vdom = dict(type='str', default=None ),
|
||||
backup = dict(type='bool', default=False),
|
||||
backup_path = dict(type='path'),
|
||||
backup_filename = dict(type='str'),
|
||||
file_mode=dict(type='bool', default=False),
|
||||
config_file=dict(type='path'),
|
||||
host=dict(),
|
||||
username=dict(),
|
||||
password=dict(type='str', no_log=True),
|
||||
timeout=dict(type='int', default=60),
|
||||
vdom=dict(type='str'),
|
||||
backup=dict(type='bool', default=False),
|
||||
backup_path=dict(type='path'),
|
||||
backup_filename=dict(type='str'),
|
||||
)
|
||||
|
||||
fortios_required_if = [
|
||||
['file_mode', False, ['host', 'username', 'password']],
|
||||
['file_mode', True, ['config_file']],
|
||||
['backup', True , ['backup_path'] ],
|
||||
['backup', True, ['backup_path']],
|
||||
]
|
||||
|
||||
fortios_mutually_exclusive = [
|
||||
|
@ -67,12 +67,12 @@ fortios_mutually_exclusive = [
|
|||
]
|
||||
|
||||
fortios_error_codes = {
|
||||
'-3':"Object not found",
|
||||
'-61':"Command error"
|
||||
'-3': "Object not found",
|
||||
'-61': "Command error"
|
||||
}
|
||||
|
||||
|
||||
def backup(module,running_config):
|
||||
def backup(module, running_config):
|
||||
backup_path = module.params['backup_path']
|
||||
backup_filename = module.params['backup_filename']
|
||||
if not os.path.exists(backup_path):
|
||||
|
@ -91,8 +91,6 @@ def backup(module,running_config):
|
|||
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:
|
||||
|
@ -103,7 +101,6 @@ class AnsibleFortios(object):
|
|||
}
|
||||
self.module = module
|
||||
|
||||
|
||||
def _connect(self):
|
||||
if self.module.params['file_mode']:
|
||||
self.forti_device = FortiOS('')
|
||||
|
@ -122,11 +119,10 @@ class AnsibleFortios(object):
|
|||
e = get_exception()
|
||||
self.module.fail_json(msg='Error connecting device. %s' % e)
|
||||
|
||||
|
||||
def load_config(self, path):
|
||||
self.path = path
|
||||
self._connect()
|
||||
#load in file_mode
|
||||
# load in file_mode
|
||||
if self.module.params['file_mode']:
|
||||
try:
|
||||
f = open(self.module.params['config_file'], 'r')
|
||||
|
@ -135,10 +131,10 @@ class AnsibleFortios(object):
|
|||
except IOError:
|
||||
e = get_exception()
|
||||
self.module.fail_json(msg='Error reading configuration file. %s' % e)
|
||||
self.forti_device.load_config(config_text=running, path = path)
|
||||
self.forti_device.load_config(config_text=running, path=path)
|
||||
|
||||
else:
|
||||
#get config
|
||||
# get config
|
||||
try:
|
||||
self.forti_device.load_config(path=path)
|
||||
except Exception:
|
||||
|
@ -146,22 +142,21 @@ class AnsibleFortios(object):
|
|||
e = get_exception()
|
||||
self.module.fail_json(msg='Error reading running config. %s' % e)
|
||||
|
||||
#set configs in object
|
||||
# set configs in object
|
||||
self.result['running_config'] = self.forti_device.running_config.to_text()
|
||||
self.candidate_config = self.forti_device.candidate_config
|
||||
|
||||
#backup if needed
|
||||
# backup if needed
|
||||
if self.module.params['backup']:
|
||||
backup(self.module, self.forti_device.running_config.to_text())
|
||||
|
||||
|
||||
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
|
||||
# Commit if not check mode
|
||||
if change_string and not self.module.check_mode:
|
||||
if self.module.params['file_mode']:
|
||||
try:
|
||||
|
@ -175,35 +170,31 @@ class AnsibleFortios(object):
|
|||
try:
|
||||
self.forti_device.commit()
|
||||
except FailedCommit:
|
||||
#Something's wrong (rollback is automatic)
|
||||
# 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.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))
|
||||
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