2014-09-26 01:01:01 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
Remove wildcard imports
Made the following changes:
* Removed wildcard imports
* Replaced long form of GPL header with short form
* Removed get_exception usage
* Added from __future__ boilerplate
* Adjust division operator to // where necessary
For the following files:
* web_infrastructure modules
* system modules
* linode, lxc, lxd, atomic, cloudscale, dimensiondata, ovh, packet,
profitbricks, pubnub, smartos, softlayer, univention modules
* compat dirs (disabled as its used intentionally)
2017-07-28 05:55:24 +00:00
|
|
|
# (c) 2014, Gabe Mulley <gabe.mulley@gmail.com>
|
|
|
|
# (c) 2015, David Wittman <dwittman@gmail.com>
|
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
2014-09-26 01:01:01 +00:00
|
|
|
|
Remove wildcard imports
Made the following changes:
* Removed wildcard imports
* Replaced long form of GPL header with short form
* Removed get_exception usage
* Added from __future__ boilerplate
* Adjust division operator to // where necessary
For the following files:
* web_infrastructure modules
* system modules
* linode, lxc, lxd, atomic, cloudscale, dimensiondata, ovh, packet,
profitbricks, pubnub, smartos, softlayer, univention modules
* compat dirs (disabled as its used intentionally)
2017-07-28 05:55:24 +00:00
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
2014-09-26 01:01:01 +00:00
|
|
|
|
|
|
|
|
2017-08-16 03:16:38 +00:00
|
|
|
ANSIBLE_METADATA = {'metadata_version': '1.1',
|
2017-03-14 16:07:22 +00:00
|
|
|
'status': ['preview'],
|
|
|
|
'supported_by': 'community'}
|
|
|
|
|
2016-12-06 10:35:25 +00:00
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: alternatives
|
|
|
|
short_description: Manages alternative programs for common commands
|
|
|
|
description:
|
2015-03-14 02:09:33 +00:00
|
|
|
- Manages symbolic links using the 'update-alternatives' tool
|
2014-09-26 01:01:01 +00:00
|
|
|
- Useful when multiple programs are installed but provide similar functionality (e.g. different editors).
|
|
|
|
version_added: "1.6"
|
2015-05-14 13:37:00 +00:00
|
|
|
author:
|
2015-06-16 18:32:39 +00:00
|
|
|
- "David Wittman (@DavidWittman)"
|
|
|
|
- "Gabe Mulley (@mulby)"
|
2014-09-26 01:01:01 +00:00
|
|
|
options:
|
|
|
|
name:
|
|
|
|
description:
|
|
|
|
- The generic name of the link.
|
|
|
|
required: true
|
|
|
|
path:
|
|
|
|
description:
|
|
|
|
- The path to the real executable that the link should point to.
|
|
|
|
required: true
|
|
|
|
link:
|
|
|
|
description:
|
|
|
|
- The path to the symbolic link that should point to the real executable.
|
2017-08-16 01:35:09 +00:00
|
|
|
- This option is always required on RHEL-based distributions. On Debian-based distributions this option is
|
|
|
|
required when the alternative I(name) is unknown to the system.
|
2014-09-26 01:01:01 +00:00
|
|
|
required: false
|
2016-07-09 07:08:44 +00:00
|
|
|
priority:
|
|
|
|
description:
|
|
|
|
- The priority of the alternative
|
|
|
|
required: false
|
2016-07-09 07:11:24 +00:00
|
|
|
default: 50
|
|
|
|
version_added: "2.2"
|
2014-09-26 01:01:01 +00:00
|
|
|
requirements: [ update-alternatives ]
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
- name: correct java version selected
|
2016-11-11 22:40:07 +00:00
|
|
|
alternatives:
|
|
|
|
name: java
|
|
|
|
path: /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java
|
2014-09-26 01:01:01 +00:00
|
|
|
|
|
|
|
- name: alternatives link created
|
2016-11-11 22:40:07 +00:00
|
|
|
alternatives:
|
|
|
|
name: hadoop-conf
|
|
|
|
link: /etc/hadoop/conf
|
|
|
|
path: /etc/hadoop/conf.ansible
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2016-07-09 07:08:44 +00:00
|
|
|
- name: make java 32 bit an alternative with low priority
|
2016-11-11 22:40:07 +00:00
|
|
|
alternatives:
|
|
|
|
name: java
|
|
|
|
path: /usr/lib/jvm/java-7-openjdk-i386/jre/bin/java
|
|
|
|
priority: -10
|
2016-07-09 07:08:44 +00:00
|
|
|
'''
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2015-03-14 02:09:33 +00:00
|
|
|
import re
|
2017-07-23 01:15:46 +00:00
|
|
|
import subprocess
|
|
|
|
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
2016-06-03 13:23:55 +00:00
|
|
|
|
2015-03-14 02:09:33 +00:00
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
def main():
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec = dict(
|
|
|
|
name = dict(required=True),
|
2016-03-19 23:41:14 +00:00
|
|
|
path = dict(required=True, type='path'),
|
|
|
|
link = dict(required=False, type='path'),
|
2016-07-09 07:08:44 +00:00
|
|
|
priority = dict(required=False, type='int',
|
|
|
|
default=50),
|
2014-09-22 16:54:00 +00:00
|
|
|
),
|
|
|
|
supports_check_mode=True,
|
2014-09-26 01:01:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
params = module.params
|
|
|
|
name = params['name']
|
|
|
|
path = params['path']
|
|
|
|
link = params['link']
|
2016-07-09 07:08:44 +00:00
|
|
|
priority = params['priority']
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2015-03-14 02:09:33 +00:00
|
|
|
UPDATE_ALTERNATIVES = module.get_bin_path('update-alternatives',True)
|
2014-09-26 01:01:01 +00:00
|
|
|
|
|
|
|
current_path = None
|
|
|
|
all_alternatives = []
|
|
|
|
|
2015-03-14 02:09:33 +00:00
|
|
|
# Run `update-alternatives --display <name>` to find existing alternatives
|
|
|
|
(rc, display_output, _) = module.run_command(
|
2015-05-24 00:05:38 +00:00
|
|
|
['env', 'LC_ALL=C', UPDATE_ALTERNATIVES, '--display', name]
|
2014-09-26 01:01:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if rc == 0:
|
2015-03-14 02:09:33 +00:00
|
|
|
# Alternatives already exist for this link group
|
|
|
|
# Parse the output to determine the current path of the symlink and
|
|
|
|
# available alternatives
|
|
|
|
current_path_regex = re.compile(r'^\s*link currently points to (.*)$',
|
|
|
|
re.MULTILINE)
|
|
|
|
alternative_regex = re.compile(r'^(\/.*)\s-\spriority', re.MULTILINE)
|
|
|
|
|
|
|
|
current_path = current_path_regex.search(display_output).group(1)
|
|
|
|
all_alternatives = alternative_regex.findall(display_output)
|
|
|
|
|
|
|
|
if not link:
|
|
|
|
# Read the current symlink target from `update-alternatives --query`
|
|
|
|
# in case we need to install the new alternative before setting it.
|
|
|
|
#
|
|
|
|
# This is only compatible on Debian-based systems, as the other
|
|
|
|
# alternatives don't have --query available
|
|
|
|
rc, query_output, _ = module.run_command(
|
2015-05-24 00:05:38 +00:00
|
|
|
['env', 'LC_ALL=C', UPDATE_ALTERNATIVES, '--query', name]
|
2015-03-14 02:09:33 +00:00
|
|
|
)
|
|
|
|
if rc == 0:
|
|
|
|
for line in query_output.splitlines():
|
|
|
|
if line.startswith('Link:'):
|
|
|
|
link = line.split()[1]
|
|
|
|
break
|
2014-09-26 01:01:01 +00:00
|
|
|
|
|
|
|
if current_path != path:
|
2014-09-22 16:54:00 +00:00
|
|
|
if module.check_mode:
|
|
|
|
module.exit_json(changed=True, current_path=current_path)
|
2014-09-26 01:01:01 +00:00
|
|
|
try:
|
|
|
|
# install the requested path if necessary
|
2015-03-14 02:09:33 +00:00
|
|
|
if path not in all_alternatives:
|
|
|
|
if not link:
|
|
|
|
module.fail_json(msg="Needed to install the alternative, but unable to do so as we are missing the link")
|
|
|
|
|
|
|
|
module.run_command(
|
2016-07-09 07:08:44 +00:00
|
|
|
[UPDATE_ALTERNATIVES, '--install', link, name, path, str(priority)],
|
2015-03-14 02:09:33 +00:00
|
|
|
check_rc=True
|
|
|
|
)
|
2014-09-26 01:01:01 +00:00
|
|
|
|
|
|
|
# select the requested path
|
|
|
|
module.run_command(
|
|
|
|
[UPDATE_ALTERNATIVES, '--set', name, path],
|
|
|
|
check_rc=True
|
|
|
|
)
|
|
|
|
|
|
|
|
module.exit_json(changed=True)
|
2017-07-23 01:15:46 +00:00
|
|
|
except subprocess.CalledProcessError as cpe:
|
2014-09-26 01:01:01 +00:00
|
|
|
module.fail_json(msg=str(dir(cpe)))
|
|
|
|
else:
|
|
|
|
module.exit_json(changed=False)
|
|
|
|
|
2016-11-11 22:40:07 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|