mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Add the option to use config file instead of enviroment variables (#32497)
and some error handling for the requests
This commit is contained in:
parent
e9b0a4ccb4
commit
7c998027c0
1 changed files with 44 additions and 31 deletions
|
@ -13,6 +13,8 @@ DOCUMENTATION = '''
|
||||||
short_description: Sends events to Foreman
|
short_description: Sends events to Foreman
|
||||||
description:
|
description:
|
||||||
- This callback will report facts and task events to Foreman https://theforeman.org/
|
- This callback will report facts and task events to Foreman https://theforeman.org/
|
||||||
|
- Before 2.4, if you wanted to use an ini configuration, the file must be placed in the same directory as this plugin and named foreman.ini
|
||||||
|
- In 2.4 and above you can just put it in the main Ansible configuration file.
|
||||||
version_added: "2.2"
|
version_added: "2.2"
|
||||||
requirements:
|
requirements:
|
||||||
- whitelisting in configuration
|
- whitelisting in configuration
|
||||||
|
@ -23,14 +25,26 @@ DOCUMENTATION = '''
|
||||||
env:
|
env:
|
||||||
- name: FOREMAN_URL
|
- name: FOREMAN_URL
|
||||||
required: True
|
required: True
|
||||||
|
default: http://localhost:3000
|
||||||
|
ini:
|
||||||
|
- section: callback_foreman
|
||||||
|
key: url
|
||||||
ssl_cert:
|
ssl_cert:
|
||||||
description: X509 certificate to authenticate to Foreman if https is used
|
description: X509 certificate to authenticate to Foreman if https is used
|
||||||
env:
|
env:
|
||||||
- name: FOREMAN_SSL_CERT
|
- name: FOREMAN_SSL_CERT
|
||||||
|
default: /etc/foreman/client_cert.pem
|
||||||
|
ini:
|
||||||
|
- section: callback_foreman
|
||||||
|
key: ssl_cert
|
||||||
ssl_key:
|
ssl_key:
|
||||||
description: the corresponding private key
|
description: the corresponding private key
|
||||||
env:
|
env:
|
||||||
- name: FOREMAN_SSL_KEY
|
- name: FOREMAN_SSL_KEY
|
||||||
|
default: /etc/foreman/client_key.pem
|
||||||
|
ini:
|
||||||
|
- section: callback_foreman
|
||||||
|
key: ssl_key
|
||||||
verify_certs:
|
verify_certs:
|
||||||
description:
|
description:
|
||||||
- Toggle to decidewhether to verify the Foreman certificate.
|
- Toggle to decidewhether to verify the Foreman certificate.
|
||||||
|
@ -38,6 +52,10 @@ DOCUMENTATION = '''
|
||||||
- Set to '0' to disable certificate checking.
|
- Set to '0' to disable certificate checking.
|
||||||
env:
|
env:
|
||||||
- name: FOREMAN_SSL_VERIFY
|
- name: FOREMAN_SSL_VERIFY
|
||||||
|
default: 1
|
||||||
|
ini:
|
||||||
|
- section: callback_foreman
|
||||||
|
key: verify_certs
|
||||||
'''
|
'''
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
@ -56,32 +74,11 @@ from ansible.plugins.callback import CallbackBase
|
||||||
|
|
||||||
|
|
||||||
class CallbackModule(CallbackBase):
|
class CallbackModule(CallbackBase):
|
||||||
"""
|
|
||||||
This callback will report facts and reports to Foreman https://theforeman.org/
|
|
||||||
|
|
||||||
It makes use of the following environment variables:
|
|
||||||
|
|
||||||
FOREMAN_URL: URL to the Foreman server
|
|
||||||
FOREMAN_SSL_CERT: X509 certificate to authenticate to Foreman if
|
|
||||||
https is used
|
|
||||||
FOREMAN_SSL_KEY: the corresponding private key
|
|
||||||
FOREMAN_SSL_VERIFY: whether to verify the Foreman certificate
|
|
||||||
It can be set to '1' to verify SSL certificates using the
|
|
||||||
installed CAs or to a path pointing to a CA bundle. Set to '0'
|
|
||||||
to disable certificate checking.
|
|
||||||
"""
|
|
||||||
|
|
||||||
CALLBACK_VERSION = 2.0
|
CALLBACK_VERSION = 2.0
|
||||||
CALLBACK_TYPE = 'notification'
|
CALLBACK_TYPE = 'notification'
|
||||||
CALLBACK_NAME = 'foreman'
|
CALLBACK_NAME = 'foreman'
|
||||||
CALLBACK_NEEDS_WHITELIST = True
|
CALLBACK_NEEDS_WHITELIST = True
|
||||||
|
|
||||||
FOREMAN_URL = os.getenv('FOREMAN_URL', "http://localhost:3000")
|
|
||||||
FOREMAN_SSL_CERT = (os.getenv('FOREMAN_SSL_CERT',
|
|
||||||
"/etc/foreman/client_cert.pem"),
|
|
||||||
os.getenv('FOREMAN_SSL_KEY',
|
|
||||||
"/etc/foreman/client_key.pem"))
|
|
||||||
FOREMAN_SSL_VERIFY = os.getenv('FOREMAN_SSL_VERIFY', "1")
|
|
||||||
FOREMAN_HEADERS = {
|
FOREMAN_HEADERS = {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
"Accept": "application/json"
|
"Accept": "application/json"
|
||||||
|
@ -93,6 +90,14 @@ class CallbackModule(CallbackBase):
|
||||||
self.items = defaultdict(list)
|
self.items = defaultdict(list)
|
||||||
self.start_time = int(time.time())
|
self.start_time = int(time.time())
|
||||||
|
|
||||||
|
def set_options(self, options):
|
||||||
|
|
||||||
|
super(CallbackModule, self).set_options(options)
|
||||||
|
|
||||||
|
self.FOREMAN_URL = self._plugin_options['url']
|
||||||
|
self.FOREMAN_SSL_CERT = (self._plugin_options['ssl_cert'], self._plugin_options['ssl_key'])
|
||||||
|
self.FOREMAN_SSL_VERIFY = self._plugin_options['verify_certs']
|
||||||
|
|
||||||
if HAS_REQUESTS:
|
if HAS_REQUESTS:
|
||||||
requests_major = int(requests.__version__.split('.')[0])
|
requests_major = int(requests.__version__.split('.')[0])
|
||||||
if requests_major >= 2:
|
if requests_major >= 2:
|
||||||
|
@ -136,11 +141,15 @@ class CallbackModule(CallbackBase):
|
||||||
facts = {"name": host,
|
facts = {"name": host,
|
||||||
"facts": data,
|
"facts": data,
|
||||||
}
|
}
|
||||||
requests.post(url=self.FOREMAN_URL + '/api/v2/hosts/facts',
|
try:
|
||||||
data=json.dumps(facts),
|
r = requests.post(url=self.FOREMAN_URL + '/api/v2/hosts/facts',
|
||||||
headers=self.FOREMAN_HEADERS,
|
data=json.dumps(facts),
|
||||||
cert=self.FOREMAN_SSL_CERT,
|
headers=self.FOREMAN_HEADERS,
|
||||||
verify=self.ssl_verify)
|
cert=self.FOREMAN_SSL_CERT,
|
||||||
|
verify=self.ssl_verify)
|
||||||
|
r.raise_for_status()
|
||||||
|
except requests.exceptions.RequestException as err:
|
||||||
|
print(str(err))
|
||||||
|
|
||||||
def _build_log(self, data):
|
def _build_log(self, data):
|
||||||
logs = []
|
logs = []
|
||||||
|
@ -192,11 +201,15 @@ class CallbackModule(CallbackBase):
|
||||||
# To be changed to /api/v2/config_reports in 1.11. Maybe we
|
# To be changed to /api/v2/config_reports in 1.11. Maybe we
|
||||||
# could make a GET request to get the Foreman version & do
|
# could make a GET request to get the Foreman version & do
|
||||||
# this automatically.
|
# this automatically.
|
||||||
requests.post(url=self.FOREMAN_URL + '/api/v2/reports',
|
try:
|
||||||
data=json.dumps(report),
|
r = requests.post(url=self.FOREMAN_URL + '/api/v2/reports',
|
||||||
headers=self.FOREMAN_HEADERS,
|
data=json.dumps(report),
|
||||||
cert=self.FOREMAN_SSL_CERT,
|
headers=self.FOREMAN_HEADERS,
|
||||||
verify=self.ssl_verify)
|
cert=self.FOREMAN_SSL_CERT,
|
||||||
|
verify=self.ssl_verify)
|
||||||
|
r.raise_for_status()
|
||||||
|
except requests.exceptions.RequestException as err:
|
||||||
|
print(str(err))
|
||||||
self.items[host] = []
|
self.items[host] = []
|
||||||
|
|
||||||
def append_result(self, result):
|
def append_result(self, result):
|
||||||
|
|
Loading…
Reference in a new issue