Skip to content

Commit

Permalink
Add floating ip feature
Browse files Browse the repository at this point in the history
  • Loading branch information
ggggut authored and jooola committed Oct 4, 2023
1 parent 857bde7 commit 6f230cd
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 0 deletions.
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,45 @@ platforms:
ip: 10.1.0.1/24
```
**Floating IP**
You can create an floating ip assigned to your host during molecule run.
```yaml
platforms:
- name: instance1
server_type: cx11
image: debian-10
floating_ips:
test_ip_1:
type: ipv4
test_ip_2:
type: ipv6
```
- **floating_ips**
- **type** (required): type of floating ip ('ipv4' or 'ipv6')
Note that you have to assign the ip to the specific interface on the host.
You can do something like this in your `Converge.yaml` or `Prepare.yaml`:

```yaml
- name: set floating_ip
changed_when: false
vars:
instance_conf: "{{ lookup('file', molecule_instance_config, errors='warn') | from_yaml }}"
shell: |
set -o pipefail;
if ip a | grep -q {{ item.1.ip }};
then echo "ip already assigned";
else ip addr add {{ item.1.ip }} dev eth0;
fi
args:
executable: /bin/bash
when: "item.0.instance == inventory_hostname"
loop: "{{ instance_conf|subelements('floating_ips', skip_missing=True) }}"
```

> [!NOTE]
> The `networks.ip_range` is important for creating. If you have multiple
> hosts, you may only define it once.
Expand Down
28 changes: 28 additions & 0 deletions molecule_hetznercloud/playbooks/create.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,34 @@
| rejectattr('type', '==', 'vswitch')
}}
- name: Create floating ip(s) and assign it to server
hetzner.hcloud.hcloud_floating_ip:
name: "{{ item.name }}"
server: "{{ item.server_name }}"
type: "{{ item.type }}"
home_location: "{{ item.home_location | default(omit) }}"
state: "present"
register: "floating_ip"
loop: "{{ molecule_yml.platforms | molecule_get_hetznercloud_floating_ips() }}"

- name: Set floating_ips list
ansible.builtin.set_fact:
server_floating_ips: >
{{
server_floating_ips | default([]) + [
{
'instance': item.hcloud_floating_ip.server,
'floating_ips': {
'ip_name': item.hcloud_floating_ip.name,
'ip': item.hcloud_floating_ip.ip,
'type': item.hcloud_floating_ip.type,
'home_location': item.hcloud_floating_ip.home_location,
}
}
]
}}
loop: "{{ floating_ip.results }}"

- name: Create instance config
block:
- name: Populate instance config dict
Expand Down
7 changes: 7 additions & 0 deletions molecule_hetznercloud/playbooks/destroy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@
loop: "{{ instance_conf | subelements('networks', skip_missing=True) }}"
register: networks

- name: Destroy floating_ip(s)
hetzner.hcloud.hcloud_floating_ip:
name: "{{ item.1.ip_name }}"
force: true
state: absent
loop: "{{ instance_conf | subelements('floating_ips', skip_missing=True) }}"

- name: Remove registered SSH key
when: instance_conf | length
hetzner.hcloud.hcloud_ssh_key:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,22 @@ def get_hetznercloud_subnetworks(platforms: list[dict]) -> list[dict]:
return all_subnetworks


def get_hetznercloud_floating_ips(platforms: list[dict]) -> list[dict]:
all_floating_ips = []

for platform in platforms:
if "floating_ips" not in platform:
continue

for floating_ip_name, floating_ip in platform["floating_ips"].items():
floating_ip["server_name"] = platform["name"]
floating_ip["name"] = floating_ip_name

all_floating_ips.append(floating_ip)

return all_floating_ips


def get_hetznercloud_volumes(platforms: list[dict]) -> list[dict]:
all_volumes = []
for platform in platforms:
Expand All @@ -69,5 +85,6 @@ def filters(self):
return {
"molecule_get_hetznercloud_networks": get_hetznercloud_networks,
"molecule_get_hetznercloud_subnetworks": get_hetznercloud_subnetworks,
"molecule_get_hetznercloud_floating_ips": get_hetznercloud_floating_ips,
"molecule_get_hetznercloud_volumes": get_hetznercloud_volumes,
}
3 changes: 3 additions & 0 deletions tests/integration/molecule/default/molecule.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ platforms:
shared:
subnet:
ip: 10.10.10.2/24
floating_ips:
floating-1:
type: ipv4
volumes:
- name: volume-1
- name: volume-2
Expand Down

0 comments on commit 6f230cd

Please sign in to comment.