From 9bd6da5a6715ba6201abce2a3b9cbaac4d46711a Mon Sep 17 00:00:00 2001 From: Bertrand Lanson Date: Mon, 11 Mar 2024 19:29:02 +0100 Subject: [PATCH] feat(config): add custom template option --- README.md | 9 +++ defaults/main.yml | 3 + molecule/with_custom_conf/group_vars/all.yml | 3 + molecule/with_custom_conf/prepare.yml | 55 +++++++++++++++++++ molecule/with_custom_conf/verify.yml | 1 + .../group_vars/all.yml | 3 + .../with_custom_conf_vagrant/prepare copy.yml | 55 +++++++++++++++++++ .../with_custom_conf_vagrant/requirements.yml | 1 + molecule/with_custom_conf_vagrant/verify.yml | 1 + tasks/configure.yml | 2 +- 10 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 molecule/with_custom_conf/prepare.yml create mode 100644 molecule/with_custom_conf_vagrant/prepare copy.yml diff --git a/README.md b/README.md index 28edba1..b05754b 100644 --- a/README.md +++ b/README.md @@ -100,6 +100,15 @@ deploy_keepalived_extra_container_volumes: [] Extra volumes to mount to the container if using the `docker` deploy method. By default, `/etc/keepalived` (host) will be mounted to `/etc/keepalived` (container) +```yaml +deploy_keepalived_use_custom_config: false # by default, set to false +``` +This variable lets you switch to using a custom keepalived.conf template file. By default, this is false, and the role will use the default keepalived.conf template. + +```yaml +deploy_keepalived_custom_config_src: # by default, unset +``` +If `deploy_keepalived_use_custom_config: true`, this sets the path for the custom keepalived.conf template to use. This can either be a static file, or a jinja2 template. It will be copied to `/etc/keepalived/keepalived.conf` on the target machine. Dependencies diff --git a/defaults/main.yml b/defaults/main.yml index b649924..f7f52d1 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -20,3 +20,6 @@ deploy_keepalived_notify_script: notify.sh deploy_keepalived_custom_scripts_src: deploy_keepalived_extra_container_volumes: [] + +deploy_keepalived_use_custom_config: false +deploy_keepalived_custom_config_src: diff --git a/molecule/with_custom_conf/group_vars/all.yml b/molecule/with_custom_conf/group_vars/all.yml index d6a1323..e7d2016 100644 --- a/molecule/with_custom_conf/group_vars/all.yml +++ b/molecule/with_custom_conf/group_vars/all.yml @@ -23,3 +23,6 @@ deploy_keepalived_notify_script: notify.sh deploy_keepalived_custom_scripts_src: deploy_keepalived_extra_container_volumes: [] + +deploy_keepalived_use_custom_config: true +deploy_keepalived_custom_config_src: /tmp/keepalived.conf.j2 diff --git a/molecule/with_custom_conf/prepare.yml b/molecule/with_custom_conf/prepare.yml new file mode 100644 index 0000000..0150eae --- /dev/null +++ b/molecule/with_custom_conf/prepare.yml @@ -0,0 +1,55 @@ +--- +- name: Prepare + hosts: all + become: true + tasks: + - name: "Generate custom keepalived.conf template" # noqa: run-once[task] + delegate_to: localhost + run_once: true + block: + - name: "Generate custom keepalived.conf template" + ansible.builtin.copy: + content: | + {% raw -%} + # {{ ansible_managed }} + # THIS IS A CUSTOM CONF + global_defs { + script_user keepalived_script + enable_script_security + } + + vrrp_instance {{ deploy_keepalived_vrrp_instance_name }} { + interface {{ deploy_keepalived_interface }} + + state {{ deploy_keepalived_state }} + virtual_router_id {{ deploy_keepalived_router_id }} + priority {{ deploy_keepalived_priority }} + advert_int {{ deploy_keepalived_advert_interval }} + + nopreempt + + {% if deploy_keepalived_unicast_peers %} + unicast_src_ip {{ deploy_keepalived_unicast_source }} + unicast_peer { + {% for peer in deploy_keepalived_unicast_peers %} + {{ peer }} + {% endfor %} + } + {% endif %} + + authentication { + auth_type PASS + auth_pass {{ deploy_keepalived_auth_passwd }} + } + + virtual_ipaddress { + {% for vip in deploy_keepalived_virtual_ips %} + {{ vip }} + {% endfor %} + } + + notify {{ deploy_keepalived_scripts_dir }}/{{ deploy_keepalived_notify_script }} + } + {%- endraw %} + dest: /tmp/keepalived.conf.j2 + mode: '0644' diff --git a/molecule/with_custom_conf/verify.yml b/molecule/with_custom_conf/verify.yml index 9fee29f..a052539 100644 --- a/molecule/with_custom_conf/verify.yml +++ b/molecule/with_custom_conf/verify.yml @@ -54,6 +54,7 @@ vars: keepalived_expected_cfg_file: | # Ansible managed: Do NOT edit this file manually! + # THIS IS A CUSTOM CONF global_defs { script_user keepalived_script enable_script_security diff --git a/molecule/with_custom_conf_vagrant/group_vars/all.yml b/molecule/with_custom_conf_vagrant/group_vars/all.yml index 9fcf684..daab598 100644 --- a/molecule/with_custom_conf_vagrant/group_vars/all.yml +++ b/molecule/with_custom_conf_vagrant/group_vars/all.yml @@ -22,3 +22,6 @@ deploy_keepalived_notify_script: notify.sh deploy_keepalived_custom_scripts_src: deploy_keepalived_extra_container_volumes: [] + +deploy_keepalived_use_custom_config: true +deploy_keepalived_custom_config_src: /tmp/keepalived.conf.j2 diff --git a/molecule/with_custom_conf_vagrant/prepare copy.yml b/molecule/with_custom_conf_vagrant/prepare copy.yml new file mode 100644 index 0000000..0150eae --- /dev/null +++ b/molecule/with_custom_conf_vagrant/prepare copy.yml @@ -0,0 +1,55 @@ +--- +- name: Prepare + hosts: all + become: true + tasks: + - name: "Generate custom keepalived.conf template" # noqa: run-once[task] + delegate_to: localhost + run_once: true + block: + - name: "Generate custom keepalived.conf template" + ansible.builtin.copy: + content: | + {% raw -%} + # {{ ansible_managed }} + # THIS IS A CUSTOM CONF + global_defs { + script_user keepalived_script + enable_script_security + } + + vrrp_instance {{ deploy_keepalived_vrrp_instance_name }} { + interface {{ deploy_keepalived_interface }} + + state {{ deploy_keepalived_state }} + virtual_router_id {{ deploy_keepalived_router_id }} + priority {{ deploy_keepalived_priority }} + advert_int {{ deploy_keepalived_advert_interval }} + + nopreempt + + {% if deploy_keepalived_unicast_peers %} + unicast_src_ip {{ deploy_keepalived_unicast_source }} + unicast_peer { + {% for peer in deploy_keepalived_unicast_peers %} + {{ peer }} + {% endfor %} + } + {% endif %} + + authentication { + auth_type PASS + auth_pass {{ deploy_keepalived_auth_passwd }} + } + + virtual_ipaddress { + {% for vip in deploy_keepalived_virtual_ips %} + {{ vip }} + {% endfor %} + } + + notify {{ deploy_keepalived_scripts_dir }}/{{ deploy_keepalived_notify_script }} + } + {%- endraw %} + dest: /tmp/keepalived.conf.j2 + mode: '0644' diff --git a/molecule/with_custom_conf_vagrant/requirements.yml b/molecule/with_custom_conf_vagrant/requirements.yml index 92a4b82..7fb07b0 100644 --- a/molecule/with_custom_conf_vagrant/requirements.yml +++ b/molecule/with_custom_conf_vagrant/requirements.yml @@ -3,4 +3,5 @@ roles: - name: ednz_cloud.manage_repositories - name: ednz_cloud.manage_apt_packages + - name: ednz_cloud.docker_systemd_service - name: ednz_cloud.install_docker diff --git a/molecule/with_custom_conf_vagrant/verify.yml b/molecule/with_custom_conf_vagrant/verify.yml index bdc32b4..029eadc 100644 --- a/molecule/with_custom_conf_vagrant/verify.yml +++ b/molecule/with_custom_conf_vagrant/verify.yml @@ -30,6 +30,7 @@ vars: keepalived_expected_cfg_file: | # Ansible managed: Do NOT edit this file manually! + # THIS IS A CUSTOM CONF global_defs { script_user keepalived_script enable_script_security diff --git a/tasks/configure.yml b/tasks/configure.yml index 8864696..917d727 100644 --- a/tasks/configure.yml +++ b/tasks/configure.yml @@ -13,7 +13,7 @@ - name: "Copy keepalived.conf template" ansible.builtin.template: - src: keepalived.conf.j2 + src: "{{ deploy_keepalived_custom_config_src if deploy_keepalived_use_custom_config else 'keepalived.conf.j2' }}" dest: "{{ deploy_keepalived_config_dir }}/keepalived.conf" owner: "{{ deploy_keepalived_user }}" group: "{{ deploy_keepalived_group }}"