一、角色的目录结构
.
├── keepalive
│ ├── default
│ ├── files
│ │ ├── keepalived.conf.backup
│ │ └── keepalived.conf.master
│ ├── handlers
│ │ └── main.yml
│ ├── meta
│ ├── tasks
│ │ └── main.yml
│ ├── templates
│ └── vars
├── mariadb
│ ├── default
│ ├── files
│ ├── handlers
│ ├── meta
│ ├── tasks
│ │ └── main.yml
│ ├── templates
│ └── vars
├── nginx
│ ├── default
│ ├── files
│ │ └── web.conf
│ ├── handlers
│ │ └── main.yml
│ ├── meta
│ ├── tasks
│ │ └── main.yml
│ ├── templates
│ └── vars
└── websrv
├── default
├── files
│ ├── wordpress-4.9.4-zh_CN.tar.gz
│ └── wp-config.php
├── handlers
│ └── main.yml
├── meta
├── tasks
│ ├── httpd.yml
│ ├── main.yml
│ └── wp.yml
├── templates
└── vars
二、编写相应的playbook
vim /etc/ansible/hosts
[lb]
192.168.30.10[2:3]
[websrv]
192.168.30.100
192.168.30.104
[dbsrv]
192.168.30.107
vim keepalive/tasks/main.yml
- name: install keepalive package
yum: name=keepalived state=installed
when: ansible_os_family == "RedHat"
- name: copy config file for master
copy: src=keepalived.conf.master dest=/etc/keepalived/keepalived.conf force=yes
when: ansible_ens33.ipv4.address == "192.168.30.102"
notify: restart keepalive
- name: copy config file for backup
copy: src=keepalived.conf.backup dest=/etc/keepalived/keepalived.conf force=yes
when: ansible_ens33.ipv4.address == "192.168.30.103"
notify: restart keepalive
- name: start keepalive service
service: name=keepalived state=started enabled=yes
vim keepalive/handlers/main.yml
- name: restart keepalive
service: name=keepalived state=restarted
vim keepalive/files/keepalived.conf.backup
! Configuration File for keepalived
global_defs {
router_id node1
vrrp_mcast_group4 224.0.100.10
}
vrrp_instance VI_1 {
state BACKUP
interface ens37
virtual_router_id 20
priority 98
advert_int 1
authentication {
auth_type PASS
auth_pass ed93f89c
}
virtual_ipaddress {
10.1.0.100/8 dev ens37
}
}
vim keepalive/files/keepalived.conf.master
! Configuration File for keepalived
global_defs {
router_id node2
vrrp_mcast_group4 224.0.100.10
}
vrrp_instance VI_1 {
state MASTER
interface ens37
virtual_router_id 20
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass ed93f89c
}
virtual_ipaddress {
10.1.0.100/8 dev ens37
}
}
vim nginx/tasks/main.yml
- name: install nginx
yum: name=nginx state=latest
when: ansible_os_family == "RedHat"
- name: copy config file
copy: src=web.conf dest=/etc/nginx/conf.d/
notify: restart nginx
- name: start nginx
service: name=nginx state=started enabled=yes
vim nginx/handlers/main.yml
- name: restart nginx
service: name=nginx state=restarted
vim nginx/files/web.conf
upstream websrvs {
server 192.168.30.104:80;
server 192.168.30.100:80;
}
server {
server_name www.ilinux.io;
listen 80;
location / {
proxy_pass http://websrvs/;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header host $host;
}
# 1
vim websrv/tasks/httpd.yml
- name: install httpd
yum:
name: ['httpd', 'php', 'php-mysql', 'php-mbstring'] # 安装多个软件的方法
state: installed
- name: start httpd service
service: name=httpd state=started
# 2
vim websrv/tasks/wp.yml
- name: unpack wordpress package
unarchive:
src: wordpress-4.9.4-zh_CN.tar.gz
dest: /var/www/html/
copy: yes
- name: copy config file
copy:
src: wp-config.php
dest: /var/www/html/wordpress/
# 3
vim websrv/tasks/main.yml
- include: httpd.yml
- include: wp.yml
vim websrv/handlers/main.yml
- name: restart httpd
service: name=httpd state=restarted
vim mariadb/tasks/main.yml
- name: install mariadb
yum: name=mariadb-server state=latest
when: ansible_os_family == "RedHat"
- name: start mariadb
service: name=mariadb state=started
- name: grant wordpress user
shell: mysql -e "create database wordpress;grant all on wordpress.* to test@'192.168.30.%' identified by 'centos';"
三、测试
http://www.ilinux.io/wordpress/