summaryrefslogtreecommitdiffstats
path: root/ansible_collections/hetzner/hcloud/examples/server-with-firewall.yml
blob: 0e570967671e15e21ab973c95b9747638a510e04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
---
- name: Demonstrate creating servers with a firewall
  hosts: localhost
  connection: local

  vars:
    servers:
      - name: my-server1
      - name: my-server2

  tasks:
    - name: Create firewall
      hetzner.hcloud.firewall:
        name: my-firewall
        rules:
          - description: allow icmp from everywhere
            direction: in
            protocol: icmp
            source_ips:
              - 0.0.0.0/0
              - ::/0
          - description: allow ssh from everywhere
            direction: in
            protocol: tcp
            port: 22
            source_ips:
              - 0.0.0.0/0
              - ::/0
        state: present

    - name: Create servers
      hetzner.hcloud.server:
        name: "{{ item.name }}"
        server_type: cx11
        image: debian-12
        labels:
          kind: runners
        state: started
      loop: "{{ servers }}"

    - name: Apply firewall to resources using label selectors
      hetzner.hcloud.firewall_resource:
        firewall: my-firewall
        label_selectors: [kind=runners]
        state: present

    - name: Apply firewall to individual servers
      hetzner.hcloud.firewall_resource:
        firewall: my-firewall
        servers: "{{ servers | map(attribute='name') }}"
        state: present

    - name: Delete firewall
      hetzner.hcloud.firewall:
        name: my-firewall
        state: absent

    - name: Delete servers
      hetzner.hcloud.server:
        name: "{{ item.name }}"
        state: absent
      loop: "{{ servers }}"