wgvirtipd/dev/wireguard-configs.yaml
2024-01-17 21:49:09 +01:00

35 lines
1,006 B
YAML

---
- name: Wireguard config generator
hosts: localhost
vars:
mask_bits: 24
base_ip: 10.2.0.0
port: 51871
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 }}/')"
jq --null-input \
--arg priv "$priv" \
--arg pub "$pub" \
--arg my_ip "$my_ip" \
'{"private_key": $priv, "public_key": $pub, "item": "vm{{ item }}", "ip": $my_ip}'
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: ./docker-compose.yaml.tmpl
dest: ./docker-compose.yaml