playbook简介
playbooks是一个不同于使用Ansible命令行执行方式的模式, 其功能更强大灵活; playbook是一个非常简单的配置管理和多主机部署系统, 不同于任何已经存在的模式,可作为一个适合部署复杂应用程序的基础; Playbook可以定制配置, 可以按照指定的操作步骤有序执行, 支持同步和异步方式.
playbook格式
---
- hosts: webserver
remote_user: root
gather_facts: true
vars:
package: nginx
tasks:
- name: transfer nginx source.
template: src=configfile/nginx.repo dest=/etc/yum.repos.d/nginx.repo
- name: install nginx service.
yum: name={{ package }} state=latest
- name: configure nginx service.
template: src=configfile/vhost.conf dest=/etc/nginx/conf.d/default.conf
notify: restart nginx service
- name: install mariadb service.
yum: name={{ item }} state=latest
with_items:
- mariadb-server
- mariadb
- epel-release
notify: restart mariadb service
- name: setting database's password
shell: mysql -uroot -e "create user 'ansible'@'%' identified by '123456';"
- name: install php env
yum: name={{ item }} state=latest
with_items:
- php
- php-fpm
- php-xml
- php-devel
- php-gd
- php-mysql
- php-mbstring
- php-mcrypt
notify: restart php-fpm service
- name: transfer test page
template: src=configfile/index.php dest=/usr/share/nginx/html/index.php
handlers:
- name: restart nginx service
service: name=nginx state=restarted
- name: restart mariadb service
service: name=mariadb state=restarted
- name: restart php-fpm service
service: name=php-fpm state=restarted
# 检查语法是否有误
[root@control_machine ~]# ansible-playbook .ansible/playbook/lnmp_deploy.yaml --syntax-check
playbook: .ansible/playbook/lnmp_deploy.yaml
##### JinJa2模板
variables变量的使用实例: 生成nginx配置文件
# nginx.j2
server {
listen {{ http_port }};
server_name {{ http_name }};
charset koi8-r;
access_log /var/log/nginx/{{ http_name }}.access.log main;
location / {
root /usr/share/nginx/html;
index index.php index.html index.htm;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
root /usr/share/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
protected-mode yes
port {{ redis_port }}
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile "{{ redis_log_path }}"
databases {{ redis_databases }}
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir ./
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
protected-mode yes
port {{ redis_port }}
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile "{{ redis_log_path }}"
databases {{ redis_databases }}
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir ./
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
---
- hosts: webserver
vars:
http_port: 80
http_name: www.qfedu.com
tasks:
- name: transfer jinja2 module file.
template:
src=nginx.j2
dest=/tmp/nginx.conf
---
- hosts: redis
vars:
redis_port: 6379
redis_log_path: "/var/log/redis.log"
redis_databases: 10
tasks:
- name: transfer jinja2 module file.
template:
src=redis.j2
dest=/tmp/redis.conf