角色部署haproxy实现轮询
1,部署apache
添加主机清单
[root@ansible ~]# cd /etc/ansible/
[root@ansible ansible]# vim hosts
[webservers]
node1
node2
[haproxy]
node3
定义角色
[root@ansible roles]# ansible-galaxy init httpd
- Role httpd was created successfully
部署tasks任务
[root@ansible roles]# cd httpd/
[root@ansible httpd]# ls
defaults files handlers meta README.md tasks templates tests vars
[root@ansible httpd]# cat tasks/main.yml
---
# tasks file for httpd
- name: stop firewalld
state: stopped
enabled: no
- name: selinux
lineinfile:
path: /etc/selinux/config
regexp: '^SELINUX='
line: SELINUX=disabled
- name: selinux1
shell:
cmd: setenforce 0
- name: mount
mount:
src: /dev/cdrom
path: /mnt
fstype: iso9660
state: mounted
- name: repo1
yum_repository:
name: baseos
description: baseos1
file: server
baseurl: file:///mnt/BaseOS
enabled: yes
gpgcheck: no
- name: repo2
yum_repository:
name: appsteram
description: appstream
file: server
baseurl: file:///mnt/AppStream
enabled: yes
gpgcheck: no
- name: install httpd
yum:
name: httpd
state: present
- name: index.html
template:
src: index.html.j2
dest: /var/www/html/index.html
- name: start httpd
service:
name: httpd
state: restarted
enabled: yes
编写index.html.j2模板
[root@ansible httpd]# cat templates/index.html.j2
welcome to {{ ansible_fqdn }} of {{ ansible_ens160.ipv4.address }}
编写启动模板
[root@ansible ansible]# cat a.yml
---
- name: use httpd
hosts: node2
roles:
- httpd
启动
[root@ansible ansible]# ansible-playbook a.yml
PLAY [use httpd] *********************************************************************************
TASK [Gathering Facts] ***************************************************************************
ok: [node2]
TASK [httpd : stop firewalld] ********************************************************************
ok: [node2]
TASK [httpd : selinux] ***************************************************************************
changed: [node2]
TASK [httpd : selinux1] **************************************************************************
changed: [node2]
TASK [httpd : mount] *****************************************************************************
changed: [node2]
TASK [httpd : repo1] *****************************************************************************
changed: [node2]
TASK [httpd : repo2] *****************************************************************************
changed: [node2]
TASK [install httpd] *****************************************************************************
changed: [node2]
TASK [httpd : index.html] ************************************************************************
changed: [node2]
TASK [start httpd] *******************************************************************************
changed: [node2]
PLAY RECAP ***************************************************************************************
node2 : ok=10 changed=8 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
//测试
[root@ansible ansible]# curl http://node2
welcome to node2.example.com of 192.168.17,128
2,部署haproxy
创建角色
[root@ansible roles]# ansible-galaxy init haproxy
- Role haporoxy was created successfully
[root@ansible roles]# ls
apache haporoxy httpd mysql php
部署tasks 任务
[root@ansible haproxy]# cat tasks/main.yml
---
# tasks file for haproxy
- name: stop firewalld
service:
name: firewalld
state: stopped
enabled: no
- name: stop selinux
shell:
cmd: setenforce 0
- name: mount
mount:
src: /dev/cdrom
path: /mnt
fstype: iso9660
state: mounted
- name: repo1
yum_repository:
name: baseos
description: baseos1
file: server
baseurl: file:///mnt/BaseOS
enabled: yes
gpgcheck: no
- name: repo2
yum_repository:
name: appsteram
description: appstream
file: server
baseurl: file:///mnt/AppStream
enabled: yes
gpgcheck: no
- name: install haproxy
yum:
name: haproxy
state: present
- name: cp
template:
src: haproxy.cfg.j2
dest: /etc/haproxy/haproxy.cfg
- name: restart haproxy
service:
name: haproxy
state: restarted
enabled: yes
定义模板
[root@ansible ansible]# yum -y install haproxy
[root@ansible haproxy]# cp /etc/haproxy/haproxy.cfg templates/haproxy.cfg.j2
[root@ansible haproxy]# vim templates/haproxy.cfg.j2
{% for rww in groups.webservers %}
server {{ hostvars[gbj].ansible_fqdn }} {{hostvars[gbj].ansible_ens160.ipv4.address}}:80 check
{% endfor %}
编写启动文件
[root@ansible ansible]# vim b.yml
[root@ansible ansible]# cat b.yml
---
- name: get webservers
hosts : webservers
- name: use haproxy
hosts: node3
roles:
- haproxy
启动
[root@ansible haproxy]# ansible-playbook b.yml
PLAY [get webservers] ***************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [node1]
ok: [node2]
PLAY [use haparoxy] *****************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [node3]
TASK [haproxy : stop firewalld] *****************************************************************************************************
ok: [node3]
TASK [haproxy : stop selinux] *******************************************************************************************************
changed: [node3]
TASK [haproxy : mount] **************************************************************************************************************
ok: [node3]
TASK [haproxy : repo1] **************************************************************************************************************
ok: [node3]
TASK [haproxy : repo2] **************************************************************************************************************
ok: [node3]
TASK [install haproxy] **************************************************************************************************************
ok: [node3]
TASK [haproxy : cp] *****************************************************************************************************************
changed: [node3]
TASK [restart haproxy] **************************************************************************************************************
changed: [node3]
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=9 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0