-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from Jason-RH/change_registration_check
Change registration check, ensure support for insights-client v3, refactor register and config into modules
- Loading branch information
Showing
4 changed files
with
274 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from __future__ import (absolute_import, division, print_function) | ||
__metaclass__ = type | ||
|
||
from ansible.plugins.action import ActionBase | ||
|
||
|
||
class ActionModule(ActionBase): | ||
|
||
def run(self, tmp=None, task_vars=None): | ||
|
||
result = super(ActionModule, self).run(tmp, task_vars) | ||
|
||
insights_name = self._task.args.get('insights_name', 'insights-client') | ||
|
||
config_vars = dict( | ||
username = self._task.args.get('username', None), | ||
password = self._task.args.get('password', None), | ||
auto_config = self._task.args.get('auto_config', None), | ||
authmethod = self._task.args.get('authmethod', None), | ||
display_name = self._task.args.get('display_name', None) | ||
) | ||
|
||
for k, v in config_vars.items(): | ||
if v: | ||
new_module_args = dict( | ||
path = '/etc/' + insights_name + '/' + insights_name + '.conf', | ||
section = insights_name, | ||
option = k, | ||
value = v, | ||
no_extra_spaces = True, | ||
state = "present" | ||
) | ||
result.update(self._execute_module( | ||
module_name='ini_file', | ||
module_args=new_module_args, | ||
task_vars=task_vars, | ||
tmp=tmp | ||
)) | ||
|
||
return result | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
ANSIBLE_METADATA = { | ||
'metadata_version': '1.1', | ||
'status': ['preview'], | ||
'supported_by': 'community' | ||
} | ||
|
||
DOCUMENTATION = ''' | ||
--- | ||
module: insights_config | ||
short_description: This module handles initial configuration of the insights client on install | ||
description: | ||
- Supply values for various configuration options that you would like to use. On install | ||
this module will add those values to the insights-client.conf file prior to registering. | ||
version_added: "3.0" | ||
options: | ||
username: | ||
description: | ||
- Insights basic auth username. If defined this will change, set, or remove the username | ||
in the insights configuration. To remove a username set this value to an empty string. | ||
required: false | ||
password: | ||
description: | ||
- Insights basic auth password. If defined this will change, set, or remove the password | ||
in the insights configuration. To remove a password set this value to an empty string. | ||
required: false | ||
auto_config: | ||
description: | ||
- Attempt to auto-configure the network connection with Satellite or RHSM. Default is True. | ||
required: false | ||
authmethod: | ||
description: | ||
- Authentication method for the Portal (BASIC, CERT). Default is BASIC. Note: when | ||
auto_config is enabled, CERT will be used if RHSM or Satellite is detected. | ||
required: false | ||
display_name: | ||
description: | ||
- Custom display name to appear in the Insights web UI. Only used on machine registration. | ||
Blank by default. | ||
required: false | ||
insights_name: | ||
description: | ||
- For now, this is just 'insights-client', but it could change in the future so having | ||
it as a variable is just preparing for that. | ||
required: false | ||
''' | ||
|
||
EXAMPLES = ''' | ||
- name: Configure the insights client to register with username and password | ||
insights_config: | ||
username: "rhn_support" or "{{ redhat_portal_username }}" if passing in as a role variable | ||
password: "rhn_password" or "{{ redhat_portal_password }}" | ||
auto_config: False or "{{ auto_config }}" | ||
authmethod: BASIC or "{{ authmethod }}" | ||
become: true | ||
- name: Configure the insights client to register with RHSM and no display name | ||
insights_config: | ||
become: true | ||
Note: The above example calls the insights_config module with no parameters. This is because auto_config defaults to True | ||
which in turn forces the client to try RHSM (or Satellite) | ||
- name: Configure the insights client to register with RHSM and a display name | ||
insights_config: | ||
display_name: "nice_name" or "{{ insights_display_name }}" if passing in as a role variable | ||
become: true | ||
''' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
#!/usr/bin/python | ||
|
||
# Copyright: (c) 2018, Terry Jones <[email protected]> | ||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) | ||
|
||
ANSIBLE_METADATA = { | ||
'metadata_version': '1.1', | ||
'status': ['preview'], | ||
'supported_by': 'community' | ||
} | ||
|
||
DOCUMENTATION = ''' | ||
--- | ||
module: insights_register | ||
short_description: This module registers the insights client | ||
description: | ||
- This module will check the current registration status, unregister if needed, | ||
and then register the insights client (and update the display_name if needed) | ||
options: | ||
state: | ||
description: | ||
- Determines whether to register or unregister insights-client | ||
choices: ["present", "absent"] | ||
required: true | ||
insights_name: | ||
description: | ||
- For now, this is just 'insights-client', but it could change in the future | ||
so having it as a variable is just preparing for that | ||
required: false | ||
display_name: | ||
description: | ||
- This option is here to enable registering with a display_name outside of using | ||
a configuration file. Some may be used to doing it this way so I left this in as | ||
an optional parameter. | ||
required: false | ||
force_reregister: | ||
description: | ||
- This option should be set to true if you wish to force a reregister of the insights-client. | ||
Note that this will remove the existing machine-id and create a new one. Only use this option | ||
if you are okay with creating a new machine-id. | ||
required: false | ||
author: | ||
- Jason Stephens (@Jason-RH) | ||
''' | ||
|
||
EXAMPLES = ''' | ||
# Normal Register | ||
- name: Register the insights client | ||
insights_register: | ||
state: present | ||
# Force a Reregister (for config changes, etc) | ||
- name: Resgiter the insights client | ||
insights_register: | ||
state: present | ||
force_reregister: true | ||
# Unregister | ||
- name: Unregister the insights client | ||
insights_regsiter: | ||
state: absent | ||
# Register an install of redhat-access-insights (this is not a 100% automated process) | ||
- name: Register redhat-access-insights | ||
insights_register: | ||
state: present | ||
insights_name: 'redhat-access-insights' | ||
Note: The above example for registering redhat-access-insights requires that the playbook be | ||
changed to install redhat-access-insights and that redhat-access-insights is also passed into | ||
the insights_config module and that the file paths be changed when using the file module | ||
''' | ||
|
||
RETURN = ''' | ||
original_message: | ||
description: Just a sentence declaring that there is a registration attempt | ||
type: str | ||
message: | ||
description: The output message that the module generates | ||
''' | ||
|
||
from ansible.module_utils.basic import AnsibleModule | ||
import subprocess | ||
|
||
def run_module(): | ||
# define available arguments/parameters a user can pass to the module | ||
module_args = dict( | ||
state=dict(choices=['present', 'absent'], default='present'), | ||
insights_name=dict(type='str', required=False, default='insights-client'), | ||
display_name=dict(type='str', required=False, default=''), | ||
force_reregister=dict(type='bool', required=False, default=False) | ||
) | ||
|
||
result = dict( | ||
changed=False, | ||
original_message='', | ||
message='' | ||
) | ||
|
||
module = AnsibleModule( | ||
argument_spec=module_args, | ||
supports_check_mode=True | ||
) | ||
|
||
if module.check_mode: | ||
return result | ||
|
||
state = module.params['state'] | ||
insights_name = module.params['insights_name'] | ||
display_name = module.params['display_name'] | ||
force_reregister = module.params['force_reregister'] | ||
|
||
reg_status = subprocess.call([insights_name, '--status']) | ||
|
||
if state == 'present': | ||
result['original_message'] = 'Attempting to register ' + insights_name | ||
if reg_status == 0 and not force_reregister: | ||
result['changed'] = False | ||
result['message'] = 'The Insights API has determined that this machine is already registered' | ||
module.exit_json(**result) | ||
elif reg_status == 0 and force_reregister: | ||
subprocess.call([insights_name, '--force-reregister']) | ||
result['changed'] = True | ||
result['message'] = 'New machine-id created - ' + insights_name + ' has been registered' | ||
module.exit_json(**result) | ||
else: | ||
subprocess.call([insights_name, '--register']) | ||
result['changed'] = True | ||
result['message'] = insights_name + ' has been registered' | ||
module.exit_json(**result) | ||
|
||
if state == 'absent': | ||
result['original_message'] = 'Attempting to unregister ' + insights_name | ||
if reg_status is not 0: | ||
result['changed'] = False | ||
result['message'] = insights_name + ' is already unregistered' | ||
module.exit_json(**result) | ||
else: | ||
subprocess.call([insights_name, '--unregister']) | ||
result['changed'] = True | ||
result['message'] = insights_name + ' has been unregistered' | ||
module.exit_json(**result) | ||
|
||
def main(): | ||
run_module() | ||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters