一、环境
1.实验主机信息
ansible管理机:
主机名:liang
IP:10.0.0.128
nginx(php):
主机名:nginx
IP:10.0.0.131
IP:10.0.0.132
haproxy:
主机名:haproxy
IP:10.0.0.130
mysql(nginx):
主机名:mysql
IP:10.0.0.132
2.实验结构图
二、部署:
1.roles结构与配置
1.1编辑hosts清单
[root@liang ~]# cat /etc/ansible/hosts
# This is the default ansible 'hosts' file.
[haproxy]
10.0.0.130
[nginx]
10.0.0.131
10.0.0.132
[mysql]
10.0.0.132
1.2创建结构目录
[root@liang ~]
1.3创建头文件
[root@liang ~]# cd /roles/
[root@liang roles]# cat site.yml
---
- name: install nginx
hosts: nginx
roles:
- nginx
- name: install mysql
hosts: mysql
roles:
- mysql
- name: install haproxy
hosts: haproxy
roles:
- haproxy
2.nginx+php角色编辑与配置
2.1创建目录与php文件
[root@liang roles]
[root@liang roles]
{{ ansible_default_ipv4.address }
2.2创建nginx角色入口文件
[root@liang roles]
- name: install nginx
yum: name=nginx state=present
- name: install php
yum: name=php state=present
- name: copy file
template: src=index.html.j2 dest=/usr/share/nginx/html/index.html
- name: start nginx
service: name=nginx state=started
3.mysql配置
3.1编辑mysql角色文件
[root@liang roles]# cat mysql/tasks/main.yml
---
- name: install mysql
yum: name={{item}} state=installed
with_items:
- mysql-server
- MySQL-python
- name: start mysql
service: name=mysqld state=started
4.haproxy配置
4.1创建目录与配置文件
[root@liang roles]
[root@liang roles]
global
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
stats socket /var/lib/haproxy/stats
defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000
frontend main *:80
acl url_static path_beg -i /static /images /javascript /stylesheets
acl url_static path_end -i .jpg .gif .png .css .js
use_backend static if url_static
default_backend app
backend static
balance roundrobin
server static 127.0.0.1:8080 check
backend app
balance roundrobin
{% for host in groups['nginx'] %}
server {{hostvars[host]['inventory_hostname']}} {{host}}:80}
{% endfor %}
4.2创建haproxy入口文件
[root@liang roles]# cat haproxy/tasks/main.yml
---
- name: install haproxy
yum: name=haproxy state=installed
- name: template file
template: src=haproxy.cfg.j2 dest=/etc/haproxy/haproxy.cfg owner=root group=root mode=644
- name: start haproxy
service: name=haproxy state=started enabled=yes
三、执行site.yml文件
[root@liang roles]# ansible-playbook site.yml
PLAY [install nginx] ***********************************************************************
TASK [Gathering Facts] *********************************************************************
ok: [10.0.0.132]
ok: [10.0.0.131]
TASK [nginx : install nginx] ***************************************************************
ok: [10.0.0.132]
ok: [10.0.0.131]
TASK [nginx : install php] *****************************************************************
ok: [10.0.0.132]
ok: [10.0.0.131]
TASK [nginx : copy file] *******************************************************************
ok: [10.0.0.132]
ok: [10.0.0.131]
TASK [nginx : start nginx] *****************************************************************
ok: [10.0.0.132]
ok: [10.0.0.131]
PLAY [install mysql] ***********************************************************************
TASK [Gathering Facts] *********************************************************************
ok: [10.0.0.132]
TASK [mysql : install mysql] ***************************************************************
ok: [10.0.0.132] => (item=[u'mysql-server', u'MySQL-python'])
TASK [mysql : start mysql] *****************************************************************
ok: [10.0.0.132]
PLAY [install haproxy] *********************************************************************
TASK [Gathering Facts] *********************************************************************
ok: [10.0.0.130]
TASK [haproxy : install haproxy] ***********************************************************
ok: [10.0.0.130]
TASK [haproxy : template file] *************************************************************
changed: [10.0.0.130]
TASK [haproxy : start haproxy] *************************************************************
changed: [10.0.0.130]
PLAY RECAP *********************************************************************************
10.0.0.130 : ok=4 changed=2 unreachable=0 failed=0
10.0.0.131 : ok=5 changed=0 unreachable=0 failed=0
10.0.0.132 : ok=8 changed=0 unreachable=0 failed=0
四、测试:
[root@liang ~]
10.0.0.132
[root@liang ~]
10.0.0.131
[root@liang ~]
10.0.0.132
[root@liang ~]
10.0.0.131