Skip to content

Commit

Permalink
feat(install): add service and config template for host install
Browse files Browse the repository at this point in the history
  • Loading branch information
ednxzu committed Jan 31, 2024
1 parent 97676d6 commit 4fb90df
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 8 deletions.
42 changes: 34 additions & 8 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
17 changes: 17 additions & 0 deletions handlers/main.yml
Original file line number Diff line number Diff line change
@@ -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
23 changes: 23 additions & 0 deletions tasks/configure.yml
Original file line number Diff line number Diff line change
@@ -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"
10 changes: 10 additions & 0 deletions tasks/install_host.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
3 changes: 3 additions & 0 deletions tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
31 changes: 31 additions & 0 deletions templates/haproxy.cfg.j2
Original file line number Diff line number Diff line change
@@ -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 changes: 4 additions & 0 deletions templates/haproxy.env.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# {{ ansible_managed }}
{% for item in deploy_haproxy_env_variables %}
{{ item }}="{{ deploy_haproxy_env_variables[item] }}"
{% endfor %}
24 changes: 24 additions & 0 deletions templates/haproxy.service.j2
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 4fb90df

Please sign in to comment.