diff --git a/ansible/playbooks/roles/wireguard_server/tasks/gather_current_config.yaml b/ansible/playbooks/roles/wireguard_server/tasks/gather_current_config.yaml new file mode 100644 index 0000000..d30825f --- /dev/null +++ b/ansible/playbooks/roles/wireguard_server/tasks/gather_current_config.yaml @@ -0,0 +1,35 @@ +--- +- name: Check if config already exists + stat: + path: "/etc/wireguard/{{ wg_interface }}.conf" + register: interface_configuration + +# - name: Fetching configuration file +# ansible.builtin.fetch: +# src: "/etc/wireguard/{{ wg_interface }}.conf" +# dest: "/tmp/{{ wg_interface }}.conf" + + + +- name: Parse configuration + ansible.utils.cli_parse: + command: "cat /etc/wireguard/{{ wg_interface }}.conf " + parser: + name: ansible.netcommon.native + template_path: templates/wg_config_template.yaml + set_fact: existing_wg_config + when: interface_configuration.stat.exists + +- name: Check registered peers + debug: + msg: "Found configs : {{ existing_wg_config }}" + + +- name: Updating client configs info + set_fact: + wg_peers: "{{ wg_peers | community.general.lists_mergeby([{'name':item.key,'publicKey':item.value.publicKey}] ,'name')}}" + loop: "{{ existing_wg_config | ansible.builtin.dict2items }}" + +- name: Log client configs info + debug: + msg: "Updated peers defintion : {{ wg_peers }}" \ No newline at end of file diff --git a/ansible/playbooks/roles/wireguard_server/tasks/main.yaml b/ansible/playbooks/roles/wireguard_server/tasks/main.yaml index 05c05c9..5de9e1a 100644 --- a/ansible/playbooks/roles/wireguard_server/tasks/main.yaml +++ b/ansible/playbooks/roles/wireguard_server/tasks/main.yaml @@ -1,5 +1,6 @@ --- - include_tasks: install_wireguard.yaml +- include_tasks: gather_current_config.yaml - include_tasks: generate_server_keys.yaml - include_tasks: generate_client_configs.yaml - include_tasks: configure_server.yaml diff --git a/ansible/playbooks/roles/wireguard_server/templates/wg_config_template.yaml b/ansible/playbooks/roles/wireguard_server/templates/wg_config_template.yaml new file mode 100644 index 0000000..84986d5 --- /dev/null +++ b/ansible/playbooks/roles/wireguard_server/templates/wg_config_template.yaml @@ -0,0 +1,13 @@ +--- +- example: "# Peer ID : fabio_test" + getval: '# Peer ID : (?P\S+)' + result: + "{{ peer_id }}": + name: "{{ peer_id }}" + shared: true + +- example: "PublicKey = 9a3tgXpF5CAOffbHZdW1QBEJgSLFnBDVbD1JaMPgzkM=" + getval: 'PublicKey = (?P\S+)' + result: + "{{ peer_id }}": + publicKey: "{{ publicKey }}" \ No newline at end of file