Use redhat_repos module for mananging repos.

The module needs some work in order to operate efficiently. Currently
it always runs and reports a change. Additionally, calling it using a loop
results in multiple runs of the subscription-manager tool, which is quite
slow.
This commit is contained in:
Sam Doran 2016-11-11 13:38:41 -05:00
parent fa24f09ff4
commit d1417155c6
4 changed files with 119 additions and 5 deletions

View File

@ -17,7 +17,7 @@ Role Variables
| `rhn_username` | `{{ lookup('env', 'RHN_USERNAME') }}` | Red Hat Portal username. |
| `rhn_password` | `{{ lookup('env', 'RHN_PASSWORD') }}` | Red Hat Portal password. |
| `rhsub_state` | `enable` | Whether to enable or disable a Red Hat subscription. |
| `rhsub_autosubscribe` | `True` | Whether or not to autosubscibe to available repositories. |
| `rhsub_autosubscribe` | `yes` | Whether or not to autosubscibe to available repositories. |
| `rhsub_repos` | `[undefined]` | If defined, the list of repositories to enable or disable. See `defaults/main.yml` for examples. |
Dependencies
@ -35,7 +35,9 @@ Example Playbook
rhn_password: "{{ vault_rhn_password }}"
rhsub_repos:
- name: rhel-7-server-extras-rpms
state: enable
state: present
- name: rhel-7-server-rh-common-rpms
- name: rhel-7-server-openstack-8-rpms
roles:
- samdoran.redhat-subscription

107
library/redhat_repos.py Normal file
View File

@ -0,0 +1,107 @@
#!/usr/bin/python
# Karim Boumedhel (karim@redhat.com)
#
# Ansible 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.
#
# Ansible 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 Ansible. If not, see <http://www.gnu.org/licenses/>.
from ansible.module_utils.basic import AnsibleModule
import os
DOCUMENTATION = '''
module: redhat_repos
short_description: Handles repositories for rhel machines
description:
- Handles repositories for rhel machines
version_added: "2.3"
author: "Karim Boumedhel, @karmab"
notes:
- This module doesn't handle subscriptions of the machine, only its repositories
requirements:
- subscription manager and a rhel machine
options:
repos:
description:
- a list of repositories to either add or remove
required: true
default: null
only:
description:
- whether the indicated repos should be the only one left to the system
required: false
default: no
state:
description:
- whether the repositories should be made present or absent
required: false
default: present
'''
EXAMPLES = '''
- name: Assign Openstack Liberty Repositories
redhat_repos:
repos:
- rhel-7-server-rpms
- rhel-7-server-rh-common-rpms
- rhel-7-server-openstack-8-rpms
- rhel-ha-for-rhel-7-server-rpms
- rhel-7-server-extras-rpms
'''
RETURN = '''
stdout:
description: output from subscription-manager
returned: success, when needed
type: string
sample: "Loaded plugins: product-id, refresh-packagekit, subscription-manager\nUpdating Red Hat repositories"
'''
def main():
argument_spec = {
"repos": {"required": True, "type": "list"},
"state": {
"default": "present",
"choices": ['present', 'absent'],
"type": 'str'
},
"only": {"default": 'no', "required": False, "type": "str", "choices": ['yes', 'no']},
}
module = AnsibleModule(argument_spec=argument_spec)
repos = module.params['repos']
state = module.params['state']
only = module.params['only']
if state == 'present':
if only == 'yes':
os.system("subscription-manager repos --disable='*'" % repos)
repos = ' '.join(['--enable=' + repo for repo in repos])
# result = os.system("subscription-manager repos %s" % repos)
result = os.popen("subscription-manager repos %s" % repos).read()
if 'Error' in result:
module.fail_json(msg=result)
meta = {'result': result}
changed = True
skipped = False
else:
repos = ' '.join(['--disable=' + repo for repo in repos])
result = os.popen("subscription-manager repos %s" % repos).read()
if 'Error' in result:
module.fail_json(msg=result)
meta = {'result': result}
changed = True
skipped = False
module.exit_json(changed=changed, skipped=skipped, meta=meta)
if __name__ == '__main__':
main()

View File

@ -26,8 +26,11 @@
- rhsub_register
- name: Configure repository subscriptions
command: subscription-manager repos --{{ item.state }} {{ item.name }}
redhat_repos:
repos: "{{ item.name }}"
state: "{{ item.state | default('present') }}"
with_items: "{{ rhsub_repos | default([]) }}"
when: rhsub_state == 'present'
tags:
- rhsub
- rhsub_repos

View File

@ -5,8 +5,10 @@
vars:
rhsub_state: present
rhsub_repos:
- name: rhel-7-server-extras-rpms # wildcard or repo name
state: enable
- name: rhel-7-server-extras-rpms
state: present
- name: rhel-7-server-rh-common-rpms
- name: rhel-7-server-openstack-8-rpms
roles:
- redhat-subscription