Skip to content

Commit 326cd31

Browse files
authored
feat: remove hetzner.hcloud ansible dependency (#9)
* feat: remove hetzner.hcloud ansible dependency * refactor: rename variable hcloud_image_info to images
1 parent 381bc19 commit 326cd31

File tree

6 files changed

+51
-43
lines changed

6 files changed

+51
-43
lines changed

README.md

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@ Ansible role to create and rotate backups and snapshots of servers in Hetzner Cl
77
<!-- TOC -->
88

99
* [Requirements](#requirements)
10-
* [Dependencies](#dependencies)
1110
* [Role Variables](#role-variables)
1211
* [api_token](#api_token)
1312
* [Default Variables](#default-variables)
1413
* [Example Playbook](#example-playbook)
15-
* [Dependencies](#dependencies)
1614
* [License](#license)
1715
* [Author](#author)
1816

@@ -22,20 +20,6 @@ Ansible role to create and rotate backups and snapshots of servers in Hetzner Cl
2220

2321
- Ansible 2.15 or later
2422

25-
## Dependencies
26-
27-
This role depends on the following collections and must be installed before using this role:
28-
29-
- [hetzner.hcloud](https://galaxy.ansible.com/ui/repo/published/hetzner/hcloud/)
30-
31-
```bash
32-
ansible-galaxy collection install hetzner.hcloud
33-
```
34-
35-
Unfortunately, the ansible dependency system does not allow roles to
36-
depend on collections and vice versa even though it has been requested many times.
37-
Therefore, the collection has to be installed manually.
38-
3923
## Role Variables
4024

4125
### api_token

galaxy-requirements.yaml

Lines changed: 0 additions & 5 deletions
This file was deleted.

meta/main.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,4 @@ galaxy_info:
2828
versions:
2929
- all
3030

31-
#dependencies:
32-
# - name: hetzner.hcloud
33-
# src: hetzner.hcloud
34-
# version: ">=4.0.1"
31+
dependencies: []

tasks/main.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
- name: Validate variables
33
include_tasks: validate.yml
44

5-
- name: Create {{ backup_type }} for {{ inventory_hostname }}
5+
- name: Create {{ backup_type }}
66
delegate_to: "{{ delegation }}"
77
register: response
88
uri:
@@ -16,7 +16,7 @@
1616
status_code: 201
1717
body_format: json
1818

19-
- name: Wait until {{ backup_type }} is finished for {{ inventory_hostname }} with interval {{ backup_check_delay }}s
19+
- name: Wait until {{ backup_type }} is finished with interval {{ backup_check_delay }}s
2020
delegate_to: "{{ delegation }}"
2121
register: response
2222
uri:
@@ -31,6 +31,6 @@
3131
retries: "{{ backup_check_retries }}"
3232
delay: "{{ backup_check_delay }}"
3333

34-
- name: Rotate snapshots for {{ inventory_hostname }}
34+
- name: Rotate snapshots
3535
when: backup_type == "snapshot" and rotate_snapshots == true
3636
include_tasks: rotate-snapshots.yml

tasks/rotate-snapshots.yml

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,47 @@
11
---
2-
- name: 'Gather all snapshots with labels: "{{ label_selector }}"'
2+
- name: Prepare label selector filter
3+
set_fact:
4+
label_selector_string: "{{ label_selector | items | map('join', '=') | join(',') }}"
5+
6+
- name: Get number of pages for snapshots that match the label selector
37
delegate_to: "{{ delegation }}"
48
register: response
5-
hetzner.hcloud.hcloud_image_info:
6-
api_token: "{{ api_token }}"
7-
type: snapshot
8-
label_selector: "{{ label_selector | items | map('join', '=') | join(',') }}"
9-
failed_when: response.hcloud_image_info | length == 0
9+
uri:
10+
url: "https://api.hetzner.cloud/v1/images?type=snapshot&per_page=50&status=available&label_selector={{ label_selector_string }}"
11+
method: GET
12+
status_code: 200
13+
headers:
14+
Authorization: "Bearer {{ api_token }}"
1015

11-
- name: Gather snapshots to delete for {{ inventory_hostname }}
16+
- name: Gather all snapshots that match the label selector
1217
delegate_to: "{{ delegation }}"
18+
register: response
19+
uri:
20+
url: "https://api.hetzner.cloud/v1/images?type=snapshot&per_page=50&status=available&label_selector={{ label_selector_string }}&page={{ item }}"
21+
method: GET
22+
status_code: 200
23+
headers:
24+
Authorization: "Bearer {{ api_token }}"
25+
loop: "{{ range(1, response.json.meta.pagination.last_page + 1) | list }}"
26+
27+
- name: "Prepare list of snapshots"
1328
set_fact:
14-
delete_list: "{{ response.hcloud_image_info[0:response.hcloud_image_info | length - keep_snapshots | int] }}"
15-
when: response.hcloud_image_info | length - keep_snapshots | int > 0
29+
# workaround for selecting only snapshots created from the current host because label_selector has some problems:
30+
# selectattr('created_from.name', 'equalto', inventory_hostname)
31+
images: "{{ response.results | map(attribute='json.images') | flatten | selectattr('created_from.name', 'equalto', inventory_hostname) }}"
32+
33+
- name: Gather snapshots to delete
34+
delegate_to: "{{ delegation }}"
35+
set_fact:
36+
delete_list: "{{ images[0:images | length - keep_snapshots | int] }}"
37+
when: images | length - keep_snapshots | int > 0
38+
39+
- name: Show number of snapshots to delete
40+
debug:
41+
msg: "{{ delete_list | length }} of {{ images | length }} snapshots will be deleted"
42+
when: delete_list | length > 0
1643

17-
- name: 'Delete {{ delete_list | length }} old snapshots for {{ inventory_hostname }}'
44+
- name: "Delete old snapshots"
1845
delegate_to: "{{ delegation }}"
1946
when: delete_list | length > 0
2047
loop: "{{ delete_list }}"

tasks/validate.yml

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,20 @@
8787
block:
8888
- name: Get server details
8989
delegate_to: "{{ delegation }}"
90-
register: server
91-
hetzner.hcloud.server_info:
92-
api_token: "{{ api_token }}"
93-
name: "{{ inventory_hostname }}"
90+
register: response
91+
uri:
92+
url: "https://api.hetzner.cloud/v1/servers?name={{ inventory_hostname }}"
93+
method: GET
94+
status_code: 200
95+
headers:
96+
Authorization: "Bearer {{ api_token }}"
97+
failed_when:
98+
- response.json.servers | length == 0
9499

95100
- name: Check if backups are enabled
96101
delegate_to: "{{ delegation }}"
97102
assert:
98103
quiet: yes
99104
that:
100-
- server.hcloud_server_info[0].backup_window not in [None, ""]
105+
- response.json.servers.0.backup_window not in [None, ""]
101106
fail_msg: "Backups are not enabled on this server."

0 commit comments

Comments
 (0)