wgvirtipd/dev/setup.yaml
2024-01-21 22:52:57 +01:00

52 lines
1.5 KiB
YAML

---
- name: Wireguard config generator
hosts: localhost
vars:
mask_bits: 24
base_ip: 10.2.0.0
port: 51871
keepalived_ip: 10.2.0.100
tasks:
- name: generate keypair
shell: |
#!/bin/sh
priv="$(wg genkey)"
pub="$(echo "$priv" | wg pubkey)"
base_ip="{{ base_ip }}"
my_ip="$(echo "$base_ip" | sed 's/0$/{{ item }}/')"
if [[ {{item}} -eq 1 ]]; then
state=MASTER
else
state=BACKUP
fi
priority=$((100 - {{ item }}))
jq --null-input \
--arg priv "$priv" \
--arg pub "$pub" \
--arg my_ip "$my_ip" \
--arg state "$state" \
--arg priority "$priority" \
'{"private_key": $priv, "public_key": $pub, "item": "vm{{ item }}", "ip": $my_ip, "keepalived_state": $state, "keepalived_priority": $priority}'
with_items: ["1", "2", "3", "4"]
register: keypairs_
- set_fact:
keypairs: "{{ keypairs | default([]) + [item.stdout | from_json] }}"
with_items: "{{ keypairs_.results }}"
- debug:
var: keypairs
name: write wg configs
- template:
src: ./config/wg0.conf.tmpl
dest: ./config/{{ item.item }}-wg0.conf
with_items: "{{ keypairs }}"
- template:
src: ./config/keepalived.conf.tmpl
dest: ./config/{{ item.item }}-keepalived.conf
with_items: "{{ keypairs }}"
- template:
src: ./config/index.html.tmpl
dest: ./config/{{ item.item }}-index.html
with_items: "{{ keypairs }}"
- template:
src: ./docker-compose.yaml.tmpl
dest: ./docker-compose.yaml