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

ansible_galaxy_install: add upgrade feature #8431

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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/8431-galaxy-upgrade.yml
Original file line number Diff line number Diff line change
@@ -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).
27 changes: 21 additions & 6 deletions plugins/modules/ansible_galaxy_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@
diff_mode:
support: none
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 using O(force=true) can be used to perform upgrade regardless of O(type).
russoz marked this conversation as resolved.
Show resolved Hide resolved
type: str
choices: [ present, latest ]
default: present
version_added: 9.1.0
type:
description:
- The type of installation performed by C(ansible-galaxy).
Expand Down Expand Up @@ -69,7 +82,8 @@
default: false
force:
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.
type: bool
default: false
Expand Down Expand Up @@ -188,6 +202,7 @@ class AnsibleGalaxyInstall(ModuleHelper):
output_params = ('type', 'name', 'dest', 'requirements_file', 'force', 'no_deps')
module = dict(
argument_spec=dict(
state=dict(type='str', choices=['present', 'latest'], default="present"),
russoz marked this conversation as resolved.
Show resolved Hide resolved
russoz marked this conversation as resolved.
Show resolved Hide resolved
type=dict(type='str', choices=('collection', 'role', 'both'), required=True),
name=dict(type='str'),
requirements_file=dict(type='path'),
Expand All @@ -206,6 +221,7 @@ class AnsibleGalaxyInstall(ModuleHelper):
command_args_formats = dict(
type=cmd_runner_fmt.as_func(lambda v: [] if v == 'both' else [v]),
galaxy_cmd=cmd_runner_fmt.as_list(),
upgrade=cmd_runner_fmt.as_bool("--upgrade"),
requirements_file=cmd_runner_fmt.as_opt_val('-r'),
dest=cmd_runner_fmt.as_opt_val('-p'),
force=cmd_runner_fmt.as_bool("--force"),
Expand Down Expand Up @@ -244,9 +260,7 @@ def process(rc, out, err):
def __init_module__(self):
self.runner, self.ansible_version = self._get_ansible_galaxy_version()
if self.ansible_version < (2, 11):
self.module.fail_json(
msg="Support for Ansible 2.9 and ansible-base 2.10 has been removed."
)
self.module.fail_json(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_roles", {}, change=True)
if self.vars.type != "collection":
Expand Down Expand Up @@ -299,8 +313,9 @@ def process(rc, out, err):
elif match.group("role"):
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:
ctx.run(galaxy_cmd="install")
upgrade = (self.vars.type == "collection" and self.vars.state == "latest")
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:
self.vars.set("run_info", ctx.run_info)

Expand Down
55 changes: 55 additions & 0 deletions tests/integration/targets/ansible_galaxy_install/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@
# 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
community.general.ansible_galaxy_install:
type: collection
name: netbox.netbox
dest: "{{ remote_tmp_dir }}/install_c"
register: install_c0

- name: Assert collection netbox.netbox was installed
Expand All @@ -20,6 +26,7 @@
community.general.ansible_galaxy_install:
type: collection
name: netbox.netbox
dest: "{{ remote_tmp_dir }}/install_c"
register: install_c1

- name: Assert collection was not installed
Expand All @@ -28,10 +35,16 @@
- 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
community.general.ansible_galaxy_install:
type: role
name: ansistrano.deploy
dest: "{{ remote_tmp_dir }}/install_r"
register: install_r0

- name: Assert collection ansistrano.deploy was installed
Expand All @@ -44,6 +57,7 @@
community.general.ansible_galaxy_install:
type: role
name: ansistrano.deploy
dest: "{{ remote_tmp_dir }}/install_r"
register: install_r1

- name: Assert role was not installed
Expand Down Expand Up @@ -86,3 +100,44 @@
assert:
that:
- 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
Loading