Ansible-roles批量部署nginx

我先将大致实现过程记录一下,后续完善:
server1:主控 ansible节点
server2:待部署的nginx节点

先开另外一台虚拟机server3,编译nginx,得到nginx的配置文件,以及制作nginx的启动脚本
在server1 ansible节点:
新建nginx的roles目录,ansible-galaxy init nginx
在此目录下:

files目录下:得到nginx安装包
Ansible-roles批量部署nginx_第1张图片
tasks目录下,编辑main.yml文件:

---
- name: copy package
  copy:
    src: nginx-1.16.0.tar.gz
    dest: /usr/local/nginx-1.16.0.tar.gz

- name: tar nginx
  shell: cd /usr/local/;tar -zxf nginx-1.16.0.tar.gz

- name: yum install devel
  yum: name={{ item }} state=latest
  with_items:
    - openssl-devel
    - pcre-devel
    - gcc

- name: install nginx
  shell: useradd nginx;cd /usr/local/nginx-1.16.0;./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

vars目录下main.yml,定义变量:

---
nginxport: "80"
server_name: "zyw.com"
root_dir: "/web"

templates目录下,存储两个模板文件,nginx.conf和nginx.service:
nginx.conf:

[devops@server1 nginx]$ cat templates/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;
    #    }
    #}

}

nginx.service:

[devops@server1 nginx]$ cat templates/nginx.service 
[Unit]
Description=The nginx Server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target

编辑nginx.yml文件:

[devops@server1 nginx]$ cd ../../..
[devops@server1 ~]$ cd ansible/
[devops@server1 ansible]$ vim nginx.yml 
- hosts: test
  roles:
    - nginx

检测语法,并测试部署:

[devops@server1 ansible]$ ansible-playbook --syntax-check nginx.yml 
[devops@server1 ansible]$ ansible-playbook -C nginx.yml 
[devops@server1 ansible]$ ansible-playbook  nginx.yml

Ansible-roles批量部署nginx_第2张图片

在server2测试:

[devops@server2 local]$ systemctl status nginx.service

Ansible-roles批量部署nginx_第3张图片
Ansible-roles批量部署nginx_第4张图片

浏览器访问测试:

http://172.25.24.2/

Ansible-roles批量部署nginx_第5张图片

你可能感兴趣的:(Ansible)