2013-03-23 16:30:02 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: rhn_channel
|
2013-03-25 16:03:38 +01:00
|
|
|
short_description: Adds or removes Red Hat software channels
|
2013-03-23 16:30:02 +01:00
|
|
|
description:
|
2013-03-25 16:03:38 +01:00
|
|
|
- Adds or removes Red Hat software channels
|
2013-03-30 20:44:34 +01:00
|
|
|
version_added: "1.1"
|
2013-03-23 16:30:02 +01:00
|
|
|
author: Vincent Van der Kussen
|
|
|
|
notes:
|
2013-03-25 16:03:38 +01:00
|
|
|
- this module fetches the system id from RHN.
|
2013-03-23 16:30:02 +01:00
|
|
|
requirements:
|
|
|
|
- none
|
|
|
|
options:
|
|
|
|
name:
|
2013-03-25 16:03:38 +01:00
|
|
|
description:
|
2013-03-23 16:30:02 +01:00
|
|
|
- name of the software channel
|
|
|
|
required: true
|
|
|
|
default: null
|
|
|
|
sysname:
|
|
|
|
description:
|
2013-03-25 16:03:38 +01:00
|
|
|
- name of the system as it is known in RHN/Satellite
|
2013-03-23 16:30:02 +01:00
|
|
|
required: true
|
|
|
|
default: null
|
2013-03-26 03:24:30 +01:00
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- whether the channel should be present or not
|
|
|
|
required: false
|
|
|
|
default: present
|
2013-03-23 16:30:02 +01:00
|
|
|
url:
|
|
|
|
description:
|
2013-03-25 16:03:38 +01:00
|
|
|
- The full url to the RHN/Satellite api
|
2013-03-23 16:30:02 +01:00
|
|
|
required: true
|
2013-03-23 22:04:48 +01:00
|
|
|
user:
|
|
|
|
description:
|
2013-03-25 16:03:38 +01:00
|
|
|
- RHN/Satellite user
|
2013-03-23 22:04:48 +01:00
|
|
|
required: true
|
2013-03-24 21:43:03 +01:00
|
|
|
password:
|
2013-03-23 22:04:48 +01:00
|
|
|
description:
|
2013-03-25 16:03:38 +01:00
|
|
|
- "the user's password"
|
2013-03-23 22:04:48 +01:00
|
|
|
required: true
|
2013-03-25 16:03:38 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
2013-06-14 11:53:43 +02:00
|
|
|
- rhn_channel: name=rhel-x86_64-server-v2vwin-6 sysname=server01 url=https://rhn.redhat.com/rpc/api user=rhnuser password=guessme
|
2013-03-23 16:30:02 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
import xmlrpclib
|
|
|
|
from operator import itemgetter
|
|
|
|
import re
|
|
|
|
|
|
|
|
|
|
|
|
# ------------------------------------------------------- #
|
|
|
|
|
|
|
|
def get_systemid(client, session, sysname):
|
|
|
|
systems = client.system.listUserSystems(session)
|
|
|
|
for system in systems:
|
|
|
|
if system.get('name') == sysname:
|
|
|
|
idres = system.get('id')
|
|
|
|
idd = int(idres)
|
|
|
|
return idd
|
|
|
|
|
|
|
|
# ------------------------------------------------------- #
|
|
|
|
|
2013-03-25 16:03:38 +01:00
|
|
|
# unused:
|
|
|
|
#
|
|
|
|
#def get_localsystemid():
|
|
|
|
# f = open("/etc/sysconfig/rhn/systemid", "r")
|
|
|
|
# content = f.read()
|
|
|
|
# loc_id = re.search(r'\b(ID-)(\d{10})' ,content)
|
|
|
|
# return loc_id.group(2)
|
2013-03-23 16:30:02 +01:00
|
|
|
|
|
|
|
# ------------------------------------------------------- #
|
|
|
|
|
|
|
|
def subscribe_channels(channels, client, session, sysname, sys_id):
|
|
|
|
c = base_channels(client, session, sys_id)
|
|
|
|
c.append(channels)
|
|
|
|
return client.channel.software.setSystemChannels(session, sys_id, c)
|
|
|
|
|
|
|
|
# ------------------------------------------------------- #
|
|
|
|
|
|
|
|
def unsubscribe_channels(channels, client, session, sysname, sys_id):
|
|
|
|
c = base_channels(client, session, sys_id)
|
|
|
|
c.remove(channels)
|
|
|
|
return client.channel.software.setSystemChannels(session, sys_id, c)
|
|
|
|
|
|
|
|
# ------------------------------------------------------- #
|
|
|
|
|
|
|
|
def base_channels(client, session, sys_id):
|
|
|
|
basechan = client.channel.software.listSystemChannels(session, sys_id)
|
2013-09-20 12:58:43 +02:00
|
|
|
try:
|
|
|
|
chans = [item['label'] for item in basechan]
|
|
|
|
except KeyError:
|
|
|
|
chans = [item['channel_label'] for item in basechan]
|
2013-03-23 16:30:02 +01:00
|
|
|
return chans
|
|
|
|
|
|
|
|
# ------------------------------------------------------- #
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec = dict(
|
|
|
|
state = dict(default='present', choices=['present', 'absent']),
|
|
|
|
name = dict(required=True),
|
|
|
|
sysname = dict(required=True),
|
|
|
|
url = dict(required=True),
|
2013-03-23 22:04:48 +01:00
|
|
|
user = dict(required=True),
|
2013-03-24 21:43:03 +01:00
|
|
|
password = dict(required=True, aliases=['pwd']),
|
2013-03-23 16:30:02 +01:00
|
|
|
)
|
|
|
|
# supports_check_mode=True
|
|
|
|
)
|
|
|
|
|
|
|
|
state = module.params['state']
|
|
|
|
channelname = module.params['name']
|
|
|
|
systname = module.params['sysname']
|
|
|
|
saturl = module.params['url']
|
2013-03-23 22:04:48 +01:00
|
|
|
user = module.params['user']
|
2013-03-24 21:43:03 +01:00
|
|
|
password = module.params['password']
|
2013-03-23 16:30:02 +01:00
|
|
|
|
|
|
|
#initialize connection
|
|
|
|
client = xmlrpclib.Server(saturl, verbose=0)
|
2013-03-24 21:43:03 +01:00
|
|
|
session = client.auth.login(user, password)
|
2013-03-23 16:30:02 +01:00
|
|
|
|
|
|
|
# get systemid
|
|
|
|
sys_id = get_systemid(client, session, systname)
|
|
|
|
|
|
|
|
# get channels for system
|
|
|
|
chans = base_channels(client, session, sys_id)
|
|
|
|
|
|
|
|
|
|
|
|
if state == 'present':
|
|
|
|
if channelname in chans:
|
|
|
|
module.exit_json(changed=False, msg="Channel %s already exists" % channelname)
|
|
|
|
else:
|
|
|
|
subscribe_channels(channelname, client, session, systname, sys_id)
|
|
|
|
module.exit_json(changed=True, msg="Channel %s added" % channelname)
|
|
|
|
|
|
|
|
if state == 'absent':
|
|
|
|
if not channelname in chans:
|
2013-03-23 22:04:48 +01:00
|
|
|
module.exit_json(changed=False, msg="Not subscribed to channel %s." % channelname)
|
2013-03-23 16:30:02 +01:00
|
|
|
else:
|
|
|
|
unsubscribe_channels(channelname, client, session, systname, sys_id)
|
|
|
|
module.exit_json(changed=True, msg="Channel %s removed" % channelname)
|
|
|
|
|
|
|
|
client.auth.logout(session)
|
|
|
|
|
|
|
|
|
2013-12-02 21:11:23 +01:00
|
|
|
# import module snippets
|
|
|
|
from ansible.module_utils.basic import *
|
2013-03-23 16:30:02 +01:00
|
|
|
main()
|