diff --git a/changelogs/fragments/6134-add-locked-option-for-cargo.yml b/changelogs/fragments/6134-add-locked-option-for-cargo.yml new file mode 100644 index 0000000000..ed9f06864b --- /dev/null +++ b/changelogs/fragments/6134-add-locked-option-for-cargo.yml @@ -0,0 +1,2 @@ +minor_changes: + - "cargo - add option ``locked`` which allows user to specify install the locked version of dependency instead of latest compatible version (https://github.com/ansible-collections/community.general/pull/6134)." diff --git a/plugins/modules/cargo.py b/plugins/modules/cargo.py index a5fc7c1a59..f3109db844 100644 --- a/plugins/modules/cargo.py +++ b/plugins/modules/cargo.py @@ -44,6 +44,14 @@ options: try to install all of them in this version. type: str required: false + locked: + description: + - Install with locked dependencies. + - This is only used when installing packages. + required: false + type: bool + default: false + version_added: 7.5.0 state: description: - The state of the Rust package. @@ -60,6 +68,11 @@ EXAMPLES = r""" community.general.cargo: name: ludusavi +- name: Install "ludusavi" Rust package with locked dependencies + community.general.cargo: + name: ludusavi + locked: true + - name: Install "ludusavi" Rust package in version 0.10.0 community.general.cargo: name: ludusavi @@ -94,6 +107,7 @@ class Cargo(object): self.path = kwargs["path"] self.state = kwargs["state"] self.version = kwargs["version"] + self.locked = kwargs["locked"] self.executable = [module.get_bin_path("cargo", True)] @@ -132,6 +146,8 @@ class Cargo(object): def install(self, packages=None): cmd = ["install"] cmd.extend(packages or self.name) + if self.locked: + cmd.append("--locked") if self.path: cmd.append("--root") cmd.append(self.path) @@ -164,6 +180,7 @@ def main(): path=dict(default=None, type="path"), state=dict(default="present", choices=["present", "absent", "latest"]), version=dict(default=None, type="str"), + locked=dict(default=False, type="bool"), ) module = AnsibleModule(argument_spec=arg_spec, supports_check_mode=True) @@ -171,6 +188,7 @@ def main(): path = module.params["path"] state = module.params["state"] version = module.params["version"] + locked = module.params["locked"] if not name: module.fail_json(msg="Package name must be specified") @@ -180,7 +198,7 @@ def main(): LANG="C", LC_ALL="C", LC_MESSAGES="C", LC_CTYPE="C" ) - cargo = Cargo(module, name=name, path=path, state=state, version=version) + cargo = Cargo(module, name=name, path=path, state=state, version=version, locked=locked) changed, out, err = False, None, None installed_packages = cargo.get_installed() if state == "present":