87 lines
4.1 KiB
Python
87 lines
4.1 KiB
Python
|
from __future__ import (absolute_import, division, print_function)
|
||
|
__metaclass__ = type
|
||
|
|
||
|
from ansible.plugins.action import ActionBase
|
||
|
import requests
|
||
|
|
||
|
|
||
|
class ActionModule(ActionBase):
|
||
|
|
||
|
def run(self, tmp=None, task_vars=None):
|
||
|
if task_vars is None:
|
||
|
task_vars = {}
|
||
|
|
||
|
# Récupérer les arguments de la tâche
|
||
|
module_args = self._task.args.copy()
|
||
|
|
||
|
# Valider les arguments
|
||
|
if 'opnsense_url' not in module_args:
|
||
|
return {'failed': True, 'msg': 'The "opnsense_url" argument is mandatory.'}
|
||
|
if 'api_key' not in module_args:
|
||
|
return {'failed': True, 'msg': 'The "api_key" argument is mandatory.'}
|
||
|
if 'api_secret' not in module_args:
|
||
|
return {'failed': True, 'msg': 'The "api_secret" argument is mandatory.'}
|
||
|
if 'override_id' not in module_args:
|
||
|
return {'failed': True, 'msg': 'The "override_id" argument is mandatory.'}
|
||
|
if 'alias_host' not in module_args:
|
||
|
return {'failed': True, 'msg': 'The "alias_value" argument is mandatory.'}
|
||
|
if 'alias_domain' not in module_args:
|
||
|
return {'failed': True, 'msg': 'The "alias_domain" argument is mandatory.'}
|
||
|
|
||
|
opnsense_url = module_args['opnsense_url']
|
||
|
api_auth = (module_args['api_key'], module_args['api_secret'])
|
||
|
override_id = module_args['override_id']
|
||
|
alias_host = module_args['alias_host']
|
||
|
alias_domain = module_args['alias_domain']
|
||
|
state = module_args.get('state', 'present')
|
||
|
|
||
|
base_url = f'{opnsense_url}/api/unbound/settings'
|
||
|
|
||
|
try:
|
||
|
# Check if override exist
|
||
|
response = requests.get(f'{base_url}/getHostOverride/{override_id}', auth=api_auth)
|
||
|
if response.status_code != 200 or not response.json():
|
||
|
return {'failed': True, 'msg': f'Fail to fetch override info'}
|
||
|
|
||
|
# Seach host alias
|
||
|
response = requests.post(f'{base_url}/searchHostAlias/{override_id}', auth=api_auth, json= {'host': override_id, 'searchPhrase': alias_host})
|
||
|
if response.status_code != 200 :
|
||
|
return {'failed': True, 'msg': f'Fail to fetch Alias list', 'status_code': response.status_code}
|
||
|
json = response.json()
|
||
|
finded = None
|
||
|
if json.get('rowCount', 0) > 0:
|
||
|
# We have result, check if host-domain pair exist
|
||
|
for row in json.get('rows', []):
|
||
|
if row['hostname'] == alias_host and row['domain'] == alias_domain:
|
||
|
finded = row.copy()
|
||
|
|
||
|
if (state == "present" and finded) or (state == "absent" and not finded):
|
||
|
return {'changed': False}
|
||
|
elif state == "present":
|
||
|
if self._play_context.check_mode:
|
||
|
return {'changed': True}
|
||
|
body = {
|
||
|
"alias": {
|
||
|
"description": "",
|
||
|
"domain": alias_domain,
|
||
|
"enabled": "1",
|
||
|
"host": override_id,
|
||
|
"hostname": alias_host,
|
||
|
}
|
||
|
}
|
||
|
response = requests.post(f'{base_url}/addHostAlias/', auth=api_auth, json=body)
|
||
|
if response.status_code != 200 :
|
||
|
return {'failed': True, 'msg': f'Fail create Alias', 'status_code': response.status_code, 'body': response.json()}
|
||
|
return {'changed': True, 'api_response': response.json()}
|
||
|
elif state == "absent":
|
||
|
if self._play_context.check_mode:
|
||
|
return {'changed': True}
|
||
|
|
||
|
response = requests.post(f'{base_url}/delHostAlias/{finded["uuid"]}', auth=api_auth)
|
||
|
if response.status_code != 200 :
|
||
|
return {'failed': True, 'msg': f'Fail del Alias', 'status_code': response.status_code, 'body': response.json()}
|
||
|
return {'changed': True, 'api_response': response.json()}
|
||
|
except requests.exceptions.RequestException as e:
|
||
|
return {'failed': True, 'msg': f'Error durring API request : {str(e)}'}
|
||
|
|