alternatives: Fix bug with priority default ()

* alternatives: Fix bug with priority default

If neigther the priority nor the subcommands where specified the module decided to update the priority with the default value anyway. This resulted in bug  and 

* Add changelog fragment.

* Distinguish None from 0.

* Address review comments.

* Update plugins/modules/system/alternatives.py

Co-authored-by: Pilou <pierre-louis@libregerbil.fr>

* Remove unrelated issues from changelog.

Co-authored-by: Felix Fontein <felix@fontein.de>
Co-authored-by: Pilou <pierre-louis@libregerbil.fr>
This commit is contained in:
Marius Rieder 2022-06-13 21:40:02 +02:00 committed by GitHub
parent 72faebffc6
commit 57e83ac80b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 7 deletions
changelogs/fragments
plugins/modules/system
tests/integration/targets/alternatives/tasks

View file

@ -0,0 +1,2 @@
bugfixes:
- "alternatives - do not set the priority if the priority was not set by the user (https://github.com/ansible-collections/community.general/pull/4810)."

View file

@ -40,9 +40,8 @@ options:
type: path type: path
priority: priority:
description: description:
- The priority of the alternative. - The priority of the alternative. If no priority is given for creation C(50) is used as a fallback.
type: int type: int
default: 50
state: state:
description: description:
- C(present) - install the alternative (if not already installed), but do - C(present) - install the alternative (if not already installed), but do
@ -171,9 +170,10 @@ class AlternativesModule(object):
if self.mode_present: if self.mode_present:
# Check if we need to (re)install # Check if we need to (re)install
subcommands_parameter = self.module.params['subcommands'] subcommands_parameter = self.module.params['subcommands']
priority_parameter = self.module.params['priority']
if ( if (
self.path not in self.current_alternatives or self.path not in self.current_alternatives or
self.current_alternatives[self.path].get('priority') != self.priority or (priority_parameter is not None and self.current_alternatives[self.path].get('priority') != priority_parameter) or
(subcommands_parameter is not None and ( (subcommands_parameter is not None and (
not all(s in subcommands_parameter for s in self.current_alternatives[self.path].get('subcommands')) or not all(s in subcommands_parameter for s in self.current_alternatives[self.path].get('subcommands')) or
not all(s in self.current_alternatives[self.path].get('subcommands') for s in subcommands_parameter) not all(s in self.current_alternatives[self.path].get('subcommands') for s in subcommands_parameter)
@ -273,7 +273,9 @@ class AlternativesModule(object):
@property @property
def priority(self): def priority(self):
if self.module.params.get('priority') is not None:
return self.module.params.get('priority') return self.module.params.get('priority')
return self.current_alternatives.get(self.path, {}).get('priority', 50)
@property @property
def subcommands(self): def subcommands(self):
@ -373,7 +375,7 @@ def main():
name=dict(type='str', required=True), name=dict(type='str', required=True),
path=dict(type='path', required=True), path=dict(type='path', required=True),
link=dict(type='path'), link=dict(type='path'),
priority=dict(type='int', default=50), priority=dict(type='int'),
state=dict( state=dict(
type='str', type='str',
choices=AlternativeState.to_list(), choices=AlternativeState.to_list(),

View file

@ -48,4 +48,4 @@
when: ansible_os_family == 'RedHat' and not with_alternatives and item == 1 when: ansible_os_family == 'RedHat' and not with_alternatives and item == 1
- name: check that alternative has been updated - name: check that alternative has been updated
command: "grep -Pzq '/bin/dummy{{ item }}\\n50' '{{ alternatives_dir }}/dummy'" command: "grep -Pzq '/bin/dummy{{ item }}\\n' '{{ alternatives_dir }}/dummy'"

View file

@ -32,3 +32,18 @@
- name: check that alternative priority has been updated - name: check that alternative priority has been updated
command: "grep -Pzq '/bin/dummy{{ item }}\\n{{ 70 + item|int }}' '{{ alternatives_dir }}/dummy'" command: "grep -Pzq '/bin/dummy{{ item }}\\n{{ 70 + item|int }}' '{{ alternatives_dir }}/dummy'"
- name: no change without priority
alternatives:
name: dummy
path: '/usr/bin/dummy{{ item }}'
link: /usr/bin/dummy
register: alternative
- name: check no change was triggered without priority
assert:
that:
- 'alternative is not changed'
- name: check that alternative priority has not been changed
command: "grep -Pzq '/bin/dummy{{ item }}\\n{{ 70 + item|int }}' '{{ alternatives_dir }}/dummy'"