From 59c163b6ba3ccffd83620dbd263ce645e161363a Mon Sep 17 00:00:00 2001 From: Marius Rieder Date: Wed, 8 Jun 2022 18:16:40 +0200 Subject: [PATCH] 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 #4803 and #4804 --- plugins/modules/system/alternatives.py | 10 +++++----- .../targets/alternatives/tasks/test.yml | 2 +- .../alternatives/tasks/tests_set_priority.yml | 14 +++++++++++++- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/plugins/modules/system/alternatives.py b/plugins/modules/system/alternatives.py index 1fbc5ccddc2..c05e1248ff3 100644 --- a/plugins/modules/system/alternatives.py +++ b/plugins/modules/system/alternatives.py @@ -40,9 +40,8 @@ type: path priority: description: - - The priority of the alternative. + - The priority of the alternative. If no priority is given for creation 50 is used as a fallback. type: int - default: 50 state: description: - C(present) - install the alternative (if not already installed), but do @@ -171,9 +170,10 @@ def run(self): if self.mode_present: # Check if we need to (re)install subcommands_parameter = self.module.params['subcommands'] + priority_parameter = self.module.params['priority'] if ( self.path not in self.current_alternatives or - self.current_alternatives[self.path].get('priority') != self.priority or + (priority_parameter and self.current_alternatives[self.path].get('priority') != priority_parameter) or (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 self.current_alternatives[self.path].get('subcommands') for s in subcommands_parameter) @@ -273,7 +273,7 @@ def link(self): @property def priority(self): - return self.module.params.get('priority') + return self.module.params.get('priority') or self.current_alternatives.get(self.path, {}).get('priority') or 50 @property def subcommands(self): @@ -373,7 +373,7 @@ def main(): name=dict(type='str', required=True), path=dict(type='path', required=True), link=dict(type='path'), - priority=dict(type='int', default=50), + priority=dict(type='int'), state=dict( type='str', choices=AlternativeState.to_list(), diff --git a/tests/integration/targets/alternatives/tasks/test.yml b/tests/integration/targets/alternatives/tasks/test.yml index a5b36ce9224..a4d2f955028 100644 --- a/tests/integration/targets/alternatives/tasks/test.yml +++ b/tests/integration/targets/alternatives/tasks/test.yml @@ -48,4 +48,4 @@ when: ansible_os_family == 'RedHat' and not with_alternatives and item == 1 - 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'" diff --git a/tests/integration/targets/alternatives/tasks/tests_set_priority.yml b/tests/integration/targets/alternatives/tasks/tests_set_priority.yml index a629e3f3682..9572a729e75 100644 --- a/tests/integration/targets/alternatives/tasks/tests_set_priority.yml +++ b/tests/integration/targets/alternatives/tasks/tests_set_priority.yml @@ -31,4 +31,16 @@ register: alternative - name: check that alternative priority has been updated - command: "grep -Pzq '/bin/dummy{{ item }}\\n{{ 70 + item|int }}' '{{ alternatives_dir }}/dummy'" \ No newline at end of file + 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 triggerd without priority + assert: + that: + - 'alternative is not changed'