haproxy roles
[root@ansible-server ansible]
[root@ansible-server ansible]
[root@ansible-server haproxy]
files tasks templates vars
[root@ansible-server haproxy]
[root@ansible-server haproxy]
[root@ansible-server haproxy]
[Unit]
Description=HAProxy Load Balancer
After=syslog.target network.target
[Service]
ExecStartPre=/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q
ExecStart=/usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/lib/haproxy/haproxy.pid
ExecReload=/bin/kill -USR2 $MAINPID
[Install]
WantedBy=multi-user.target
[root@ansible-server haproxy]
SRC_DIR: /usr/local/src
LUA_FILE: lua-5.4.3.tar.gz
HAPROXY_FILE: haproxy-2.4.10.tar.gz
HAPROXY_INSTALL_DIR: /apps/haproxy
STATS_AUTH_USER: admin
STATS_AUTH_PASSWORD: 123456
[root@ansible-server haproxy]
global
maxconn 100000
chroot {{ HAPROXY_INSTALL_DIR }}
stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin
uid 99
gid 99
daemon
pidfile /var/lib/haproxy/haproxy.pid
log 127.0.0.1 local3 info
defaults
option http-keep-alive
option forwardfor
maxconn 100000
mode http
timeout connect 300000ms
timeout client 300000ms
timeout server 300000ms
listen stats
mode http
bind 0.0.0.0:9999
stats enable
log global
stats uri /haproxy-status
stats auth {{ STATS_AUTH_USER }}:{{ STATS_AUTH_PASSWORD }}
[root@ansible-server haproxy]
- name: install CentOS or Rocky depend on the package
yum:
name: gcc,make,gcc-c++,glibc,glibc-devel,pcre,pcre-devel,openssl,openssl-devel,systemd-devel,libtermcap-devel,ncurses-devel,libevent-devel,readline-devel
when:
- (ansible_distribution=="CentOS" or ansible_distribution=="Rocky")
- name: delete lock files
file:
path: "{{ item }}"
state: absent
loop:
- /var/lib/dpkg/lock
- /var/lib/apt/lists/lock
- /var/cache/apt/archives/lock
when:
- ansible_distribution=="Ubuntu"
- name: apt update
apt:
update_cache: yes
force: yes
when:
- ansible_distribution=="Ubuntu"
- name: install Ubuntu depend on the package
apt:
name: gcc,make,openssl,libssl-dev,libpcre3,libpcre3-dev,zlib1g-dev,libreadline-dev,libsystemd-dev
force: yes
when:
- ansible_distribution=="Ubuntu"
[root@ansible-server haproxy]
- name: unarchive lua package
unarchive:
src: "{{ LUA_FILE }}"
dest: "{{ SRC_DIR }}"
- name: get LUA_DIR directory
shell:
cmd: echo {{ LUA_FILE }} | sed -nr 's/^(.*[0-9]).([[:lower:]]).*/\1/p'
register: LUA_DIR
- name: Build and install lua
shell:
chdir: "{{ SRC_DIR }}/{{ LUA_DIR.stdout }}"
cmd: make all test
[root@ansible-server haproxy]
- name: unarchive haproxy package
unarchive:
src: "{{ HAPROXY_FILE }}"
dest: "{{ SRC_DIR }}"
- name: get HAPROXY_DIR directory
shell:
cmd: echo {{ HAPROXY_FILE }} | sed -nr 's/^(.*[0-9]).([[:lower:]]).*/\1/p'
register: HAPROXY_DIR
- name: make Haproxy
shell:
chdir: "{{ SRC_DIR }}/{{ HAPROXY_DIR.stdout }}"
cmd: make -j {{ ansible_processor_vcpus }} ARCH=x86_64 TARGET=linux-glibc USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 USE_SYSTEMD=1 USE_CPU_AFFINITY=1 USE_LUA=1 LUA_INC={{ SRC_DIR }}/{{ LUA_DIR.stdout }}/src/ LUA_LIB={{ SRC_DIR }}/{{ LUA_DIR.stdout }}/src/ PREFIX={{ HAPROXY_INSTALL_DIR }}
- name: make install Haproxy
shell:
chdir: "{{ SRC_DIR }}/{{ HAPROXY_DIR.stdout }}"
cmd: make install PREFIX={{ HAPROXY_INSTALL_DIR }}
[root@ansible-server haproxy]
- name: copy haproxy.service file
copy:
src: haproxy.service
dest: /lib/systemd/system
- name: create haproxy link
file:
src: "../..{{ HAPROXY_INSTALL_DIR }}/sbin/{{ item.src }}"
dest: "/usr/sbin/{{ item.src }}"
state: link
owner: root
group: root
mode: 755
force: yes
with_items:
- src: haproxy
- name: create /etc/haproxy directory
file:
path: /etc/haproxy
state: directory
- name: create /var/lib/haproxy/ directory
file:
path: /var/lib/haproxy/
state: directory
- name: copy haproxy.cfg file
template:
src: haproxy.cfg.j2
dest: /etc/haproxy/haproxy.cfg
- name: Add the kernel
sysctl:
name: net.ipv4.ip_nonlocal_bind
value: "1"
- name: PATH variable
copy:
content: 'PATH={{ HAPROXY_INSTALL_DIR }}/sbin:$PATH'
dest: /etc/profile.d/haproxy.sh
- name: PATH variable entry
shell:
cmd: . /etc/profile.d/haproxy.sh
[root@ansible-server haproxy]
- name: start haproxy
systemd:
name: haproxy
state: started
enabled: yes
daemon_reload: yes
[root@ansible-server haproxy]
- include: install_package.yml
- include: build_lua.yml
- include: build_haproxy.yml
- include: config.yml
- include: service.yml
[root@ansible-server haproxy]
[root@ansible-server ansible]
roles/haproxy/
├── files
│ ├── haproxy-2.4.10.tar.gz
│ ├── haproxy.service
│ └── lua-5.4.3.tar.gz
├── tasks
│ ├── build_haproxy.yml
│ ├── build_lua.yml
│ ├── config.yml
│ ├── install_package.yml
│ ├── main.yml
│ └── service.yml
├── templates
│ └── haproxy.cfg.j2
└── vars
└── main.yml
4 directories, 11 files
[root@ansible-server ansible]
---
- hosts: all
roles:
- role: haproxy
[root@ansible-server ansible]
PLAY [all] ************************************************************************************************************************************
TASK [Gathering Facts] ************************************************************************************************************************
ok: [172.31.0.103]
ok: [172.31.0.104]
ok: [172.31.0.105]
ok: [172.31.0.101]
ok: [172.31.0.102]
TASK [haproxy : install CentOS or Rocky depend on the package] ********************************************************************************
skipping: [172.31.0.104]
skipping: [172.31.0.105]
changed: [172.31.0.103]
changed: [172.31.0.102]
changed: [172.31.0.101]
TASK [haproxy : delete lock files] ************************************************************************************************************
skipping: [172.31.0.101] => (item=/var/lib/dpkg/lock)
skipping: [172.31.0.101] => (item=/var/lib/apt/lists/lock)
skipping: [172.31.0.101] => (item=/var/cache/apt/archives/lock)
skipping: [172.31.0.102] => (item=/var/lib/dpkg/lock)
skipping: [172.31.0.102] => (item=/var/lib/apt/lists/lock)
skipping: [172.31.0.102] => (item=/var/cache/apt/archives/lock)
skipping: [172.31.0.103] => (item=/var/lib/dpkg/lock)
skipping: [172.31.0.103] => (item=/var/lib/apt/lists/lock)
skipping: [172.31.0.103] => (item=/var/cache/apt/archives/lock)
changed: [172.31.0.104] => (item=/var/lib/dpkg/lock)
changed: [172.31.0.105] => (item=/var/lib/dpkg/lock)
changed: [172.31.0.104] => (item=/var/lib/apt/lists/lock)
changed: [172.31.0.105] => (item=/var/lib/apt/lists/lock)
changed: [172.31.0.104] => (item=/var/cache/apt/archives/lock)
changed: [172.31.0.105] => (item=/var/cache/apt/archives/lock)
TASK [haproxy : apt update] *******************************************************************************************************************
skipping: [172.31.0.101]
skipping: [172.31.0.102]
skipping: [172.31.0.103]
changed: [172.31.0.105]
changed: [172.31.0.104]
TASK [haproxy : install Ubuntu depend on the package] *****************************************************************************************
skipping: [172.31.0.101]
skipping: [172.31.0.102]
skipping: [172.31.0.103]
changed: [172.31.0.104]
changed: [172.31.0.105]
TASK [haproxy : unarchive lua package] ********************************************************************************************************
changed: [172.31.0.103]
changed: [172.31.0.104]
changed: [172.31.0.105]
changed: [172.31.0.102]
changed: [172.31.0.101]
TASK [haproxy : get LUA_DIR directory] ********************************************************************************************************
changed: [172.31.0.103]
changed: [172.31.0.104]
changed: [172.31.0.105]
changed: [172.31.0.102]
changed: [172.31.0.101]
TASK [haproxy : Build and install lua] ********************************************************************************************************
changed: [172.31.0.103]
changed: [172.31.0.104]
changed: [172.31.0.102]
changed: [172.31.0.101]
changed: [172.31.0.105]
TASK [unarchive haproxy package] **************************************************************************************************************
changed: [172.31.0.103]
changed: [172.31.0.105]
changed: [172.31.0.104]
changed: [172.31.0.101]
changed: [172.31.0.102]
TASK [haproxy : get HAPROXY_DIR directory] ****************************************************************************************************
changed: [172.31.0.103]
changed: [172.31.0.105]
changed: [172.31.0.104]
changed: [172.31.0.101]
changed: [172.31.0.102]
TASK [haproxy : make Haproxy] *****************************************************************************************************************
changed: [172.31.0.103]
changed: [172.31.0.104]
changed: [172.31.0.102]
changed: [172.31.0.101]
changed: [172.31.0.105]
TASK [haproxy : make install Haproxy] *********************************************************************************************************
changed: [172.31.0.103]
changed: [172.31.0.104]
changed: [172.31.0.105]
changed: [172.31.0.101]
changed: [172.31.0.102]
TASK [copy haproxy.service file] **************************************************************************************************************
changed: [172.31.0.105]
changed: [172.31.0.103]
changed: [172.31.0.104]
changed: [172.31.0.102]
changed: [172.31.0.101]
TASK [create haproxy link] ********************************************************************************************************************
changed: [172.31.0.103] => (item={'src': 'haproxy'})
changed: [172.31.0.105] => (item={'src': 'haproxy'})
changed: [172.31.0.104] => (item={'src': 'haproxy'})
changed: [172.31.0.101] => (item={'src': 'haproxy'})
changed: [172.31.0.102] => (item={'src': 'haproxy'})
TASK [create /etc/haproxy directory] **********************************************************************************************************
changed: [172.31.0.103]
changed: [172.31.0.105]
changed: [172.31.0.104]
changed: [172.31.0.102]
changed: [172.31.0.101]
TASK [create /var/lib/haproxy/ directory] *****************************************************************************************************
changed: [172.31.0.103]
changed: [172.31.0.101]
changed: [172.31.0.104]
changed: [172.31.0.105]
changed: [172.31.0.102]
TASK [copy haproxy.cfg file] ******************************************************************************************************************
changed: [172.31.0.103]
changed: [172.31.0.105]
changed: [172.31.0.104]
changed: [172.31.0.101]
changed: [172.31.0.102]
TASK [haproxy : Add the kernel] ***************************************************************************************************************
changed: [172.31.0.103]
changed: [172.31.0.105]
changed: [172.31.0.104]
changed: [172.31.0.101]
changed: [172.31.0.102]
TASK [haproxy : PATH variable] ****************************************************************************************************************
changed: [172.31.0.103]
changed: [172.31.0.105]
changed: [172.31.0.104]
changed: [172.31.0.101]
changed: [172.31.0.102]
TASK [haproxy : PATH variable entry] **********************************************************************************************************
changed: [172.31.0.103]
changed: [172.31.0.104]
changed: [172.31.0.105]
changed: [172.31.0.102]
changed: [172.31.0.101]
TASK [start haproxy] **************************************************************************************************************************
changed: [172.31.0.103]
changed: [172.31.0.104]
changed: [172.31.0.102]
changed: [172.31.0.101]
changed: [172.31.0.105]
PLAY RECAP ************************************************************************************************************************************
172.31.0.101 : ok=18 changed=17 unreachable=0 failed=0 skipped=3 rescued=0 ignored=0
172.31.0.102 : ok=18 changed=17 unreachable=0 failed=0 skipped=3 rescued=0 ignored=0
172.31.0.103 : ok=18 changed=17 unreachable=0 failed=0 skipped=3 rescued=0 ignored=0
172.31.0.104 : ok=20 changed=19 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
172.31.0.105 : ok=20 changed=19 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
