1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

ansible_galaxy_install: add upgrade feature (#8431)

* add upgrade feature

* add changelog frag

* Update plugins/modules/ansible_galaxy_install.py

* Update plugins/modules/ansible_galaxy_install.py
This commit is contained in:
Alexei Znamensky 2024-06-02 10:17:26 +12:00 committed by GitHub
parent 5a5188a453
commit d46e12e280
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 78 additions and 6 deletions

View file

@ -0,0 +1,2 @@
minor_changes:
- ansible_galaxy_install - add upgrade feature (https://github.com/ansible-collections/community.general/pull/8431, https://github.com/ansible-collections/community.general/issues/8351).

View file

@ -32,6 +32,19 @@ attributes:
diff_mode: diff_mode:
support: none support: none
options: options:
state:
description:
- >
If O(state=present) then the collection or role will be installed.
Note that the collections and roles are not updated with this option.
- >
Currently the O(state=latest) is ignored unless O(type=collection), and it will
ensure the collection is installed and updated to the latest available version.
- Please note that O(force=true) can be used to perform upgrade regardless of O(type).
type: str
choices: [ present, latest ]
default: present
version_added: 9.1.0
type: type:
description: description:
- The type of installation performed by C(ansible-galaxy). - The type of installation performed by C(ansible-galaxy).
@ -69,7 +82,8 @@ options:
default: false default: false
force: force:
description: description:
- Force overwriting an existing role or collection. - Force overwriting existing roles and/or collections.
- It can be used for upgrading, but the module output will always report C(changed=true).
- Using O(force=true) is mandatory when downgrading. - Using O(force=true) is mandatory when downgrading.
type: bool type: bool
default: false default: false
@ -188,6 +202,7 @@ class AnsibleGalaxyInstall(ModuleHelper):
output_params = ('type', 'name', 'dest', 'requirements_file', 'force', 'no_deps') output_params = ('type', 'name', 'dest', 'requirements_file', 'force', 'no_deps')
module = dict( module = dict(
argument_spec=dict( argument_spec=dict(
state=dict(type='str', choices=['present', 'latest'], default='present'),
type=dict(type='str', choices=('collection', 'role', 'both'), required=True), type=dict(type='str', choices=('collection', 'role', 'both'), required=True),
name=dict(type='str'), name=dict(type='str'),
requirements_file=dict(type='path'), requirements_file=dict(type='path'),
@ -206,6 +221,7 @@ class AnsibleGalaxyInstall(ModuleHelper):
command_args_formats = dict( command_args_formats = dict(
type=cmd_runner_fmt.as_func(lambda v: [] if v == 'both' else [v]), type=cmd_runner_fmt.as_func(lambda v: [] if v == 'both' else [v]),
galaxy_cmd=cmd_runner_fmt.as_list(), galaxy_cmd=cmd_runner_fmt.as_list(),
upgrade=cmd_runner_fmt.as_bool("--upgrade"),
requirements_file=cmd_runner_fmt.as_opt_val('-r'), requirements_file=cmd_runner_fmt.as_opt_val('-r'),
dest=cmd_runner_fmt.as_opt_val('-p'), dest=cmd_runner_fmt.as_opt_val('-p'),
force=cmd_runner_fmt.as_bool("--force"), force=cmd_runner_fmt.as_bool("--force"),
@ -244,9 +260,7 @@ class AnsibleGalaxyInstall(ModuleHelper):
def __init_module__(self): def __init_module__(self):
self.runner, self.ansible_version = self._get_ansible_galaxy_version() self.runner, self.ansible_version = self._get_ansible_galaxy_version()
if self.ansible_version < (2, 11): if self.ansible_version < (2, 11):
self.module.fail_json( self.module.fail_json(msg="Support for Ansible 2.9 and ansible-base 2.10 has been removed.")
msg="Support for Ansible 2.9 and ansible-base 2.10 has been removed."
)
self.vars.set("new_collections", {}, change=True) self.vars.set("new_collections", {}, change=True)
self.vars.set("new_roles", {}, change=True) self.vars.set("new_roles", {}, change=True)
if self.vars.type != "collection": if self.vars.type != "collection":
@ -299,8 +313,9 @@ class AnsibleGalaxyInstall(ModuleHelper):
elif match.group("role"): elif match.group("role"):
self.vars.new_roles[match.group("role")] = match.group("rversion") self.vars.new_roles[match.group("role")] = match.group("rversion")
with self.runner("type galaxy_cmd force no_deps dest requirements_file name", output_process=process) as ctx: upgrade = (self.vars.type == "collection" and self.vars.state == "latest")
ctx.run(galaxy_cmd="install") with self.runner("type galaxy_cmd upgrade force no_deps dest requirements_file name", output_process=process) as ctx:
ctx.run(galaxy_cmd="install", upgrade=upgrade)
if self.verbosity > 2: if self.verbosity > 2:
self.vars.set("run_info", ctx.run_info) self.vars.set("run_info", ctx.run_info)

View file

@ -4,10 +4,16 @@
# SPDX-License-Identifier: GPL-3.0-or-later # SPDX-License-Identifier: GPL-3.0-or-later
################################################### ###################################################
- name: Make directory install_c
ansible.builtin.file:
path: "{{ remote_tmp_dir }}/install_c"
state: directory
- name: Install collection netbox.netbox - name: Install collection netbox.netbox
community.general.ansible_galaxy_install: community.general.ansible_galaxy_install:
type: collection type: collection
name: netbox.netbox name: netbox.netbox
dest: "{{ remote_tmp_dir }}/install_c"
register: install_c0 register: install_c0
- name: Assert collection netbox.netbox was installed - name: Assert collection netbox.netbox was installed
@ -20,6 +26,7 @@
community.general.ansible_galaxy_install: community.general.ansible_galaxy_install:
type: collection type: collection
name: netbox.netbox name: netbox.netbox
dest: "{{ remote_tmp_dir }}/install_c"
register: install_c1 register: install_c1
- name: Assert collection was not installed - name: Assert collection was not installed
@ -28,10 +35,16 @@
- install_c1 is not changed - install_c1 is not changed
################################################### ###################################################
- name: Make directory install_r
ansible.builtin.file:
path: "{{ remote_tmp_dir }}/install_r"
state: directory
- name: Install role ansistrano.deploy - name: Install role ansistrano.deploy
community.general.ansible_galaxy_install: community.general.ansible_galaxy_install:
type: role type: role
name: ansistrano.deploy name: ansistrano.deploy
dest: "{{ remote_tmp_dir }}/install_r"
register: install_r0 register: install_r0
- name: Assert collection ansistrano.deploy was installed - name: Assert collection ansistrano.deploy was installed
@ -44,6 +57,7 @@
community.general.ansible_galaxy_install: community.general.ansible_galaxy_install:
type: role type: role
name: ansistrano.deploy name: ansistrano.deploy
dest: "{{ remote_tmp_dir }}/install_r"
register: install_r1 register: install_r1
- name: Assert role was not installed - name: Assert role was not installed
@ -86,3 +100,44 @@
assert: assert:
that: that:
- install_rq1 is not changed - install_rq1 is not changed
###################################################
- name: Make directory upgrade_c
ansible.builtin.file:
path: "{{ remote_tmp_dir }}/upgrade_c"
state: directory
- name: Install collection netbox.netbox 3.17.0
community.general.ansible_galaxy_install:
type: collection
name: netbox.netbox:3.17.0
dest: "{{ remote_tmp_dir }}/upgrade_c"
register: upgrade_c0
- name: Assert collection netbox.netbox was installed
assert:
that:
- upgrade_c0 is changed
- '"netbox.netbox" in upgrade_c0.new_collections'
- name: Upgrade collection netbox.netbox
community.general.ansible_galaxy_install:
state: latest
type: collection
name: netbox.netbox
dest: "{{ remote_tmp_dir }}/upgrade_c"
register: upgrade_c1
- name: Upgrade collection netbox.netbox (again)
community.general.ansible_galaxy_install:
state: latest
type: collection
name: netbox.netbox
dest: "{{ remote_tmp_dir }}/upgrade_c"
register: upgrade_c2
- name: Assert collection was not installed
assert:
that:
- upgrade_c1 is changed
- upgrade_c2 is not changed