ansible部署haproxy实现lamp和apache两个站点的轮询
node2
部署apache
[root@ansible roles]# ansible-galaxy init httpd
[root@ansible ansible]# vim roles/httpd/tasks/main.yml
[root@ansible ansible]# cat roles/httpd/tasks/main.yml
---
# tasks file for httpd
- name: stop firewalld
service:
name: firewalld
state: stopped
enabled: no
- name: stop selinux
lineinfile:
path: /etc/selinux/config
regexp: '^SELINUX='
line: SELINUX=disabled
- name: stop selinux1
shell: sentenforce 0
- name: mount
mount:
src: /dev/cdrom
path: /mnt
fstype: iso9660
state: mounted
- name: del yum_repository
shell: rm -rf /etc/yum.repos.d/*
- name: yum_repository
yum_repository:
file: server
name: baseos
description: baseos
baseurl: file:///mnt/BaseOS
enabled: yes
gpgcheck: no
- name: yum_repository1
yum_repository:
file: server
name: AppStream
description: AppStream
baseurl: file:///mnt/AppStream
enabled: yes
gpgcheck: no
- name: install httpd
yum:
name: httpd
state: present
- name: enabled httpd
service:
name: httpd
enabled: yes
state: restarted
[root@ansible ansible]# vim httpd.yml
[root@ansible ansible]# cat httpd.yml
---
- name: httpd
hosts: node2
roles:
- httpd
[root@ansible ansible]# ansible-playbook httpd.yml
PLAY RECAP *********************************************************************
node2 : ok=9 changed=6 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node3
配置haproxy
[root@ansible roles]# ansible-galaxy init haproxy
[root@ansible ansible]# yum -y install haproxy
[root@ansible ansible]# cp /etc/haproxy/haproxy.cfg /etc/ansible/roles/haproxy/templates/haproxy.cfg.j2
[root@ansible ansible]# vim roles/haproxy/templates/haproxy.cfg
bind *:80
backend app
balance roundrobin
{% for webservers in groups.webservers %}
server {{ hostvars[webservers].ansible_fqdn }} {{ hostvars[webservers].ansible_ens33.ipv4.address }}:80 check
{% endfor %}
[root@ansible ansible]# cat roles/haproxy/tasks/main.yml
---
# tasks file for haproxy
- name: stop firewalld
service:
name: firewalld
state: stopped
enabled: no
- name: stop selinux
lineinfile:
path: /etc/selinux/config
regexp: '^SELINUX='
line: SELINUX=disabled
- name: stop selinux1
shell: setenforce 0
- name: mount
mount:
src: /dev/cdrom
path: /mnt
fstype: iso9660
state: mounted
- name: del yum_repository
shell: rm -rf /etc/yum.repos.d/*
- name: yum_repository
yum_repository:
file: server
name: baseos
description: baseos
baseurl: file:///mnt/BaseOS
enabled: yes
gpgcheck: no
- name: yum_repository1
yum_repository:
file: server
name: AppStream
description: AppStream
baseurl: file:///mnt/AppStream
enabled: yes
gpgcheck: no
- name: install haproxy
yum:
name: haproxy
state: present
- name: template
template:
src: haproxy.cfg.j2
dest: /etc/haproxy/haproxy.cfg
- name: restat haproxy
service:
name: haproxy
state: restarted
enabled: yes
[root@ansible ansible]# vim haproxy.yml
[root@ansible ansible]# cat haproxy.yml
---
- name:
hosts: webservers
- name:
hosts: node3
roles:
- haproxy
[root@ansible ansible]# ansible-playbook haproxy.yml
PLAY RECAP *********************************************************************
node1 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node2 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node3 : ok=11 changed=5 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
浏览器访问测试