Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add subcommands parameter for module alternatives. #4654

Merged
merged 28 commits into from
Jun 6, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
fad8c09
Add slaves parameter for module alternatives.
jiuka May 9, 2022
b7606a5
alternatives: Improve documentation abous slaves parameter
jiuka May 9, 2022
88a00ae
alternatives: Apply suggestions from code review
jiuka May 10, 2022
fc2b694
alternatives: Add schangelog for slaves parameter
jiuka May 10, 2022
910e7fe
alernatives: Add integration tests
jiuka May 10, 2022
b3658ed
alternatives: Improv tests
jiuka May 11, 2022
197e98c
alternatives: Update tests/integration/targets/alternatives/tasks/sla…
jiuka May 11, 2022
81cf47d
alternatives: Rework logic to support updating priority and subcommands
jiuka May 12, 2022
4516c88
alternatives: Use more inclusive naming
jiuka May 12, 2022
8ee5cfd
alternatives: Fix linter warnings
jiuka May 12, 2022
7ff3983
alternatives: Dont fail if link is absent
jiuka May 12, 2022
e9a128f
alternatives: Update changelog fragment
jiuka May 12, 2022
c242382
alternatives: Add tests for prio change and removing
jiuka May 13, 2022
63e1d2c
alternatives: Apply suggestions from code review
jiuka May 13, 2022
aaf2a83
alternatives: Add `state=auto`to reset mode to auto
jiuka May 13, 2022
87b9f66
alternatives: Fix linter warnings
jiuka May 13, 2022
708ef3e
alternatives: Fix documentation.
jiuka May 14, 2022
851be5e
alternatives: Combine multiple messages.
jiuka May 14, 2022
56a09b7
alternatives: Set command env for all commands.
jiuka May 14, 2022
6ef4b5f
alternatives: Do not update subcommands if parameter is omited
jiuka May 14, 2022
66a7b3d
alternatives: Fix a bug with python 2.7 var scoping
jiuka May 15, 2022
e2751ce
alternatives: Improce diff before generation
jiuka May 15, 2022
1935016
alternatives: Fix linter warnings
jiuka May 15, 2022
b30160b
alternatives: Fix test names
jiuka May 16, 2022
8ed1836
alternatives: Simplify subcommands handling and improve diffs
jiuka May 16, 2022
8c2fa33
aliases: Only test for subcommand changes if subcommands parameter is…
jiuka May 16, 2022
069eed7
Update plugins/modules/system/alternatives.py
jiuka May 17, 2022
abd45ff
Apply suggestions from code review
felixfontein May 30, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/4654-alternatives-add-slaves.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- alternatives - add ``slaves`` parameter (https://github.com/ansible-collections/community.general/pull/4654).
44 changes: 43 additions & 1 deletion plugins/modules/system/alternatives.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,28 @@
default: selected
type: str
version_added: 4.8.0
slaves:
description:
- A list of slaves.
- Each slave needs a name, a link and a path parameter.
type: list
elements: dict
suboptions:
name:
description:
- The generic name of the slave.
type: str
required: true
path:
description:
- The path to the real executable that the slave should point to.
type: path
required: true
link:
description:
- The path to the symbolic link that should point to the real slave executable.
type: path
version_added: 5.0.0
felixfontein marked this conversation as resolved.
Show resolved Hide resolved
requirements: [ update-alternatives ]
'''

Expand Down Expand Up @@ -78,6 +100,16 @@
path: /usr/bin/python3.5
link: /usr/bin/python
state: present

- name: keytool is a slave of java
community.general.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 Down Expand Up @@ -109,6 +141,11 @@ def main():
choices=AlternativeState.to_list(),
default=AlternativeState.SELECTED,
),
slaves=dict(type='list', elements='dict', options=dict(
name=dict(type='str', required=True),
path=dict(type='path', required=True),
link=dict(type='path'),
)),
),
supports_check_mode=True,
)
Expand Down Expand Up @@ -180,8 +217,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 = [['--slave', slave['link'], slave['name'], slave['path']] for slave in 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
)
changed = True
Expand Down
3 changes: 3 additions & 0 deletions tests/integration/targets/alternatives/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
# Test that path is checked: alternatives must fail when path is nonexistent
- import_tasks: path_is_checked.yml

# Test that slave commands work
- import_tasks: slaves.yml

felixfontein marked this conversation as resolved.
Show resolved Hide resolved
# Test operation of the 'state' parameter
- block:
- include_tasks: remove_links.yml
Expand Down
34 changes: 34 additions & 0 deletions tests/integration/targets/alternatives/tasks/slaves.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
- name: Try with slaves
alternatives:
name: dummymain
path: '/usr/bin/dummy1'
link: '/usr/bin/dummymain'
slaves:
- name: dummyslave
felixfontein marked this conversation as resolved.
Show resolved Hide resolved
path: '/usr/bin/dummy2'
link: '/usr/bin/dummyslave'
felixfontein marked this conversation as resolved.
Show resolved Hide resolved
register: alternative

- name: Check expected command was executed
assert:
that:
- 'alternative is successful'
jiuka marked this conversation as resolved.
Show resolved Hide resolved
- 'alternative is changed'

- name: Execute the current dummymain command
command: dummymain
register: cmd

- name: Ensure that the expected command was executed
assert:
that:
- cmd.stdout == "dummy1"

- name: Execute the current dummyslave command
command: dummyslave
felixfontein marked this conversation as resolved.
Show resolved Hide resolved
register: cmd

- name: Ensure that the expected command was executed
assert:
that:
- cmd.stdout == "dummy2"