Skip to content

Commit

Permalink
Add slaves parameter for module alternatives. Bug #24278
Browse files Browse the repository at this point in the history
  • Loading branch information
jiuka committed Feb 21, 2019
1 parent 3cc4981 commit 45e0462
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 1 deletion.
24 changes: 23 additions & 1 deletion lib/ansible/modules/system/alternatives.py
Expand Up @@ -48,6 +48,12 @@
type: int
default: 50
version_added: "2.2"
slaves:
description:
- A list of slaves
- Each slave needs a name, a link and a path parameter
type: list
version_added: "2.8"
requirements: [ update-alternatives ]
'''

Expand All @@ -68,6 +74,16 @@
name: java
path: /usr/lib/jvm/java-7-openjdk-i386/jre/bin/java
priority: -10
- name: keytool is a slave of java
alternatives:
name: java
link: /usr/bin/java
path: /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java
slaves:
- name: keytool
link: /usr/bin/keytool
path: /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/keytool
'''

import os
Expand All @@ -85,6 +101,7 @@ def main():
path=dict(type='path', required=True),
link=dict(type='path'),
priority=dict(type='int', default=50),
slaves=dict(type='list'),
),
supports_check_mode=True,
)
Expand Down Expand Up @@ -144,8 +161,13 @@ def main():
if not link:
module.fail_json(msg="Needed to install the alternative, but unable to do so as we are missing the link")

cmd = [UPDATE_ALTERNATIVES, '--install', link, name, path, str(priority)]
if params['slaves']:
slaves = map(lambda slave: ['--slave', slave['link'], slave['name'], slave['path']], params['slaves'])
cmd += [item for sublist in slaves for item in sublist]

module.run_command(
[UPDATE_ALTERNATIVES, '--install', link, name, path, str(priority)],
cmd,
check_rc=True
)

Expand Down
12 changes: 12 additions & 0 deletions test/integration/targets/alternatives/tasks/main.yml
Expand Up @@ -44,6 +44,18 @@
# Test that path is checked: alternatives must fail when path is nonexistent
- import_tasks: path_is_checked.yml

##########
# Slaves
- block:
- include_tasks: remove_links.yml
- include_tasks: setup_test.yml
# at least two iterations again
- include_tasks: tests_slaves.yml
with_sequence: start=2 end=3
vars:
with_alternatives: False
mode: auto

always:
- include_tasks: remove_links.yml

Expand Down
8 changes: 8 additions & 0 deletions test/integration/targets/alternatives/tasks/setup.yml
Expand Up @@ -13,3 +13,11 @@
group: root
mode: 0755
with_sequence: start=1 end=4

- template:
src: dummy_command
dest: '/usr/bin/dummy{{ item }}'
owner: root
group: root
mode: 0755
with_sequence: start=1 end=4 format=slave%d
21 changes: 21 additions & 0 deletions test/integration/targets/alternatives/tasks/tests_slaves.yml
@@ -0,0 +1,21 @@
- name: update dummy alternative w/ slave
alternatives:
name: dummy
path: '/usr/bin/dummy{{ item }}'
link: /usr/bin/dummy
priority: '10'
slaves:
- name: dummyslave
path: '/usr/bin/dummyslave{{ item }}'
link: /usr/bin/dummyslave
register: alternative

- name: execute dummyslave command
shell: dummyslave
register: cmd

- name: check expected command was executed
assert:
that:
- 'alternative is changed'
- 'cmd.stdout == "dummyslave{{ item }}"'

0 comments on commit 45e0462

Please sign in to comment.