commit
b68f81a727
1 changed files with 146 additions and 0 deletions
146
library/rhn_channel
Executable file
146
library/rhn_channel
Executable file
|
@ -0,0 +1,146 @@
|
||||||
|
#!/usr/bin/python
|
||||||
|
|
||||||
|
DOCUMENTATION = '''
|
||||||
|
---
|
||||||
|
module: rhn_channel
|
||||||
|
short_description: Define Red Hat software channels
|
||||||
|
description:
|
||||||
|
- Adds or removes Red Hat software channels on a system
|
||||||
|
version_added: 1.0
|
||||||
|
author: Vincent Van der Kussen
|
||||||
|
notes:
|
||||||
|
- this module fetches the systemid from rhn. A function
|
||||||
|
to use the local systemid is provided (get_localsystem)
|
||||||
|
but not integrated
|
||||||
|
requirements:
|
||||||
|
- none
|
||||||
|
options:
|
||||||
|
name:
|
||||||
|
description
|
||||||
|
- name of the software channel
|
||||||
|
required: true
|
||||||
|
default: null
|
||||||
|
sysname:
|
||||||
|
description:
|
||||||
|
- name of the system as it is known in rhn/sattelite
|
||||||
|
required: true
|
||||||
|
default: null
|
||||||
|
url:
|
||||||
|
description:
|
||||||
|
- The full url to the rhn/sattelite api
|
||||||
|
required: true
|
||||||
|
user:
|
||||||
|
description:
|
||||||
|
- the user to login to rhn/sattelite
|
||||||
|
required: true
|
||||||
|
pwd:
|
||||||
|
description:
|
||||||
|
- the user's password
|
||||||
|
required: true
|
||||||
|
examples:
|
||||||
|
- code: rhn_channel name=rhel-x86_64-server-v2vwin-6 sysname=server01
|
||||||
|
url=https://rhn.redhat.com/rpc/api user=rhnuser pwd=guessme
|
||||||
|
description: add software channel rhel-x86_64-server-v2vwin-6
|
||||||
|
to server01 in Red Hat Network
|
||||||
|
'''
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
# ------------------------------------------------------- #
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
# ------------------------------------------------------- #
|
||||||
|
|
||||||
|
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)
|
||||||
|
chans = [item['channel_label'] for item in basechan]
|
||||||
|
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),
|
||||||
|
user = dict(required=True),
|
||||||
|
pwd = dict(required=True),
|
||||||
|
)
|
||||||
|
# supports_check_mode=True
|
||||||
|
)
|
||||||
|
|
||||||
|
state = module.params['state']
|
||||||
|
channelname = module.params['name']
|
||||||
|
systname = module.params['sysname']
|
||||||
|
saturl = module.params['url']
|
||||||
|
user = module.params['user']
|
||||||
|
pwd = module.params['pwd']
|
||||||
|
|
||||||
|
#initialize connection
|
||||||
|
client = xmlrpclib.Server(saturl, verbose=0)
|
||||||
|
session = client.auth.login(user, pwd)
|
||||||
|
|
||||||
|
# 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:
|
||||||
|
module.exit_json(changed=False, msg="Not subscribed to channel %s." % channelname)
|
||||||
|
else:
|
||||||
|
unsubscribe_channels(channelname, client, session, systname, sys_id)
|
||||||
|
module.exit_json(changed=True, msg="Channel %s removed" % channelname)
|
||||||
|
|
||||||
|
client.auth.logout(session)
|
||||||
|
|
||||||
|
|
||||||
|
# include magic from lib/ansible/module_common.py
|
||||||
|
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
|
||||||
|
main()
|
Loading…
Reference in a new issue