ansible-playbook(nginx例)

一、创建目录结构
  cd /etc/ansible/roles/
  mkdir nginx/{files,templates,vars,handlers,meta,default,tasks} -pv
二、files/:存储由copy或script等模块调用的文件;
三、tasks/:配置main.yml文件,用于定义各task;其它的文件需要由main.yml进行“包含”调用;
  cat main.yml
 
    - name: copy package
      copy: src=nginx-1.13.6.tar.gz dest=/usr/local/src/nginx-1.13.6.tar.gz
      tags: cppkg
 
    - name: tar nginx
      shell: cd /usr/local/src;tar -xf nginx-1.13.6.tar.gz
 
    - name: yum install
      yum: name={{ item }} state=latest  
                with_items:
                - openssl-devel
                - pcre-devel
                - gcc
 
              - name: install nginx
                shell: useradd nginx;cd /usr/local/src/nginx-1.13.6;./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre;make && make install
 
              - name: copy conf file
                 template: src=nginx.conf dest=/usr/local/nginx/conf/nginx.conf
 
              - name: systemctl init
      template: src=nginx.service dest=/usr/lib/systemd/system/nginx.service
 
    - name: start nginx service
      service: name=nginx state=started enabled=true
 
六、handlers/:配置main.yml,用于定义各handler;其它的文件需要由main.yml进行“包含”调用;
七、vars/:配置main.yml,用于定义各variable;其它的文件需要由main.yml进行“包含”调用;
  vim main.yml
 
  nginxport: "8080"
  server_name: "web.wsl.com"
  root_dir: "/web"
 
八、templates/:存储由template模块调用的模板文本;
cat nginx.conf
 
user nginx;
worker_processes {{ ansible_processor_vcpus }};
 
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
 
#pid logs/nginx.pid;
 
 
events {
worker_connections 65535;
}
 
 
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 {{ nginxport }};
server_name localhost;
 
#charset koi8-r;
 
#access_log logs/host.access.log main;
 
location / {
root html;
index index.html index.htm;
}
 
#error_page 404 /404.html;
 
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
 
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
 
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
 
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
 
 
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
 
# location / {
# root html;
# index index.html index.htm;
# }
#}
 
 
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
 
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
 
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
 
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
 
# location / {
# root html;
# index index.html index.htm;
# }
#}
 
}
 
[root@w5 templates]# cat nginx.service
 
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target
 
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillMode=process
KillSignal=SIGQUIT
TimeoutStopSec=5
PrivateTmp=true
 
[Install]
WantedBy=multi-user.target
 
九、meta/:配置main.yml,定义当前角色的特殊设定及其依赖关系;其它的文件需要由main.yml进行“包含”调用;
十、default/:配置main.yml,用于设定默认变量;
 
十一、定义一个主调用文件
/etc/ansible/nginx.yaml
- hosts: webserver
remote_user: root
roles:
- nginx
 
十二、检测语法
ansible-playbook --syntax-check /etc/ansible/nginx.yaml
 
playbook: /etc/ansible/nginx.yaml
 
十三、测试部署
ansible-playbook -C /etc/ansible/nginx.yaml
-C 测试
部署:ansible-playbook  /etc/ansible/nginx.yaml
 
 
 

猜你喜欢

转载自www.cnblogs.com/suminem/p/10457640.html