diff --git a/changelogs/fragments/8790-gitlab_project-fix-cleanup-policy-on-project-create.yml b/changelogs/fragments/8790-gitlab_project-fix-cleanup-policy-on-project-create.yml new file mode 100644 index 0000000000..ba171a1178 --- /dev/null +++ b/changelogs/fragments/8790-gitlab_project-fix-cleanup-policy-on-project-create.yml @@ -0,0 +1,3 @@ +bugfixes: + - gitlab_project - fix crash caused by old Gitlab projects not having a ``container_expiration_policy`` attribute (https://github.com/ansible-collections/community.general/pull/8790). + - gitlab_project - fix ``container_expiration_policy`` not being applied when creating a new project (https://github.com/ansible-collections/community.general/pull/8790). diff --git a/plugins/modules/gitlab_project.py b/plugins/modules/gitlab_project.py index c98e442cb0..87ad83dbc3 100644 --- a/plugins/modules/gitlab_project.py +++ b/plugins/modules/gitlab_project.py @@ -512,6 +512,8 @@ class GitLabProject(object): return True arguments['namespace_id'] = namespace.id + if 'container_expiration_policy' in arguments: + arguments['container_expiration_policy_attributes'] = arguments['container_expiration_policy'] try: project = self._gitlab.projects.create(arguments) except (gitlab.exceptions.GitlabCreateError) as e: @@ -539,9 +541,9 @@ class GitLabProject(object): for arg_key, arg_value in arguments.items(): if arguments[arg_key] is not None: - if getattr(project, arg_key) != arguments[arg_key]: + if getattr(project, arg_key, None) != arguments[arg_key]: if arg_key == 'container_expiration_policy': - old_val = getattr(project, arg_key) + old_val = getattr(project, arg_key, {}) final_val = {key: value for key, value in arg_value.items() if value is not None} if final_val.get('older_than') == '0d':