haproxy/plugins/action/opnsense_unbound_override_reconfigure.py

40 lines
1.5 KiB
Python
Raw Normal View History

2023-06-13 23:26:51 +02:00
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.'}
opnsense_url = module_args['opnsense_url']
api_auth = (module_args['api_key'], module_args['api_secret'])
base_url = f'{opnsense_url}/api/unbound/service/reconfigure'
try:
if self._play_context.check_mode:
return {'changed': True}
response = requests.post(base_url, auth=api_auth)
if response.status_code != 200 :
return {'failed': True, 'msg': f'Fail apply host override change', '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)}'}