alternatives 4.18 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
Ansible module to manage symbolic link alternatives.
(c) 2014, Gabe Mulley <gabe.mulley@gmail.com>

This file is part of Ansible

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/>.
"""

DOCUMENTATION = '''
---
module: alternatives
27
short_description: Manages alternative programs for common commands
28 29 30
description:
    - Manages symbolic links using the 'update-alternatives' tool provided on debian-like systems.
    - Useful when multiple programs are installed but provide similar functionality (e.g. different editors).
31
version_added: "1.6"
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
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.
    required: false
requirements: [ update-alternatives ]
'''

EXAMPLES = '''
- name: correct java version selected
  alternatives: name=java path=/usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java

- name: alternatives link created
  alternatives: name=hadoop-conf link=/etc/hadoop/conf path=/etc/hadoop/conf.ansible
'''

UPDATE_ALTERNATIVES = '/usr/sbin/update-alternatives'
DEFAULT_LINK_PRIORITY = 50

def main():

    module = AnsibleModule(
        argument_spec = dict(
            name = dict(required=True),
            path  = dict(required=True),
            link = dict(required=False),
        )
    )

    params = module.params
    name = params['name']
    path = params['path']
    link = params['link']

    current_path = None
    all_alternatives = []

    (rc, query_output, query_error) = module.run_command(
        [UPDATE_ALTERNATIVES, '--query', name]
    )

    # Gather the current setting and all alternatives from the query output.
    # Query output should look something like this:

        # Name: java
        # Link: /usr/bin/java
        # Slaves:
        #  java.1.gz /usr/share/man/man1/java.1.gz
        # Status: manual
        # Best: /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java
        # Value: /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/java

        # Alternative: /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/java
        # Priority: 1061
        # Slaves:
        #  java.1.gz /usr/lib/jvm/java-6-openjdk-amd64/jre/man/man1/java.1.gz

        # Alternative: /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java
        # Priority: 1071
        # Slaves:
        #  java.1.gz /usr/lib/jvm/java-7-openjdk-amd64/jre/man/man1/java.1.gz

    if rc == 0:
        for line in query_output.splitlines():
            split_line = line.split(':')
            if len(split_line) == 2:
                key = split_line[0]
                value = split_line[1].strip()
                if key == 'Value':
                    current_path = value
                elif key == 'Alternative':
                    all_alternatives.append(value)

    if current_path != path:
        try:
            # install the requested path if necessary
            if path not in all_alternatives:
                module.run_command(
                    [UPDATE_ALTERNATIVES, '--install', link, name, path, str(DEFAULT_LINK_PRIORITY)],
                    check_rc=True
                )

            # select the requested path
            module.run_command(
                [UPDATE_ALTERNATIVES, '--set', name, path],
                check_rc=True
            )

            module.exit_json(changed=True)
        except subprocess.CalledProcessError as cpe:
            module.fail_json(msg=str(dir(cpe)))
    else:
        module.exit_json(changed=False)


# import module snippets
from ansible.module_utils.basic import *
main()