目录
目录结构、主机清单
【1】安装基础环境
【2】webserver - 安装nginx
【3】安装php环境
【4】lb-server
【5】redis安装
【6】 kodcloud
【7】wordpress-proxy
ansible | 10.0.0.10 |
lb-server | 10.0.0.11 |
web01 | 10.0.0.15 |
web02 | 10.0.0.16 |
redis | 10.0.0.20 |
目录结构、主机清单
[root@ansible ~]# mkdir -p ansible/roles/
[root@ansible ~]# cd ansible/roles/
[root@ansible roles]# mkdir -p {nginx,php-fpm,redis}/{tasks,templates,handlers,file
[root@ansible roles]# mkdir -p basic/{tasks,templates,handlers,files}
[root@ansible roles]# mkdir group_vars
[root@ansible roles]# egrep "^[a-Z]" ansible.cfg
inventory = ./hosts
forks = 5
[root@ansible roles]# grep "^[^#]" hosts
[lb-server]
10.0.0.11
[webserver]
10.0.0.15
10.0.0.16
[redis]
10.0.0.20
【1】安装基础环境
[root@ansible roles]# cat basic/tasks/main.yml
- name: Install Nginx Repo
yum_repository:
name: ansible_nginx
description: ansible_nginx_repo
baseurl: http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck: no
gpgkey: https://nginx.org/keys/nginx_signing.key
when: ( ansible_hostname is match ("web*") or ansible_hostname is match ("lb*") )
- name: PHP Reop
yum_repository:
name: ansible_php
description: ansible_php_repo
baseurl: http://us-east.repo.webtatic.com/yum/el7/x86_64/
gpgcheck: no
when: ( ansible_hostname is match ("web*") )
- name: epel
yum:
name: epel-release.noarch
state: present
- name: Install Basices
yum:
name: "{{ item }}"
state: present
loop:
- vim
- wget
- make
- automake
- lrzsz
- tree
- net-tools
- nfs-utils
- rsync
- ntpdate
- unzip
- zip
- name: Disabled Firewall
systemd:
name: firewalld
state: stopped
enabled: no
- name: Disabled Selinux
selinux:
state: disabled
- name: Create Group
group:
name: "{{ group }}"
gid: "{{ id }}"
- name: Create User
user:
name: "{{ user }}"
uid: "{{ id }}"
group: "{{ id }}"
shell: /sbin/nologin
create_home: no
- name: Code Dir
file:
path: "{{ Code_path }}"
owner: "{{ user }}"
group: "{{ group }}"
state: directory
mode: '0755'
recurse: yes
[root@ansible roles]# cat kod.yml
- hosts: all
roles:
- role: basic
【2】webserver - 安装nginx
- tasks任务
- name: Install Nginx
yum:
name: nginx
state: present
- name: MV Default
shell:
cmd: mv /etc/nginx/conf.d/default.conf{,.bak}
creates: /etc/nginx/conf.d/default.conf.bak
- name: Configure Nginx-conf
template:
src: "{{ item.src }}"
dest: "{{ item.dest }}"
loop:
- { src: 'nginx.conf.j2', dest: '/etc/nginx/nginx.conf' }
notify: Restart Nginx
- name: Start Nginx
systemd:
name: nginx
state: started
enabled: yes
- 触发器
[root@ansible roles]# cat nginx/handlers/main.yml
- name: Restart Nginx
systemd:
name: nginx
state: restarted
- 变量文件
[root@ansible roles]# cat group_vars/all
## Basices
group: "www"
id: "666"
user: "www"
## Code Dir
Code_path: "/code/kod"
## webserver
ng_log: "/var/log/nginx"
ng_pid: "/var/run"
work_conn: "1024"
kod_url: "www.kod-test.org"
- nginx配置文件
[root@ansible roles]# ls nginx/templates/
nginx.conf.j2
[root@ansible roles]# cat nginx/templates/nginx.conf.j2
user {{ user }};
worker_processes {{ ansible_processor_vcpus }};
error_log {{ ng_log }}/error.log notice;
pid {{ ng_pid }}/nginx.pid;
events {
worker_connections {{ work_conn }};
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log {{ ng_log }}/access.log main;
sendfile on;
keepalive_timeout 65;
include /etc/nginx/conf.d/*.conf;
}
- 主机清单
[root@ansible roles]# cat kod.yml
#- hosts: all
# roles:
# - role: basic
- hosts: webserver
roles:
- role: nginx
【3】安装php环境
- tasks
[root@ansible roles]# cat php-fpm/tasks/main.yml
- name: Install PHP
yum:
name: "{{ item }}"
state: present
loop:
- php71w
- php71w-cli
- php71w-common
- php71w-devel
- php71w-embedded
- php71w-gd
- php71w-mcrypt
- php71w-mbstring
- php71w-pdo
- php71w-xml
- php71w-fpm
- php71w-mysqlnd
- php71w-opcache
- php71w-pecl-memcached
- php71w-pecl-redis
- php71w-pecl-mongodb
- name: Configure
template:
src: "{{ item.src }}"
dest: "{{ item.dest }}"
owner: "{{ item.owner }}"
group: "{{ item.group }}"
mode: "{{ item.mode }}"
loop:
- { src: 'www.conf.j2', dest: '/etc/php-fpm.d/www.conf', mode: '0644', owner: 'root', group: 'root' }
- { src: 'php.ini.j2', dest: '/etc/php.ini', mode: '0644', owner: 'root', group: 'root' }
notify: Restart PHP-FPM
- name: Start PHP
systemd:
name: php-fpm
state: started
enabled: yes
- handlers
[root@ansible roles]# cat php-fpm/handlers/main.yml
- name: Restart PHP-FPM
systemd:
name: php-fpm
state: restarted
- templates
[root@ansible roles]# ls php-fpm/templates/
php.ini.j2 www.conf.j2
[root@ansible roles]# cat php-fpm/templates/www.conf.j2
[www]
user = {{ user }}
group = {{ group }}
listen = 127.0.0.1:9000
listen.allowed_clients = 127.0.0.1
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
slowlog = /var/log/php-fpm/www-slow.log
php_admin_value[error_log] = /var/log/php-fpm/www-error.log
php_admin_flag[log_errors] = on
php_value[soap.wsdl_cache_dir] = /var/lib/php/wsdlcache
[root@ansible roles]# grep "redis" php-fpm/templates/php.ini.j2
session.save_handler = redis
session.save_path = "tcp://{{ redis_ip }}:{{ redis_port }}?&weight=1&timeout=2.5"
- 变量文件
[root@ansible roles]# cat group_vars/all
## Basices
group: "www"
id: "666"
user: "www"
## Code Dir
Code_path: "/code/kod"
## webserver
ng_log: "/var/log/nginx"
ng_pid: "/var/run"
work_conn: "1024"
kod_url: "www.kod-test.org"
## redis
redis_ip: "10.0.0.20"
redis_port: "6379"
- 主机清单
[root@ansible roles]# cat kod.yml
#- hosts: all
# roles:
# - role: basic
- hosts: webserver
roles:
# - role: nginx
- role: php-fpm
【4】lb-server
[root@ansible roles]# mkdir -p lb-server/{tasks,templates,handlers}
[root@ansible roles]# cat lb-server/tasks/main.yml
- name: Install Nginx
yum:
name: nginx
state: present
- name: MV Default
shell:
cmd: mv /etc/nginx/conf.d/default.conf{,.bak}
creates: /etc/nginx/conf.d/default.conf.bak
- name: Configure Nginx-conf
template:
src: "{{ item.src }}"
dest: "{{ item.dest }}"
loop:
- { src: 'nginx.conf.j2', dest: '/etc/nginx/nginx.conf' }
notify: Restart Nginx
- name: Start Nginx
systemd:
name: nginx
state: started
enabled: yes
- handlers
[root@ansible roles]# cat lb-server/handlers/main.yml
- name: Restart Nginx
systemd:
name: nginx
state: restarted
- templates
[root@ansible roles]# cat lb-server/templates/nginx.conf.j2
user {{ user }};
worker_processes {{ ansible_processor_vcpus }};
error_log {{ ng_log }}/error.log notice;
pid {{ ng_pid }}/nginx.pid;
events {
worker_connections {{ work_conn }};
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log {{ ng_log }}/access.log main;
sendfile on;
keepalive_timeout 65;
include /etc/nginx/conf.d/*.conf;
}
- 变量文件
[root@ansible roles]# cat group_vars/all
## Basices
group: "www"
id: "666"
user: "www"
## Code Dir
Code_path: "/code/kod"
## webserver
ng_log: "/var/log/nginx"
ng_pid: "/var/run"
work_conn: "1024"
kod_url: "www.kod-test.org"
## redis
redis_ip: "10.0.0.20"
redis_port: "6379"
- 主机清单
[root@ansible roles]# cat kod.yml
#- hosts: all
# roles:
# - role: basic
#- hosts: webserver
# roles:
# - role: nginx
# - role: php-fpm
- hosts: lb-server
roles:
- role: lb-server
【5】redis安装
- tasks
[root@ansible roles]# cat redis/tasks/main.yml
- name: Install Redis
yum:
name: redis
state: present
- name: Configure Redis
template:
src: redis.conf.j2
dest: /etc/redis.conf
owner: redis
group: root
mode: '0640'
notify: Restart Redis
- name: Start Redis
systemd:
name: redis
state: started
enabled: yes
- handlers
[root@ansible roles]# cat redis/handlers/maim.yml
- name: Restart Redis
systemd:
name: redis
state: restarte
- templates
[root@ansible roles]# grep "^bind" redis/templates/redis.conf.j2
bind 127.0.0.1 {{ ansible_eth0.ipv4.address }}
- 变量文件
[root@ansible roles]# cat group_vars/all
## Basices
group: "www"
id: "666"
user: "www"
## Code Dir
Code_path: "/code/kod"
## webserver
ng_log: "/var/log/nginx"
ng_pid: "/var/run"
work_conn: "1024"
kod_url: "www.kod-test.org"
## redis
redis_ip: "10.0.0.20"
redis_port: "6379"
- 主机清单
[root@ansible roles]# cat kod.yml
#- hosts: all
# roles:
# - role: basic
#- hosts: webserver
# roles:
# - role: nginx
# - role: php-fpm
#- hosts: lb-server
# roles:
# - role: lb-server
- hosts: redis
roles:
- role: redis
【6】 kodcloud
- tasks
[root@ansible roles]# mkdir -p kod/{tasks,handlers,templates,meta,files}
[root@ansible roles]# vim kod/tasks/main.yml
- name: Copy {{ url }} Nginx
template:
src: kod.conf.j2
dest: /etc/nginx/conf.d/kod.conf
notify: Restart Nginx
- name: Kodcloud Code
unarchive:
src: kodbox.1.31.zip
dest: "{{ Code_path }}"
owner: "{{ user }}"
group: "{{ group }}"
creates: "{{ Code_path }}/index.php"
- meta依赖
dependencies:
- { role: nginx }
- { role: php-fpm }
- template
[root@ansible roles]# vim kod/templates/kod.conf.j2
server {
listen 80;
server_name {{ kod_url }};
root {{ Code_path }};
location / {
index index.php;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
- handlers
[root@ansible roles]# vim kod/handlers/main.yml
- name: Restart Nginx
systemd:
name: nginx
state: restarte
- 变量文件
[root@ansible roles]# vim group_vars/all
## Basices
group: "www"
id: "666"
user: "www"
## Code Dir
Code_path: "/code/kod"
## webserver
ng_log: "/var/log/nginx"
ng_pid: "/var/run"
work_conn: "1024"
kod_url: "www.kod-test.org"
## redis
redis_ip: "10.0.0.20"
redis_port: "6379"
- 主机清单
[root@ansible roles]# cat kod.yml
#- hosts: all
# roles:
# - role: basic
- hosts: webserver
roles:
# - role: nginx
# - role: php-fpm
- role: kod
#- hosts: lb-server
# roles:
# - role: lb-server
#- hosts: redis
# roles:
# - role: redis
【7】wordpress-proxy
tasks
[root@ansible roles]# mkdir -p kod-proxy/{tasks,files,templates,handlers,meta}
[root@ansible roles]# vim kod-proxy/tasks/main.yml
- name: Proxy Configure
template:
src: "{{ item.src }}"
dest: "{{ item.dest }}"
loop:
- { src: 'lb-kod.conf.j2', dest: '/etc/nginx/conf.d/lb-kod.conf' }
- { src: 'proxy_params.j2', dest: '/etc/nginx/proxy_params' }
notify: Restart Nginx
handlers
[root@ansible roles]# cat kod-proxy/handlers/main.yml
- name: Restart Nginx
systemd:
name: nginx
state: restarted
templates
[root@ansible roles]# cat kod-proxy/templates/
lb-kod.conf.j2 proxy_params.j2
[root@ansible roles]# cat kod-proxy/templates/lb-kod.conf.j2
upstream kod {
{% for host in groups['webserver'] %}
server {{ host }};
{%endfor%}
}
server {
listen 80;
server_name {{ kod_url }};
location / {
proxy_pass http://kod;
include proxy_params;
}
}
[root@ansible roles]# cat kod-proxy/templates/proxy_params.j2
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_connect_timeout 60s; # nginx连接后端的超时时间
proxy_read_timeout 60s; # 响应头部超时时间
proxy_send_timeout 60s; # 响应数据主体的超时时间
proxy_buffering on; # 开启缓冲区
proxy_buffer_size 8k; # 缓冲区Header大小
proxy_buffers 4 64k; # 缓冲区数量 * 大小 = 最大接收
meta
[root@ansible roles]# cat kod-proxy/meta/main.yml
dependencies:
- { role: nginx }
变量文件
[root@ansible roles]# vim group_vars/all
## Basices
group: "www"
id: "666"
user: "www"
## Code Dir
Code_path: "/code/kod"
## webserver
ng_log: "/var/log/nginx"
ng_pid: "/var/run"
work_conn: "1024"
kod_url: "www.kod-test.org"
## redis
redis_ip: "10.0.0.20"
redis_port: "6379"
主机清单
[root@ansible roles]# vim kod.yml
#- hosts: all
# roles:
# - role: basic
#- hosts: webserver
# roles:
# - role: nginx
# - role: php-fpm
# - role: kod
# - role: kod-proxy
- hosts: lb-server
roles:
# - role: lb-server
- role: kod-proxy
#- hosts: redis
# roles:
# - role: redis