一 目录结构图
[root@localhost lamp_haproxy]# tree
.
├── group_vars
│ ├── all
│ ├── dbservers
│ ├── lbservers
│ └── webservers
├── hosts
├── roles
│ ├── base-apache
│ │ └── tasks
│ │ └── main.yml
│ ├── common
│ │ ├── files
│ │ │ ├── epel.repo
│ │ │ └── RPM-GPG-KEY-EPEL-6
│ │ ├── handlers
│ │ │ └── main.yml
│ │ ├── tasks
│ │ │ └── main.yml
│ │ └── templates
│ │ ├── iptables.j2
│ │ └── ntp.conf.j2
│ ├── db
│ │ ├── handlers
│ │ │ └── main.yml
│ │ ├── tasks
│ │ │ └── main.yml
│ │ └── templates
│ │ └── my.cnf.j2
│ ├── haproxy
│ │ ├── handlers
│ │ │ └── main.yml
│ │ ├── tasks
│ │ │ └── main.yml
│ │ └── templates
│ │ └── haproxy.cfg.j2
│ ├── nagios
│ │ ├── files
│ │ │ ├── ansible-managed-services.cfg
│ │ │ ├── localhost.cfg
│ │ │ └── nagios.cfg
│ │ ├── handlers
│ │ │ └── main.yml
│ │ ├── tasks
│ │ │ └── main.yml
│ │ └── templates
│ │ ├── dbservers.cfg.j2
│ │ ├── lbservers.cfg.j2
│ │ └── webservers.cfg.j2
│ └── web
│ └── tasks
│ └── main.yml
└── site.yml
二 设备环境说明
1 说明
两台Web主机、1台数据库主机、1台负载均衡器主机、1台监控主机。
2 hosts文件
[root@localhost lamp_haproxy]# cat hosts
[webservers]
web1
web2
[dbservers]
db1
[lbservers]
lb1
[monitoring]
nagios
三 入口文件
1 说明
需要注意的是base-apache角色,由于webservers及monitoring都需要部署Apache环境,为提高复用性,将部署Apache独立成base-apache角色。
2 Site.yml
[webservers]
web1
web2
[dbservers]
db1
[lbservers]
lb1
[monitoring]
nagios
[root@localhost lamp_haproxy]# cat site.yml
---
# This playbook deploys the whole application stack in this site.
# Apply common configuration to all hosts
- hosts: all
roles:
- common
# Configure and deploy database servers.
- hosts: dbservers
user: root
roles:
- db
# Configure and deploy the web servers. Note that we include two roles here,
# the 'base-apache' role which simply sets up Apache, and 'web' which includes
# our example web application.
- hosts: webservers
user: root
roles:
- base-apache
- web
# Configure and deploy the load balancer(s).
- hosts: lbservers
user: root
roles:
- haproxy
# Configure and deploy the Nagios monitoring node(s).
- hosts: monitoring
user: root
roles:
- base-apache
- nagios
四 定义组变量
1 全局变量
1.1 说明
变量作用域为所有主机,all文件定义了匹配所有主机作用域的变量,一般为系统公共类基 础配置,如ntpserver地址、sysctl变量、iptables配置等。
1.2 代码
---
# Variables here are applicable to all host groups
httpd_port: 80
ntpserver: 192.168.1.2
2 定义webservers组的变量
2.1 说明
变量作用域为webservers组主机。webservers文件定义了webservers组作用域的变量。本示例涉及Apache相关配置,其中“iface:'{{ansible_default_ipv4.interface}}'”引用了 Facts获取的本地网卡接口名信息,另外定义了一个GitHub的repository, 方便下载Web测试文件,如内部搭建git版本控制环境,此处也可以修改成本地的服务地址。
2.2 代码
---
# Variables for the web server configuration
# Ethernet interface on which the web server should listen.
# Defaults to the first interface. Change this to:
#
# iface: eth1
#
# ...to override.
#
iface: '{{ ansible_default_ipv4.interface }}'
# this is the repository that holds our sample webapp
repository: https://github.com/bennojoy/mywebapp.git
# this is the sha1sum of V5 of the test webapp.
webapp_version: 351e47276cc66b018f4890a04709d4cc3d3edb0d
3 dbservers组的变量
3.1 说明
变量作用域为dbservers组主机。dbservers文件定义了dbservers组作用域变量,本示例涉及MySQL 数据库的基本应用信息。
3.2 代码
---
# The variables file used by the playbooks in the dbservers group.
# These don't have to be explicitly imported by vars_files: they are autopopulated.
mysqlservice: mysqld
mysql_port: 3306
dbuser: root
dbname: foodb
upassword: abc
4 lbservers组作用域变量
4.1 说明
定义lbservers组作用域变量文件,本示例主要涉及haproxy环境涉及的配置参数值。
4.2 代码
---
# Variables for the HAproxy configuration
# HAProxy supports "http" and "tcp". For SSL, SMTP, etc, use "tcp".
mode: http
# Port on which HAProxy should listen
listenport: 8888
# A name for the proxy daemon, this wil be the suffix in the logs.
daemonname: myapplb
# Balancing Algorithm. Available options:
# roundrobin, source, leastconn, source, uri
# (if persistance is required use, "source")
balance: roundrobin
# Ethernet interface on which the load balancer should listen
# Defaults to the first interface. Change this to:
#
# iface: eth1
#
# ...to override.
#
iface: '{{ ansible_default_ipv4.interface }}'