From 4fb90df5cc63d865baebc1c5c8cf52be69405445 Mon Sep 17 00:00:00 2001 From: Bertrand Lanson Date: Wed, 31 Jan 2024 21:01:49 +0100 Subject: [PATCH] feat(install): add service and config template for host install --- defaults/main.yml | 42 +++++++++++++++++++++++++++++------- handlers/main.yml | 17 +++++++++++++++ tasks/configure.yml | 23 ++++++++++++++++++++ tasks/install_host.yml | 10 +++++++++ tasks/main.yml | 3 +++ templates/haproxy.cfg.j2 | 31 ++++++++++++++++++++++++++ templates/haproxy.env.j2 | 4 ++++ templates/haproxy.service.j2 | 24 +++++++++++++++++++++ 8 files changed, 146 insertions(+), 8 deletions(-) create mode 100644 tasks/configure.yml create mode 100644 templates/haproxy.env.j2 create mode 100644 templates/haproxy.service.j2 diff --git a/defaults/main.yml b/defaults/main.yml index 2647a5b..70da85c 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -3,16 +3,19 @@ deploy_haproxy_deploy_method: host # deployment method, either host or docker deploy_haproxy_version: "2.6" +deploy_haproxy_env_variables: {} + # Options from the "default" config block in haproxy.cfg # The default values here are usually set, but you can change any of them. deploy_haproxy_global: - - log /dev/log local0 - - log /dev/log local1 notice + - log /dev/log local0 + - log /dev/log local1 notice - stats socket {{ deploy_haproxy_socket }} level admin - chroot {{ deploy_haproxy_chroot }} - - user {{ deploy_haproxy_user }} - - group {{ deploy_haproxy_group }} + # - user {{ deploy_haproxy_user }} + # - group {{ deploy_haproxy_group }} - daemon + - description hashistack haproxy deploy_haproxy_defaults: - log global @@ -23,13 +26,36 @@ deploy_haproxy_defaults: - timeout client 5000 - timeout server 5000 -deploy_haproxy_frontend: +deploy_haproxy_frontends: - name: default options: - mode http - bind :80 - default_backend default + - description nginx frontend -deploy_haproxy_backend: [] - -deploy_haproxy_listen: [] +deploy_haproxy_backends: + - name: default + options: + - option forwardfor + - server srv_nginx1 172.17.0.4:80 + - server srv_nginx2 172.17.0.3:80 +deploy_haproxy_listen: + - name: stats + options: + - bind :9000 + - mode http + - stats enable + - stats uri /stats + - stats refresh 30s + - stats show-desc + - stats show-legends + - stats auth admin:password + - name: health + options: + - bind :8000 + - mode http + - option httpchk GET /health HTTP/1.1\r\nHost:\ localhost + - http-check expect status 200 + - acl health_check_ok nbsrv() ge 1 + - monitor-uri /health diff --git a/handlers/main.yml b/handlers/main.yml index 3312d8f..a9e23f5 100644 --- a/handlers/main.yml +++ b/handlers/main.yml @@ -1,2 +1,19 @@ --- # handlers file for deploy_haproxy +- name: "Reload systemd file" + ansible.builtin.systemd: + daemon_reload: true + listen: "systemctl-daemon-reload" + +- name: "Enable haproxy service" + ansible.builtin.service: + name: "{{ deploy_haproxy_service_name }}" + enabled: true + listen: "systemctl-enable-haproxy" + +- name: "Start haproxy service" + ansible.builtin.service: + name: "{{ deploy_haproxy_service_name }}" + state: restarted + listen: "systemctl-restart-haproxy" + throttle: 1 diff --git a/tasks/configure.yml b/tasks/configure.yml new file mode 100644 index 0000000..67bd40f --- /dev/null +++ b/tasks/configure.yml @@ -0,0 +1,23 @@ +--- +# task/configure file for deploy_haproxy +- name: "Configure for host installation" + when: deploy_haproxy_deploy_method == 'host' + block: + - name: "Create haproxy.env" + ansible.builtin.template: + src: haproxy.env.j2 + dest: "{{ deploy_haproxy_config_dir }}/haproxy.env" + owner: "{{ deploy_haproxy_user }}" + group: "{{ deploy_haproxy_group }}" + mode: "0600" + +- name: "Copy haproxy.cfg template" + ansible.builtin.template: + src: haproxy.cfg.j2 + dest: "{{ deploy_haproxy_config_dir }}/haproxy.cfg" + owner: "{{ deploy_haproxy_user }}" + group: "{{ deploy_haproxy_group }}" + mode: "0600" + notify: + - "systemctl-enable-haproxy" + - "systemctl-restart-haproxy" diff --git a/tasks/install_host.yml b/tasks/install_host.yml index 583b0ef..60cc5fb 100644 --- a/tasks/install_host.yml +++ b/tasks/install_host.yml @@ -33,3 +33,13 @@ when: ansible_facts.services[deploy_haproxy_service_name~'.service'] is defined and ansible_facts.services[deploy_haproxy_service_name~'.service']['state'] == 'running' + +- name: "Copy systemd service file for haproxy" + ansible.builtin.template: + src: "haproxy.service.j2" + dest: "/etc/systemd/system/haproxy.service" + owner: root + group: root + mode: "0644" + notify: + - "systemctl-daemon-reload" diff --git a/tasks/main.yml b/tasks/main.yml index 7a8fd33..581d096 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -5,3 +5,6 @@ - name: "Import install_host.yml" ansible.builtin.include_tasks: install_host.yml + +- name: "Import configure.yml" + ansible.builtin.include_tasks: configure.yml diff --git a/templates/haproxy.cfg.j2 b/templates/haproxy.cfg.j2 index e69de29..91e49e8 100644 --- a/templates/haproxy.cfg.j2 +++ b/templates/haproxy.cfg.j2 @@ -0,0 +1,31 @@ +# {{ ansible_managed }} +global +{% for option in deploy_haproxy_global %} + {{ option }} +{% endfor %} + +defaults +{% for option in deploy_haproxy_defaults %} + {{ option }} +{% endfor %} + +{% for frontend in deploy_haproxy_frontends %} +frontend {{ frontend.name }} +{% for option in frontend.options %} + {{ option }} +{% endfor %} +{% endfor %} + +{% for backend in deploy_haproxy_backends %} +backend {{ backend.name }} +{% for option in backend.options%} + {{ option }} +{% endfor %} +{% endfor %} + +{% for listen in deploy_haproxy_listen %} +listen {{ listen.name }} +{% for option in listen.options %} + {{ option }} +{% endfor %} +{% endfor %} diff --git a/templates/haproxy.env.j2 b/templates/haproxy.env.j2 new file mode 100644 index 0000000..674baf9 --- /dev/null +++ b/templates/haproxy.env.j2 @@ -0,0 +1,4 @@ +# {{ ansible_managed }} +{% for item in deploy_haproxy_env_variables %} +{{ item }}="{{ deploy_haproxy_env_variables[item] }}" +{% endfor %} diff --git a/templates/haproxy.service.j2 b/templates/haproxy.service.j2 new file mode 100644 index 0000000..c744426 --- /dev/null +++ b/templates/haproxy.service.j2 @@ -0,0 +1,24 @@ +# {{ ansible_managed }} +[Unit] +Description=HAProxy Load Balancer +Documentation=man:haproxy(1) +Documentation=file:/usr/share/doc/haproxy/configuration.txt.gz +ConditionFileNotEmpty={{ deploy_haproxy_config_dir }}/haproxy.cfg +After=network-online.target rsyslog.service +Wants=network-online.target + +[Service] +EnvironmentFile=-/etc/default/{{ deploy_haproxy_service_name }} +EnvironmentFile=-{{ deploy_haproxy_config_dir }}/haproxy.env +BindReadOnlyPaths=/dev/log:{{ deploy_haproxy_chroot }}/dev/log +Environment="CONFIG={{ deploy_haproxy_config_dir }}/haproxy.cfg" "PIDFILE=/run/haproxy.pid" "EXTRAOPTS=-S /run/haproxy-master.sock" +ExecStart=/usr/sbin/haproxy -Ws -f $CONFIG -p $PIDFILE $EXTRAOPTS +ExecReload=/usr/sbin/haproxy -Ws -f $CONFIG -c $EXTRAOPTS +ExecReload=/bin/kill -USR2 $MAINPID +KillMode=mixed +Restart=always +SuccessExitStatus=143 +Type=notify + +[Install] +WantedBy=multi-user.target