09.ansilbe利用playbook部署LNMP环境

1.剧本(playbook)的架构

lnmp
└── roles
    ├── mariadb  # mariadb组件
    │   ├── files
    │   ├── handlers
    │   │   └── main.yml # 
    │   ├── meta
    │   ├── tasks
    │   │   └── main.yml
    │   ├── templates
    │   │   └── my.cnf.j2
    │   └── vars
    │       └── main.yml
    ├── nginx  # nginx部署
    │   ├── files
    │   │   └── index.html #采用测试网页
    │   ├── handlers
    │   │   └── main.yml //存放触发一个事件之后需要做的操作,如:配置文件修改,需要再重启服务
    │   ├── meta
    │   ├── tasks
    │   │   └── main.yml  //存放基本的一些命令操作
    │   ├── templates
    │   │   └── nginx.conf.j2 //存在的基本配置文件模块
    │   └── vars
    │       └── main.yml
    ├── nginx_site.retry
    ├── nginx_site.yml
    ├── php-nginx # php-nginx 部署
    │   ├── files
    │   │   └── index.php
    │   ├── handlers
    │   │   └── main.yml
    │   ├── meta
    │   ├── tasks
    │   │   └── main.yml
    │   ├── templates
    │   │   ├── php.ini.j2
    │   │   └── www.conf.j2
    │   └── vars
    │       └── main.yml
    └── prepare # 前提准备
        ├── files
        ├── handlers
        ├── meta
        ├── tasks
        │   └── main.yml
        ├── templates
        └── vars

 2.每个模块的操作

[root@test-openstack1 roles]# ls
mariadb  nginx  nginx_site.retry  nginx_site.yml  php-nginx  prepare
[root@test-openstack1 roles]# cat nginx_site.yml 
--- 
  - hosts: test-openstack2 
    roles:  # 定义角色 
       - prepare 
       - nginx 
       - mariadb 
       - php-nginx  

 3.nignx目录

nginx/
├── files
│   └── index.html
├── handlers
│   └── main.yml
├── meta
├── tasks
│   └── main.yml
├── templates
│   └── nginx.conf.j2
└── vars
    └── main.yml
 
6 directories, 5 files
 
#index.html
[root@test-openstack1 files]# cat index.html 
<h1>welecome to nginx</h1>
 
#tasks/main.yml
[root@test-openstack1 tasks]# cat main.yml 
--- 
  - name: install nginx 
    yum : name={{item}} state=present
    with_items:
        - epel-release 
        - nginx 
    notify: start nginx 
 
  - name: provider configuration  file 
    template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf 
    notify: reload nginx 
    
  - name: nginx test 
    copy: src=index.html  dest=/usr/share/nginx/html
 
 #handlers/main.yml
 [root@test-openstack1 handlers]# cat main.yml 
--- 
  - name: start nginx 
    service: name=nginx state=started enabled=yes
  
  - name: reload nginx 
    service: name=nginx state=reloaded 
 
 #var/main.yml
[root@test-openstack1 vars]# cat main.yml 
worker_connections: 10240 
nginx_port: 80 
nginx_web_dir: /usr/share/nginx/html
 
#templates/nginx.conf.j2
nginx.conf.j2
[root@test-openstack1 templates]# cat nginx.conf.j2 
 
#user  nobody;
worker_processes  1;
 
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
 
#pid        logs/nginx.pid;
 
 
events {
    worker_connections  {{ worker_connections }};
}
 
 
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       {{ nginx_port }};
        server_name  localhost;
 
        #charset koi8-r;
 
        #access_log  logs/host.access.log  main;
 
        location / {
            root  {{ nginx_web_dir }};
            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;
    #    }
    #}
 
}

3.mariadb目录

[root@test-openstack1 lnmp]# tree roles/mariadb/
roles/mariadb/
├── files
├── handlers
│   └── main.yml
├── meta
├── tasks
│   └── main.yml
├── templates
│   └── my.cnf.j2
└── vars
└── main.yml
 
6 directories, 4 file

4.  php-nginx目录

[root@test-openstack1 lnmp]# tree roles/php-nginx/
roles/php-nginx/
├── files
│   └── index.php
├── handlers
│   └── main.yml
├── meta
├── tasks
│   └── main.yml
├── templates
│   ├── php.ini.j2
│   └── www.conf.j2
└── vars
└── main.yml

5.prepare目录

[root@test-openstack1 lnmp]# tree roles/prepare/
roles/prepare/
├── files
├── handlers
├── meta
├── tasks
│   └── main.yml
├── templates
└── vars

[root@ansible role]# ansible-playbook site_nginx.yaml  --syntax-check   //可以检测是否有语法错误

[root@ansible role]# ansible-playbook site_nginx.yaml     //执行进行部署lnmp环境

原文地址:https://www.cnblogs.com/hackerlin/p/12553219.html