Skip to content

Commit

Permalink
Add tags to control what to execute
Browse files Browse the repository at this point in the history
  • Loading branch information
brianveltman committed Nov 10, 2024
1 parent c191649 commit 7aedc1c
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
- Added support for Roles API endpoint

### Changed
No changes in existing behaviour.
- Each part of the configuration can be ran seperately by specifing its tag. For example `--tags="roles,users,cleanup-policies"`

### Fixes
- API endpoints that require a pro license will be skipped when `nexus_enable_pro: false` is set.
Expand Down
1 change: 1 addition & 0 deletions tasks/cleanup-policies-api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@
body: "{{ item }}"
body_format: json
changed_when: true
tags: cleanup-policies
6 changes: 6 additions & 0 deletions tasks/license-api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
force_basic_auth: true
status_code: [200, 402]
register: __nexus_license_info__
tags: license

- name: show __nexus_license_info__
ansible.builtin.debug:
var: __nexus_license_info__
tags: license

# Nexus API expects a license file in binary format to be uploaded
# So we need to decode the base64 encoded license string into a file
Expand All @@ -23,6 +25,7 @@
mode: "0644"
register: __license_status__
when: (lookup('env', 'NEXUS_LICENSE_B64') is defined or nexus_license_b64 is defined) and nexus_enable_pro == true and __nexus_license_info__.json.effectiveDate is not defined
tags: license

- name: Upload license file through API
ansible.builtin.uri:
Expand All @@ -39,12 +42,14 @@
status_code: 200
register: __uploaded_license__
when: (lookup('env', 'NEXUS_LICENSE_B64') is defined or nexus_license_b64 is defined) and nexus_enable_pro == true and __nexus_license_info__.json.effectiveDate is not defined
tags: license

# After uploading the license file, we can remove it from the file system
- name: Remove license from file system
ansible.builtin.file:
path: "nexus.lic"
state: absent
tags: license

- name: Remove license
ansible.builtin.uri:
Expand All @@ -56,3 +61,4 @@
force_basic_auth: true
status_code: 204
when: (lookup('env', 'NEXUS_LICENSE_B64') is not defined or nexus_license_b64 is not defined) and nexus_enable_pro == false
tags: license
32 changes: 32 additions & 0 deletions tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
---
# include tasks from license-api.yml
- include_tasks: license-api.yml
tags: license

- include_tasks: security-anonymous-api.yml
tags: security-anonymous-access

- include_tasks: user-tokens-api.yml
when: nexus_enable_pro | bool
tags: user-tokens

- name: Initialize empty array for assets to be updated
set_fact:
Expand Down Expand Up @@ -197,14 +199,17 @@
force_basic_auth: true
status_code: 200
register: __nexus_existing_users__
tags: users

- name: Determine users to create
set_fact:
nexus_users_to_create: "{{ nexus_users | rejectattr('userId', 'in', __nexus_existing_users__.json | map(attribute='userId') | list) | list }}"
tags: users

- name: Determine users to delete
set_fact:
nexus_users_to_delete: "{{ __nexus_existing_users__.json | rejectattr('userId', 'in', nexus_users | map(attribute='userId') | list) | list }}"
tags: users

- name: Compare users for changes
set_fact:
Expand All @@ -220,18 +225,22 @@
( __nexus_existing_users__.json | selectattr('userId', 'equalto', item.userId) | first).status | default(omit) != item.status | default(omit) or
( __nexus_existing_users__.json | selectattr('userId', 'equalto', item.userId) | first).roles | default([]) | sort != item.roles | default([]) | sort or
( __nexus_existing_users__.json | selectattr('userId', 'equalto', item.userId) | first).externalRoles | default([]) | sort != item.externalRoles | default([]) | sort
tags: users

- name: Show nexus_users_to_create
debug:
var: nexus_users_to_create | length
tags: users

- name: Show nexus_users_to_update
debug:
var: nexus_users_to_update | length
tags: users

- name: Show nexus_users_to_delete
debug:
var: nexus_users_to_delete | length
tags: users

- name: Create configured Users using Nexus API
ansible.builtin.include_tasks: users-api.yml
Expand All @@ -241,6 +250,7 @@
with_items:
- "{{ nexus_users_to_create | default([]) }}"
when: nexus_users_to_create | length > 0
tags: users

- name: Update configured Users using Nexus API
ansible.builtin.include_tasks: users-api.yml
Expand All @@ -250,6 +260,7 @@
with_items:
- "{{ nexus_users_to_update | default([]) }}"
when: nexus_users_to_update | length > 0
tags: users

- name: Delete Users using Nexus API
ansible.builtin.include_tasks: users-api.yml
Expand All @@ -259,6 +270,7 @@
with_items:
- "{{ nexus_users_to_delete | default([]) }}"
when: nexus_users_to_delete | length > 0
tags: users

############ End Users ############

Expand All @@ -275,16 +287,19 @@
force_basic_auth: true
register: __nexus_cleanup_policies__
when: nexus_enable_pro | bool
tags: cleanup-policies

- name: Determine cleanup policies to create
set_fact:
nexus_repos_cleanup_policies_to_create: "{{ nexus_repos_cleanup_policies | rejectattr('name', 'in', __nexus_cleanup_policies__.json | map(attribute='name') | list) | list }}"
when: nexus_enable_pro | bool
tags: cleanup-policies

- name: Determine cleanup policies to delete
set_fact:
nexus_repos_cleanup_policies_to_delete: "{{ __nexus_cleanup_policies__.json | rejectattr('name', 'in', nexus_repos_cleanup_policies | map(attribute='name') | list) | list }}"
when: nexus_enable_pro | bool
tags: cleanup-policies

- name: Compare cleanup policies for changes
set_fact:
Expand All @@ -299,21 +314,25 @@
( __nexus_cleanup_policies__.json | selectattr('name', 'equalto', item.name) | first).criteriaReleaseType | default(omit) != item.criteriaReleaseType | default(None) or
( __nexus_cleanup_policies__.json | selectattr('name', 'equalto', item.name) | first).notes | default(omit) != item.notes | default(None) or
( __nexus_cleanup_policies__.json | selectattr('name', 'equalto', item.name) | first).format | default(omit) != item.format | default(None))
tags: cleanup-policies

- name: Show nexus_repos_cleanup_policies_to_create
debug:
var: nexus_repos_cleanup_policies_to_create | length
when: nexus_enable_pro | bool
tags: cleanup-policies

- name: Show nexus_repos_cleanup_policies_to_update
debug:
var: nexus_repos_cleanup_policies_to_update | length
when: nexus_enable_pro | bool
tags: cleanup-policies

- name: Show nexus_repos_cleanup_policies_to_delete
debug:
var: nexus_repos_cleanup_policies_to_delete | length
when: nexus_enable_pro | bool
tags: cleanup-policies

- name: Create configured Cleanup Policies using Nexus API
ansible.builtin.include_tasks: cleanup-policies-api.yml
Expand All @@ -323,6 +342,7 @@
with_items:
- "{{ nexus_repos_cleanup_policies_to_create | default([]) }}"
when: nexus_repos_cleanup_policies_to_create | length > 0 and nexus_enable_pro | bool
tags: cleanup-policies

- name: Update configured Cleanup Policies using Nexus API
ansible.builtin.include_tasks: cleanup-policies-api.yml
Expand All @@ -332,6 +352,7 @@
with_items:
- "{{ nexus_repos_cleanup_policies_to_update | default([]) }}"
when: nexus_repos_cleanup_policies_to_update | length > 0 and nexus_enable_pro | bool
tags: cleanup-policies

- name: Delete Cleanup Policies using Nexus API
ansible.builtin.include_tasks: cleanup-policies-api.yml
Expand All @@ -341,6 +362,7 @@
with_items:
- "{{ nexus_repos_cleanup_policies_to_delete | default([]) }}"
when: nexus_repos_cleanup_policies_to_delete | length > 0 and nexus_enable_pro | bool
tags: cleanup-policies

############ END Cleanup Policies ############

Expand All @@ -356,14 +378,17 @@
password: "{{ nexus_admin_password }}"
force_basic_auth: true
register: __nexus_routing_rules__
tags: routing-rules

- name: Determine routing rules to create
set_fact:
nexus_routing_rules_to_create: "{{ nexus_routing_rules | rejectattr('name', 'in', __nexus_routing_rules__.json | map(attribute='name') | list) | list }}"
tags: routing-rules

- name: Determine routing rules to delete
set_fact:
nexus_routing_rules_to_delete: "{{ __nexus_routing_rules__.json | rejectattr('name', 'in', nexus_routing_rules | map(attribute='name') | list) | list }}"
tags: routing-rules

- name: Compare routing rules for changes
set_fact:
Expand All @@ -374,18 +399,22 @@
( __nexus_routing_rules__.json | selectattr('name', 'equalto', item.name) | first).description | default(omit) != item.description | default(omit) or
( __nexus_routing_rules__.json | selectattr('name', 'equalto', item.name) | first).mode | default(omit) != item.mode | default(None) or
( __nexus_routing_rules__.json | selectattr('name', 'equalto', item.name) | first).matchers | default(omit) != item.matchers | default(omit)
tags: routing-rules

- name: Show nexus_routing_rules_to_create
debug:
var: nexus_routing_rules_to_create | length
tags: routing-rules

- name: Show nexus_routing_rules_to_update
debug:
var: nexus_routing_rules_to_update | length
tags: routing-rules

- name: Show nexus_routing_rules_to_delete
debug:
var: nexus_routing_rules_to_delete | length
tags: routing-rules

- name: Create configured Routing Rules using Nexus API
ansible.builtin.include_tasks: routing-rules-api.yml
Expand All @@ -395,6 +424,7 @@
with_items:
- "{{ nexus_routing_rules | default([]) }}"
when: nexus_routing_rules_to_create | length > 0
tags: routing-rules

- name: Update configured Routing Rules using Nexus API
ansible.builtin.include_tasks: routing-rules-api.yml
Expand All @@ -404,6 +434,7 @@
with_items:
- "{{ nexus_routing_rules_to_update | default([]) }}"
when: nexus_routing_rules_to_update | length > 0
tags: routing-rules

- name: Delete Routing Rules using Nexus API
ansible.builtin.include_tasks: routing-rules-api.yml
Expand All @@ -413,6 +444,7 @@
with_items:
- "{{ nexus_routing_rules_to_delete | default([]) }}"
when: nexus_routing_rules_to_delete | length > 0
tags: routing-rules

############ END Routing Rules ############

Expand Down
1 change: 1 addition & 0 deletions tasks/routing-rules-api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@
body: "{{ item }}"
body_format: json
changed_when: true
tags: routing-rules
4 changes: 4 additions & 0 deletions tasks/user-tokens-api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,18 @@
Accept: "application/json"
status_code: 200
register: __nexus_user_tokens__
tags: user-tokens

- name: Set fact for __nexus_user_tokens__
set_fact:
current_nexus_user_tokens: "{{ __nexus_user_tokens__.json | dict2items | sort(attribute='key') | items2dict }}"
when: __nexus_user_tokens__.status == 200
tags: user-tokens

- name: Set fact for nexus_user_tokens_capability
set_fact:
desired_nexus_user_tokens_capability: "{{ nexus_user_tokens_capability | dict2items | sort(attribute='key') | items2dict }}"
tags: user-tokens

- name: Configure User tokens capability
ansible.builtin.uri:
Expand All @@ -39,3 +42,4 @@
# The API always returns 200, even if the configuration is not changed
# So we need to check if the configuration is changed to be idempotent
changed_when: current_nexus_user_tokens != desired_nexus_user_tokens_capability
tags: user-tokens
2 changes: 2 additions & 0 deletions tasks/users-api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
{% else %}
{{ item | to_json }}
{% endif %}
tags: users

- name: "{{ method }} {{ item.userId }} user"
uri:
Expand All @@ -26,3 +27,4 @@
body: "{{ api_body }}"
body_format: json
changed_when: true
tags: users

0 comments on commit 7aedc1c

Please sign in to comment.