From 8678ea4f6aba65cafc1ffab57917b06c45482eec Mon Sep 17 00:00:00 2001 From: LinuxSquare Date: Sat, 6 Jul 2024 10:24:05 +0200 Subject: [PATCH] Add haproxy playbooks --- haproxy/files/haproxy_config.j2 | 40 ++++++++++++++++++ haproxy/files/haproxy_rcscript.initd.j2 | 54 +++++++++++++++++++++++++ haproxy/init.ansible.yml | 3 +- haproxy/map.yml | 1 + haproxy/pkg.ansible.yml | 34 ++++++++++++++++ haproxy/service.ansible.yml | 7 ++++ 6 files changed, 138 insertions(+), 1 deletion(-) create mode 100644 haproxy/files/haproxy_config.j2 create mode 100644 haproxy/files/haproxy_rcscript.initd.j2 create mode 100644 haproxy/map.yml create mode 100644 haproxy/pkg.ansible.yml create mode 100644 haproxy/service.ansible.yml diff --git a/haproxy/files/haproxy_config.j2 b/haproxy/files/haproxy_config.j2 new file mode 100644 index 0000000..f590fd3 --- /dev/null +++ b/haproxy/files/haproxy_config.j2 @@ -0,0 +1,40 @@ +global + log 127.0.0.1 local2 + + maxconn 4000 + user haproxy + group haproxy + daemon + + stats socket /var/lib/haproxy/stats + +defaults + mode http + log global + timeout queue 1m + timeout connect 10s + timeout client 5m + timeout server 5m + maxconn 3000 + +frontend ssh + mode tcp + + bind {{ IPV4 }}:22 + + default_backend host-ssh + +frontend http + mode tcp + bind {{ IPV4 }}:80 + + default_backend host-http + +backend host-ssh + mode tcp + + server localhost 127.0.0.1 + +backend host-http + mode tcp + server localhost 127.0.0.1 diff --git a/haproxy/files/haproxy_rcscript.initd.j2 b/haproxy/files/haproxy_rcscript.initd.j2 new file mode 100644 index 0000000..a654b79 --- /dev/null +++ b/haproxy/files/haproxy_rcscript.initd.j2 @@ -0,0 +1,54 @@ +#!/sbin/openrc-run +# Copyright 1999-2011 Gentoo Foundation +# Distributed under the terms of the GNU General Public License v2 +# $Header: /var/cvsroot/gentoo-x86/net-proxy/haproxy/files/haproxy.initd-r1,v 1.2 2011/12/04 10:32:32 swegener Exp $ + +extra_commands="checkconfig" +extra_started_commands="reload" +command=/usr/sbin/haproxy + +CONF=${HAPROXY_CONF:-/etc/haproxy/${SVCNAME}.cfg} +CONF_DIR=${HAPROXY_CONF_DIR:-{{ CONF_DIR }}} +PIDFILE=/var/run/${SVCNAME}.pid + +depend() { + need net + after firewall + use dns logger +} + +checkconfig() { + if [ ! -f "${CONF}" ] && [ ! -d "${CONF}" ]; then + eerror "${CONF} does not exist!" + return 1 + fi + + ebegin "Checking ${CONF}" + $command -q -c -f "${CONF}" + eend $? +} + +start() { + ebegin "Starting ${SVCNAME}" + start-stop-daemon --pidfile "${PIDFILE}" --exec $command \ + --start -- -D -p "${PIDFILE}" -f "${CONF}" -f "${CONF_DIR}" + eend $? +} + +stop() { + ebegin "Stopping ${SVCNAME}" + + if [ "${RC_CMD}" = "restart" ]; then + checkconfig || return 1 + fi + + start-stop-daemon --stop --pidfile "${PIDFILE}" + eend $? +} + +reload() { + ebegin "Reloading ${SVCNAME}" + checkconfig || { eerror "Reloading failed, please fix your ${CONF} first"; return 1; } + $command -D -p "${PIDFILE}" -f "${CONF}" -f "${CONF_DIR}" -sf $(cat "${PIDFILE}") + eend $? +} diff --git a/haproxy/init.ansible.yml b/haproxy/init.ansible.yml index 24ca4e1..1a26d84 100644 --- a/haproxy/init.ansible.yml +++ b/haproxy/init.ansible.yml @@ -1 +1,2 @@ -- import_playbook: +- import_playbook: pkg.ansible.yml +- import_playbook: service.ansible.yml diff --git a/haproxy/map.yml b/haproxy/map.yml new file mode 100644 index 0000000..aca5e29 --- /dev/null +++ b/haproxy/map.yml @@ -0,0 +1 @@ +conf_dir: /etc/haproxy/conf.d diff --git a/haproxy/pkg.ansible.yml b/haproxy/pkg.ansible.yml new file mode 100644 index 0000000..569e8ba --- /dev/null +++ b/haproxy/pkg.ansible.yml @@ -0,0 +1,34 @@ +- hosts: localhost + vars_files: + - map.yml + tasks: + - name: Install haproxy pkg + package: + state: present + name: + - haproxy + - name: Setup haproxy conf.d directory + file: + state: directory + path: /etc/haproxy/conf.d + owner: root + group: root + mode: '0755' + - name: Populate haproxy config + template: + src: files/haproxy_config.j2 + dest: /etc/haproxy/haproxy.cfg + owner: root + group: root + mode: '0644' + vars: + IPV4: "{{ ansible_default_ipv4.address }}" + - name: Populate haproxy rc-script + template: + src: files/haproxy_rcscript.initd.j2 + dest: /etc/init.d/haproxy + owner: root + group: root + mode: '0755' + vars: + CONF_DIR: "{{ conf_dir }}" diff --git a/haproxy/service.ansible.yml b/haproxy/service.ansible.yml new file mode 100644 index 0000000..ba1329b --- /dev/null +++ b/haproxy/service.ansible.yml @@ -0,0 +1,7 @@ +- hosts: localhost + tasks: + - name: Enable haproxy service + service: + name: haproxy + state: started + enabled: true