feat(install): add service and config template for host install

This commit is contained in:
Bertrand Lanson 2024-01-31 21:01:49 +01:00
parent 97676d6485
commit 4fb90df5cc
8 changed files with 146 additions and 8 deletions

View File

@ -3,6 +3,8 @@
deploy_haproxy_deploy_method: host # deployment method, either host or docker deploy_haproxy_deploy_method: host # deployment method, either host or docker
deploy_haproxy_version: "2.6" deploy_haproxy_version: "2.6"
deploy_haproxy_env_variables: {}
# Options from the "default" config block in haproxy.cfg # Options from the "default" config block in haproxy.cfg
# The default values here are usually set, but you can change any of them. # The default values here are usually set, but you can change any of them.
deploy_haproxy_global: deploy_haproxy_global:
@ -10,9 +12,10 @@ deploy_haproxy_global:
- log /dev/log local1 notice - log /dev/log local1 notice
- stats socket {{ deploy_haproxy_socket }} level admin - stats socket {{ deploy_haproxy_socket }} level admin
- chroot {{ deploy_haproxy_chroot }} - chroot {{ deploy_haproxy_chroot }}
- user {{ deploy_haproxy_user }} # - user {{ deploy_haproxy_user }}
- group {{ deploy_haproxy_group }} # - group {{ deploy_haproxy_group }}
- daemon - daemon
- description hashistack haproxy
deploy_haproxy_defaults: deploy_haproxy_defaults:
- log global - log global
@ -23,13 +26,36 @@ deploy_haproxy_defaults:
- timeout client 5000 - timeout client 5000
- timeout server 5000 - timeout server 5000
deploy_haproxy_frontend: deploy_haproxy_frontends:
- name: default - name: default
options: options:
- mode http - mode http
- bind :80 - bind :80
- default_backend default - default_backend default
- description nginx frontend
deploy_haproxy_backend: [] deploy_haproxy_backends:
- name: default
deploy_haproxy_listen: [] 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

View File

@ -1,2 +1,19 @@
--- ---
# handlers file for deploy_haproxy # 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

23
tasks/configure.yml Normal file
View File

@ -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"

View File

@ -33,3 +33,13 @@
when: when:
ansible_facts.services[deploy_haproxy_service_name~'.service'] is defined ansible_facts.services[deploy_haproxy_service_name~'.service'] is defined
and ansible_facts.services[deploy_haproxy_service_name~'.service']['state'] == 'running' 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"

View File

@ -5,3 +5,6 @@
- name: "Import install_host.yml" - name: "Import install_host.yml"
ansible.builtin.include_tasks: install_host.yml ansible.builtin.include_tasks: install_host.yml
- name: "Import configure.yml"
ansible.builtin.include_tasks: configure.yml

View File

@ -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 %}

4
templates/haproxy.env.j2 Normal file
View File

@ -0,0 +1,4 @@
# {{ ansible_managed }}
{% for item in deploy_haproxy_env_variables %}
{{ item }}="{{ deploy_haproxy_env_variables[item] }}"
{% endfor %}

View File

@ -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