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

feat: remove hetzner.hcloud ansible dependency #9

Merged
merged 2 commits into from
Jun 30, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 0 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ Ansible role to create and rotate backups and snapshots of servers in Hetzner Cl
<!-- TOC -->

* [Requirements](#requirements)
* [Dependencies](#dependencies)
* [Role Variables](#role-variables)
* [api_token](#api_token)
* [Default Variables](#default-variables)
* [Example Playbook](#example-playbook)
* [Dependencies](#dependencies)
* [License](#license)
* [Author](#author)

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

- Ansible 2.15 or later

## Dependencies

This role depends on the following collections and must be installed before using this role:

- [hetzner.hcloud](https://galaxy.ansible.com/ui/repo/published/hetzner/hcloud/)

```bash
ansible-galaxy collection install hetzner.hcloud
```

Unfortunately, the ansible dependency system does not allow roles to
depend on collections and vice versa even though it has been requested many times.
Therefore, the collection has to be installed manually.

## Role Variables

### api_token
Expand Down
5 changes: 0 additions & 5 deletions galaxy-requirements.yaml

This file was deleted.

5 changes: 1 addition & 4 deletions meta/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,4 @@ galaxy_info:
versions:
- all

#dependencies:
# - name: hetzner.hcloud
# src: hetzner.hcloud
# version: ">=4.0.1"
dependencies: []
6 changes: 3 additions & 3 deletions tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
- name: Validate variables
include_tasks: validate.yml

- name: Create {{ backup_type }} for {{ inventory_hostname }}
- name: Create {{ backup_type }}
delegate_to: "{{ delegation }}"
register: response
uri:
Expand All @@ -16,7 +16,7 @@
status_code: 201
body_format: json

- name: Wait until {{ backup_type }} is finished for {{ inventory_hostname }} with interval {{ backup_check_delay }}s
- name: Wait until {{ backup_type }} is finished with interval {{ backup_check_delay }}s
delegate_to: "{{ delegation }}"
register: response
uri:
Expand All @@ -31,6 +31,6 @@
retries: "{{ backup_check_retries }}"
delay: "{{ backup_check_delay }}"

- name: Rotate snapshots for {{ inventory_hostname }}
- name: Rotate snapshots
when: backup_type == "snapshot" and rotate_snapshots == true
include_tasks: rotate-snapshots.yml
47 changes: 37 additions & 10 deletions tasks/rotate-snapshots.yml
Original file line number Diff line number Diff line change
@@ -1,20 +1,47 @@
---
- name: 'Gather all snapshots with labels: "{{ label_selector }}"'
- name: Prepare label selector filter
set_fact:
label_selector_string: "{{ label_selector | items | map('join', '=') | join(',') }}"

- name: Get number of pages for snapshots that match the label selector
delegate_to: "{{ delegation }}"
register: response
hetzner.hcloud.hcloud_image_info:
api_token: "{{ api_token }}"
type: snapshot
label_selector: "{{ label_selector | items | map('join', '=') | join(',') }}"
failed_when: response.hcloud_image_info | length == 0
uri:
url: "https://api.hetzner.cloud/v1/images?type=snapshot&per_page=50&status=available&label_selector={{ label_selector_string }}"
method: GET
status_code: 200
headers:
Authorization: "Bearer {{ api_token }}"

- name: Gather snapshots to delete for {{ inventory_hostname }}
- name: Gather all snapshots that match the label selector
delegate_to: "{{ delegation }}"
register: response
uri:
url: "https://api.hetzner.cloud/v1/images?type=snapshot&per_page=50&status=available&label_selector={{ label_selector_string }}&page={{ item }}"
method: GET
status_code: 200
headers:
Authorization: "Bearer {{ api_token }}"
loop: "{{ range(1, response.json.meta.pagination.last_page + 1) | list }}"

- name: "Prepare list of snapshots"
set_fact:
delete_list: "{{ response.hcloud_image_info[0:response.hcloud_image_info | length - keep_snapshots | int] }}"
when: response.hcloud_image_info | length - keep_snapshots | int > 0
# workaround for selecting only snapshots created from the current host because label_selector has some problems:
# selectattr('created_from.name', 'equalto', inventory_hostname)
images: "{{ response.results | map(attribute='json.images') | flatten | selectattr('created_from.name', 'equalto', inventory_hostname) }}"

- name: Gather snapshots to delete
delegate_to: "{{ delegation }}"
set_fact:
delete_list: "{{ images[0:images | length - keep_snapshots | int] }}"
when: images | length - keep_snapshots | int > 0

- name: Show number of snapshots to delete
debug:
msg: "{{ delete_list | length }} of {{ images | length }} snapshots will be deleted"
when: delete_list | length > 0

- name: 'Delete {{ delete_list | length }} old snapshots for {{ inventory_hostname }}'
- name: "Delete old snapshots"
delegate_to: "{{ delegation }}"
when: delete_list | length > 0
loop: "{{ delete_list }}"
Expand Down
15 changes: 10 additions & 5 deletions tasks/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,20 @@
block:
- name: Get server details
delegate_to: "{{ delegation }}"
register: server
hetzner.hcloud.server_info:
api_token: "{{ api_token }}"
name: "{{ inventory_hostname }}"
register: response
uri:
url: "https://api.hetzner.cloud/v1/servers?name={{ inventory_hostname }}"
method: GET
status_code: 200
headers:
Authorization: "Bearer {{ api_token }}"
failed_when:
- response.json.servers | length == 0

- name: Check if backups are enabled
delegate_to: "{{ delegation }}"
assert:
quiet: yes
that:
- server.hcloud_server_info[0].backup_window not in [None, ""]
- response.json.servers.0.backup_window not in [None, ""]
fail_msg: "Backups are not enabled on this server."