文章目录
playbook实战
通过playbook role部署nginx
1.创建角色目录及相关子目录
[root@ansinble_center tmp]#cd /etc/ansible/roles
[root@ansinble_center roles]#mkdir -pv nginx/{files,templates,vars,handers,meta,default,tasks}
mkdir: created directory ‘nginx’
mkdir: created directory ‘nginx/files’
mkdir: created directory ‘nginx/templates’
mkdir: created directory ‘nginx/vars’
mkdir: created directory ‘nginx/handers’
mkdir: created directory ‘nginx/meta’
mkdir: created directory ‘nginx/default’
mkdir: created directory ‘nginx/tasks’
2.在tasks定义任务
vim nginx/tasks/main.yaml
- name: mkdir /data/www
file: path=/data/www state=directory
- name: create user nginx
user: name=nginx
- name: copy nginx html to remote host
copy: src=index.html dest=/usr/local/nginx/html/
- name: copy nginx package to remote host
copy: src=nginx-1.12.2.tar.gz dest=/tmp/nginx-1.12.2.tar.gz
tags: cppkg
- name: tar nginx
shell: cd /tmp; tar -xf nginx-1.12.2.tar.gz
- name: install pkg
yum: name={{ item }} state=latest
with_items:
- openssl-devel
- pcre-devel
- gcc
- name: install nginx
shell: cd /tmp/nginx-1.12.2; ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre; make && make install
- name: copy conf file nginx.conf
template: src=nginx.conf dest=/usr/local/nginx/nginx.conf
tags: ngxconf
notify: reload nginx service
shell: yum install -y openssl-devel pcre-devel gcc automake
nginx源码安装文件
cd nginx/files
wget http://nginx.org/download/nginx-1.12.2.tar.gz
nginx首页html文件
cd nginx/files
vim index.html
<h1>HELLO WROLD</h1>
nginx配置模版文件
cd /nginx/templates
vim nginx.conf
#user nobody;
worker_processes 1;
events {
worker_connections {{ con_counts }};
}
http {
include 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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
location / {
root {{ html_dir }};
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
3.在vars定义变量
vim nginx/vars/main.yaml
con_counts:“65535”
html_dir:"/data/www"
4.在handers定义触发器
vim nginx/handers/main.yaml
-name:reload nginx service
shell:/usr/local/nginx/sbin/nginx
5.创建nginx.yaml引导文件
cd /etc/ansible/roles
vim nginx.yaml
6.运行
[root@ansinble_center roles]#ansible-playbook nginx.yaml