-
Notifications
You must be signed in to change notification settings - Fork 166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] Add foreman_ansible_roles module #753
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,106 @@ | ||||||
#!/usr/bin/python | ||||||
# -*- coding: utf-8 -*- | ||||||
# (c) 2020, Brant Evans ([email protected]) | ||||||
# | ||||||
# This program is free software: you can redistribute it and/or modify | ||||||
# it under the terms of the GNU General Public License as published by | ||||||
# the Free Software Foundation, either version 3 of the License, or | ||||||
# (at your option) any later version. | ||||||
# | ||||||
# This program is distributed in the hope that it will be useful, | ||||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||||
# GNU General Public License for more details. | ||||||
# | ||||||
# You should have received a copy of the GNU General Public License | ||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||||
|
||||||
from __future__ import absolute_import, division, print_function | ||||||
__metaclass__ = type | ||||||
|
||||||
|
||||||
ANSIBLE_METADATA = {'metadata_version': '1.1', | ||||||
'status': ['preview'], | ||||||
'supported_by': 'community'} | ||||||
|
||||||
DOCUMENTATION = ''' | ||||||
--- | ||||||
module: foreman_ansible_roles_facts | ||||||
short_description: Gather names of Ansible Roles that can be imported | ||||||
description: | ||||||
- Gather names of Ansible Roles that can be imported | ||||||
author: | ||||||
- "Brant Evans (@branic)" | ||||||
options: | ||||||
proxy: | ||||||
description: | ||||||
- The Smart Proxy name to use for fetching Ansible Roles | ||||||
required: false | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
type: str | ||||||
aliases: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
(the linter is unhappy) |
||||||
- smart_proxy | ||||||
organization: | ||||||
description: | ||||||
- Organization that the role is in | ||||||
required: false | ||||||
type: str | ||||||
location: | ||||||
description: | ||||||
- Location that the role is in | ||||||
required: false | ||||||
type: str | ||||||
extends_documentation_fragment: | ||||||
- foreman | ||||||
''' | ||||||
|
||||||
|
||||||
EXAMPLES = ''' | ||||||
- name: Get Ansible Roles available to import on a proxy | ||||||
foreman_ansible_roles_importable_info: | ||||||
username: "admin" | ||||||
password: "changeme" | ||||||
server_url: "https://foreman.example.com" | ||||||
proxy: "foreman.example.com" | ||||||
register: result | ||||||
- debug: | ||||||
var: result | ||||||
''' | ||||||
|
||||||
RETURN = ''' | ||||||
ansible_roles: | ||||||
description: Roles that are able to be imported on the proxy | ||||||
returned: always | ||||||
type: list | ||||||
''' | ||||||
|
||||||
from ansible.module_utils.foreman_helper import ForemanAnsibleModule, _flatten_entity | ||||||
|
||||||
|
||||||
class ForemanAnsibleRolesImportableInfoModule(ForemanAnsibleModule): | ||||||
pass | ||||||
|
||||||
|
||||||
def main(): | ||||||
|
||||||
module = ForemanAnsibleRolesImportableInfoModule( | ||||||
foreman_spec=dict( | ||||||
proxy=dict(type='entity', flat_name='proxy_id', aliases=['smart_proxy'], resource_type='smart_proxies', required=True), | ||||||
organization=dict(type='entity'), | ||||||
location=dict(type='entity'), | ||||||
Comment on lines
+88
to
+89
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. are those two actually used for anything? I know you can pass them in the request, but I don't see any use for doing so. the data on disk (which this action lists) is not org/loc aware. I think the API just allows to pass those, as it allows this on every request. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ares is that right? 'fetch' should not be scoped? |
||||||
), | ||||||
) | ||||||
|
||||||
resource = 'ansible_roles' | ||||||
|
||||||
with module.api_connection(): | ||||||
module.auto_lookup_entities() | ||||||
params = _flatten_entity(module.foreman_params, module.foreman_spec) | ||||||
|
||||||
resources = module.fetch_resource(resource, params) | ||||||
resources = resources['results']['ansible_roles'] | ||||||
|
||||||
module.exit_json(ansible_roles=resources) | ||||||
|
||||||
|
||||||
if __name__ == '__main__': | ||||||
main() |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,137 @@ | ||||||
#!/usr/bin/python | ||||||
# -*- coding: utf-8 -*- | ||||||
# (c) 2020, Brant Evans ([email protected]) | ||||||
# | ||||||
# This program is free software: you can redistribute it and/or modify | ||||||
# it under the terms of the GNU General Public License as published by | ||||||
# the Free Software Foundation, either version 3 of the License, or | ||||||
# (at your option) any later version. | ||||||
# | ||||||
# This program is distributed in the hope that it will be useful, | ||||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||||
# GNU General Public License for more details. | ||||||
# | ||||||
# You should have received a copy of the GNU General Public License | ||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||||
|
||||||
from __future__ import absolute_import, division, print_function | ||||||
__metaclass__ = type | ||||||
|
||||||
|
||||||
ANSIBLE_METADATA = {'metadata_version': '1.1', | ||||||
'status': ['preview'], | ||||||
'supported_by': 'community'} | ||||||
|
||||||
DOCUMENTATION = ''' | ||||||
--- | ||||||
module: foreman_ansible_roles_facts | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
short_description: Gather imported Ansible Roles | ||||||
description: | ||||||
- Gather details about imported Ansible Roles | ||||||
author: | ||||||
- "Brant Evans (@branic)" | ||||||
options: | ||||||
search: | ||||||
description: | ||||||
- Search query to use | ||||||
- If None, all imported roles are returned | ||||||
type: str | ||||||
full_details: | ||||||
description: | ||||||
- If C(True) all details about the found roles are returned | ||||||
type: bool | ||||||
default: false | ||||||
aliases: | ||||||
- info | ||||||
organization: | ||||||
description: | ||||||
- Organization that the role is in | ||||||
required: false | ||||||
type: str | ||||||
location: | ||||||
description: | ||||||
- Location that the role is in | ||||||
required: false | ||||||
type: str | ||||||
extends_documentation_fragment: | ||||||
- foreman | ||||||
''' | ||||||
|
||||||
|
||||||
EXAMPLES = ''' | ||||||
- name: Get all imported Ansible Roles | ||||||
foreman_ansible_roles_imported_info: | ||||||
username: "admin" | ||||||
password: "changeme" | ||||||
server_url: "https://foreman.example.com" | ||||||
register: result | ||||||
- debug: | ||||||
var: result | ||||||
|
||||||
- name: Find the imported Ansible Role named example_role | ||||||
foreman_ansible_roles_imported_info: | ||||||
username: "admin" | ||||||
password: "changeme" | ||||||
server_url: "https://foreman.example.com" | ||||||
search: "name=example_role" | ||||||
register: result | ||||||
- debug: | ||||||
var: result | ||||||
''' | ||||||
|
||||||
RETURN = ''' | ||||||
ansible_roles: | ||||||
description: Roles that have been imported | ||||||
returned: always | ||||||
type: list | ||||||
''' | ||||||
|
||||||
from ansible.module_utils.foreman_helper import ForemanAnsibleModule, _flatten_entity | ||||||
|
||||||
|
||||||
class ForemanAnsibleRolesImportedInfoModule(ForemanAnsibleModule): | ||||||
pass | ||||||
|
||||||
|
||||||
def main(): | ||||||
|
||||||
module = ForemanAnsibleRolesImportedInfoModule( | ||||||
foreman_spec=dict( | ||||||
search=dict(default=""), | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
no need for a default |
||||||
full_details=dict(type='bool', aliases=['info'], default=False), | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. given the API does not return more info, shall we drop that param? |
||||||
organization=dict(type='entity'), | ||||||
location=dict(type='entity'), | ||||||
), | ||||||
) | ||||||
|
||||||
resource = 'ansible_roles' | ||||||
|
||||||
with module.api_connection(): | ||||||
module.auto_lookup_entities() | ||||||
params = _flatten_entity(module.foreman_params, module.foreman_spec) | ||||||
|
||||||
# According to the APIDoc organization_id and location_id are valid | ||||||
# parameters, but a "500 Internal Server Error" error is returned | ||||||
# when an organization_id or location_id are present in the request | ||||||
# See https://projects.theforeman.org/issues/29583 | ||||||
if 'organization_id' in params: | ||||||
del params['organization_id'] | ||||||
|
||||||
if 'location_id' in params: | ||||||
del params['location_id'] | ||||||
|
||||||
response = module.list_resource(resource, module.foreman_params.get('search'), params) | ||||||
|
||||||
if module.foreman_params['full_details']: | ||||||
resources = [] | ||||||
for found_resource in response: | ||||||
resources.append(module.show_resource(resource, found_resource['id'], params)) | ||||||
else: | ||||||
resources = response | ||||||
|
||||||
module.exit_json(ansible_roles=resources) | ||||||
|
||||||
|
||||||
if __name__ == '__main__': | ||||||
main() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.