From d5dffb2ad0781b61565029c821e370a3836408f7 Mon Sep 17 00:00:00 2001 From: ace Date: Sun, 12 Mar 2023 20:49:31 +0300 Subject: [PATCH] first commit --- .gitignore | 1 + defaults/main.yml | 32 +++++++++++++++++++++++++ handlers/main.yml | 39 ++++++++++++++++++++++++++++++ tasks/configure.yml | 40 +++++++++++++++++++++++++++++++ tasks/docker.yml | 33 +++++++++++++++++++++++++ tasks/install.yml | 12 ++++++++++ tasks/main.yml | 22 +++++++++++++++++ tasks/podman.yml | 49 ++++++++++++++++++++++++++++++++++++++ templates/ddclient.conf.j2 | 20 ++++++++++++++++ 9 files changed, 248 insertions(+) create mode 100644 .gitignore create mode 100644 defaults/main.yml create mode 100644 handlers/main.yml create mode 100644 tasks/configure.yml create mode 100644 tasks/docker.yml create mode 100644 tasks/install.yml create mode 100644 tasks/main.yml create mode 100644 tasks/podman.yml create mode 100644 templates/ddclient.conf.j2 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ba6d390 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.galaxy_install_info diff --git a/defaults/main.yml b/defaults/main.yml new file mode 100644 index 0000000..35d3f28 --- /dev/null +++ b/defaults/main.yml @@ -0,0 +1,32 @@ +ddclient_containerized: false +ddclient_container_engine: "{{ container_engine | default('podman') }}" +ddclient_network_mode: "host" +ddclient_namespace: "{{ namespace | default('ddclient') }}" +ddclient_container_name: "{{ ddclient_namespace }}-ddclient" +ddclient_container_registry: "{{ container_registry | default(docker_registry) | default('registry.geekhome.org/ghp') }}" +ddclient_image_name: "ddclient" +ddclient_image_tag: "v3.9.1-ls45" +ddclient_systemd_unit_name: "{{ ddclient_container_name }}-container.service" + +# Volumes for containers +ddclient_default_volumes: + '/opt/{{ ddclient_namespace }}/ddclient.conf': /config/ddclient.conf + '/opt/{{ ddclient_namespace }}/Kvps.key': /config/Kvps.key + '/opt/{{ ddclient_namespace }}/Kvps.private': /config/Kvps.private + +# Put tsig key in files directory +ddclient_tsig_public_key_file: [] +ddclient_tsig_private_key_file: [] + +# OR define with base64 vars +ddclient_tsig_public_key_base64: [] +ddclient_tsig_private_key_base64: [] + +# Override default ddclient.conf template +ddclient_conf_override: [] + +ddclient_hosts: {} + # - name: my-record.example.com + # dns_ip: 1.2.3.4 + # domain: example.com + diff --git a/handlers/main.yml b/handlers/main.yml new file mode 100644 index 0000000..33c5d51 --- /dev/null +++ b/handlers/main.yml @@ -0,0 +1,39 @@ +--- +- name: Start ddclient + systemd: + name: ddclient + state: started + enabled: yes + daemon_reload: yes + +- name: Restart ddclient + systemd: + name: ddclient + state: restarted + enabled: yes + daemon_reload: yes + +- name: Restart docker ddclient + community.general.docker_container: + name: "{{ ddclient_container_name }}" + image: "{{ ddclient_container_registry }}/{{ ddclient_image_name }}:{{ ddclient_image_tag }}" + network_mode: "{{ ddclient_network_mode }}" + state: started + restart: yes + container_default_behavior: no_defaults + detach: true + restart_policy: unless-stopped + volumes: "{{ ddclient_volumes }}" + when: ddclient_container_engine == 'docker' + +- name: Restart podman ddclient + containers.podman.podman_container: + name: "{{ ddclient_container_name }}" + hostname: "{{ ddclient_container_name }}" + network: "{{ ddclient_network_mode }}" + image: "{{ ddclient_container_registry }}/{{ ddclient_image_name }}:{{ ddclient_image_tag }}" + state: started + restart: yes + volumes: "{{ ddclient_volumes }}" + when: ddclient_container_engine == 'podman' + diff --git a/tasks/configure.yml b/tasks/configure.yml new file mode 100644 index 0000000..71391cf --- /dev/null +++ b/tasks/configure.yml @@ -0,0 +1,40 @@ +--- +- block: + - name: Create public key for ddclient from file + copy: + src: "{{ ddclient_tsig_public_key }}" + dest: /etc/Kvps.key + when: ddclient_tsig_public_key_file | length > 0 + + - name: Create private key for ddclient from file + copy: + src: "{{ ddclient_tsig_private_key }}" + dest: /etc/Kvps.private + when: ddclient_tsig_private_key_file | length > 0 + + - name: Create public key for ddclient from var + copy: + content: "{{ ddclient_tsig_public_key_base64 | b64decode }}" + dest: /etc/Kvps.key + when: ddclient_tsig_public_key_base64 | length > 0 + + - name: Create private key for ddclient from var + copy: + content: "{{ ddclient_tsig_private_key_base64 | b64decode }}" + dest: /etc/Kvps.private + when: ddclient_tsig_private_key_base64 | length > 0 + + - name: Configure ddlient.conf with template + template: + src: "ddclient.conf.j2" + dest: "/etc/ddclient.conf" + when: ddclient_conf_override | length == 0 + notify: Restart ddclient + + - name: Configure ddlient.conf with overriding from var + copy: + content: "{{ ddclient_conf_override }}" + dest: "/etc/ddclient.conf" + when: ddclient_conf_override | length > 0 + notify: Restart ddclient + diff --git a/tasks/docker.yml b/tasks/docker.yml new file mode 100644 index 0000000..896fa91 --- /dev/null +++ b/tasks/docker.yml @@ -0,0 +1,33 @@ +--- +- name: Create configuration dir for {{ ddclient_namespace }} + file: + name: "/opt/{{ ddclient_namespace }}" + state: directory + +- name: Copy ddclient configuration for {{ ddclient_namespace }} + copy: + dest: "/opt/{{ ddclient_namespace }}/ddclient.conf" + content: "{{ ddclient_conf }}" + notify: Restart docker ddclient + +- name: Copy Kvps.key for {{ ddclient_namespace }} + copy: + dest: "/opt/{{ ddclient_namespace }}/Kvps.key" + content: "{{ ddclient_tsig_public_key_base64 | b64decode }}" + notify: Restart docker ddclient + +- name: Copy Kvps.private for {{ ddclient_namespace }} + copy: + dest: "/opt/{{ ddclient_namespace }}/Kvps.private" + content: "{{ ddclient_tsig_private_key_base64 | b64decode }}" + notify: Restart docker ddclient + +- name: Start ddclient in docker for {{ ddclient_namespace }} + docker_container: + name: "{{ ddclient_container_name }}" + image: "{{ ddclient_container_registry }}/{{ ddclient_image_name }}:{{ ddclient_image_tag }}" + network_mode: "{{ ddclient_network_mode }}" + state: started + container_default_behavior: no_defaults + restart_policy: unless-stopped + volumes: "{{ ddclient_volumes }}" diff --git a/tasks/install.yml b/tasks/install.yml new file mode 100644 index 0000000..a1c47c8 --- /dev/null +++ b/tasks/install.yml @@ -0,0 +1,12 @@ +--- +- block: + - name: Install ddclient and bind-utils + dnf: + name: + - ddclient + - bind-utils + state: present + notify: Start ddclient + register: install_ddlient_result + tags: + - ddclient-install diff --git a/tasks/main.yml b/tasks/main.yml new file mode 100644 index 0000000..0ff7234 --- /dev/null +++ b/tasks/main.yml @@ -0,0 +1,22 @@ +--- +- name: Install ddclient (not containerized) + when: not ddclient_containerized + block: + - import_tasks: install.yml + - import_tasks: configure.yml + become: true + +- name: Install ddclient (containerized) + when: ddclient_containerized + block: + - name: Merge volumes + set_fact: + ddclient_volumes: "{{ ddclient_volumes | default([]) + [item.key +':'+ item.value] }}" + loop: "{{ ddclient_default_volumes | dict2items }}" + + - import_tasks: docker.yml + when: ddclient_container_engine == "docker" + + - import_tasks: podman.yml + when: ddclient_container_engine == "podman" + become: true diff --git a/tasks/podman.yml b/tasks/podman.yml new file mode 100644 index 0000000..00f1f6d --- /dev/null +++ b/tasks/podman.yml @@ -0,0 +1,49 @@ +--- +- name: Create configuration dir for {{ ddclient_namespace }} + file: + name: "/opt/{{ ddclient_namespace }}" + state: directory + +- name: Copy ddclient configuration for {{ ddclient_namespace }} + copy: + dest: "/opt/{{ ddclient_namespace }}/ddclient.conf" + content: "{{ ddclient_conf }}" + notify: Restart podman ddclient + +- name: Copy Kvps.key for {{ ddclient_namespace }} + copy: + dest: "/opt/{{ ddclient_namespace }}/Kvps.key" + content: "{{ ddclient_tsig_public_key_base64 | b64decode }}" + notify: Restart podman ddclient + +- name: Copy Kvps.private for {{ ddclient_namespace }} + copy: + dest: "/opt/{{ ddclient_namespace }}/Kvps.private" + content: "{{ ddclient_tsig_private_key_base64 | b64decode }}" + notify: Restart podman ddclient + +- name: Start ddclient with podman for {{ ddclient_namespace }} + containers.podman.podman_container: + name: "{{ ddclient_container_name }}" + hostname: "{{ ddclient_container_name }}" + network: "{{ ddclient_network_mode }}" + image: "{{ ddclient_container_registry }}/{{ ddclient_image_name }}:{{ ddclient_image_tag }}" + state: present + volumes: "{{ ddclient_volumes }}" + +- name: Generate container systemd unit + shell: "podman generate systemd {{ ddclient_container_name }}" + register: ddclient_systemd_unit + changed_when: False + +- name: Create ddclient systemd unit + copy: + dest: "/etc/systemd/system/{{ ddclient_systemd_unit_name }}" + content: "{{ ddclient_systemd_unit.stdout | regex_replace('^#.*', multiline=True) | trim }}" + +- name: Enable ddclient systemd unit + ansible.builtin.systemd: + name: "{{ ddclient_systemd_unit_name }}" + enabled: yes + state: started + daemon_reload: yes diff --git a/templates/ddclient.conf.j2 b/templates/ddclient.conf.j2 new file mode 100644 index 0000000..98348b6 --- /dev/null +++ b/templates/ddclient.conf.j2 @@ -0,0 +1,20 @@ +daemon=300 +syslog=yes +mail-failure=root +pid=/var/run/ddclient/ddclient.pid +ssl=yes +debug=yes +verbose=yes + +{% for host in ddclient_hosts %} +use=web +web=checkip.dyndns.org +protocol=nsupdate +server={{ host.dns_ip }} +login=/usr/bin/nsupdate +password={{ '/config' if ddclient_containerized else '/etc'}}/Kvps.key +zone={{ host.domain }} +ttl=60 +{{ host.name }} + +{% endfor %}