[root@node1 opt]# mkdir roles [root@node1 opt]# cd roles/ [root@node1 roles]# mkdir {nginx,uwsgi,redis,mariadb} [root@node1 roles]# ll total 0 drwxr-xr-x 2 root root 6 Apr 10 23:00 mariadb drwxr-xr-x 2 root root 6 Apr 10 23:00 nginx drwxr-xr-x 2 root root 6 Apr 10 23:00 redis drwxr-xr-x 2 root root 6 Apr 10 23:00 uwsgi
[root@localhost roles]# cd nginx/ [root@localhost nginx]# mkdir tasks #必需要有tasks目錄,任務目錄
在tasks中建立yml文件,安裝nginxhtml
[root@node1 tasks]# cat install.yml - name: installnginx yum: name=nginx
建立copyfile.yml,用來複制文件node
[root@node1 tasks]# cat copyfile.yml - name: copyfile template: dest=/etc/nginx/nginx.conf src=nginx.conf
建立啓動文件nginx
[root@node1 tasks]# cat start.yml - name: start service: name=nginx state=started
將三個目錄關聯起來,建立main.yml文件web
[root@node1 tasks]# cat main.yml - import_tasks: install.yml #導入文件,注意順序 - import_tasks: copyfile.yml - import_tasks: start.yml
在服務目錄中建立templates,要與tasks目錄同級redis
[root@node1 nginx]# mkdir templates [root@node1 nginx]# ll total 0 drwxr-xr-x 2 root root 74 Apr 10 23:15 tasks drwxr-xr-x 2 root root 6 Apr 10 23:18 templates
將nginx.conf文件copy到templates目錄下vim
[root@node1 nginx]# cd templates/
[root@node1 templates]# cp /etc/nginx/nginx.conf .
更改配置文件centos
[root@node1 templates]# cat nginx.conf # For more information on configuration, see: # * Official English Documentation: http://nginx.org/en/docs/ # * Official Russian Documentation: http://nginx.org/ru/docs/ user nginx; #啓動用戶 worker_processes {{ansible_processor_vcpus}}; #默認工做進程,通常設置爲CPU的個數或CPU個數的兩倍 error_log /var/log/nginx/error.log; pid /run/nginx.pid; # Load dynamic modules. See /usr/share/nginx/README.dynamic. include /usr/share/nginx/modules/*.conf; events { worker_connections 102400; #每個進程能夠提供多少個線程 } http { 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 /var/log/nginx/access.log main; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; include /etc/nginx/mime.types; default_type application/octet-stream; # Load modular configuration files from the /etc/nginx/conf.d directory. # See http://nginx.org/en/docs/ngx_core_module.html#include # for more information. include /etc/nginx/conf.d/*.conf; server { listen {{port}} default_server; listen [::]:80 default_server; server_name _; root /usr/share/nginx/html; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; location / { } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } } # Settings for a TLS enabled server. # # server { # listen 443 ssl http2 default_server; # listen [::]:443 ssl http2 default_server; # server_name _; # root /usr/share/nginx/html; # # ssl_certificate "/etc/pki/nginx/server.crt"; # ssl_certificate_key "/etc/pki/nginx/private/server.key"; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 10m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # # # Load configuration files for the default server block. # include /etc/nginx/default.d/*.conf; # # location / { # } # # error_page 404 /404.html; # location = /40x.html { # } # # error_page 500 502 503 504 /50x.html; # location = /50x.html { # } # } }
查看CPU的個數session
[root@node1 opt]# ansible localhost -m setup -a "filter=*vcpus*" localhost | SUCCESS => { "ansible_facts": { "ansible_processor_vcpus": 1 #cpu的個數 }, "changed": false }
在與roles同級的目錄下建立啓動文件app
[root@node1 data]# cat nginx.yml - hosts: web remote_user: root roles: - nginx
檢查語法並執行tcp
[root@node1 data]# ansible-playbook --syntax-check nginx.yml [root@node1 data]# ansible-playbook nginx.yml
建立handlers,設置觸發任務
[root@node1 nginx]# ll total 0 drwxr-xr-x 2 root root 21 Apr 10 23:55 handlers drwxr-xr-x 2 root root 74 Apr 10 23:15 tasks drwxr-xr-x 2 root root 23 Apr 10 23:37 templates [root@node1 nginx]# mkdir handlers [root@node1 nginx]# cd handlers/ [root@node1 handlers]# vim main.yml [root@node1 handlers]# cat main.yml - name : restart service: name=nginx state=restarted
更改nginx的copyfile.yml文件
[root@node1 handlers]# cd ../ [root@node1 nginx]# cd tasks/ [root@node1 tasks]# cat copyfile.yml - name: copyfile template: dest=/etc/nginx/nginx.conf src=nginx.conf tags: copy notify: restart
執行nginx.yml文件
[root@node1 templates]# ansible-playbook -t copy nginx.yml
建立變量目錄vars
[root@node1 nginx]# mkdir vars [root@node1 nginx]# cd vars/ [root@node1 vars]# vim main.yml [root@node1 vars]# cat main.yml {port: 90} #定義了一個端口爲90的變量
在模板文件中使用
[root@node1 nginx]# vim templates/nginx.conf
server { listen {{port}} default_server; listen [::]:80 default_server; server_name _; root /usr/share/nginx/html;
執行
[root@node1 data]# ansible-playbook -t copy nginx.yml
查看端口
[root@node1 opt]# netstat -tunlp
建立靜態配置文件files
[root@node1 nginx]# mkdir files
將靜態文件copy到files目錄下
[root@node1 files]# cp /etc/fstab .
配置靜態文件
[root@node1 tasks]# cat main.yml - import_tasks: install.yml - import_tasks: copyfile.yml - import_tasks: start.yml - name: fstab copy: dest=/tmp/test.txt src=fstab
tags: fstab
copy並更改nginx.conf文件
[root@localhost templates]# cat centos6.conf # For more information on configuration, see: # * Official English Documentation: http://nginx.org/en/docs/ # * Official Russian Documentation: http://nginx.org/ru/docs/ user nginx; #啓動用戶 worker_processes {{ansible_processor_vcpus}}; #默認工做進程,通常設置爲CPU的個數或CPU個數的兩倍 error_log /var/log/nginx/error.log; pid /var/run/nginx.pid; #centos6 中nginx.pid默認目錄 # Load dynamic modules. See /usr/share/nginx/README.dynamic. include /usr/share/nginx/modules/*.conf; events { worker_connections 102400; #每個進程能夠提供多少個線程 } http { 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 /var/log/nginx/access.log main; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; include /etc/nginx/mime.types; default_type application/octet-stream; # Load modular configuration files from the /etc/nginx/conf.d directory. # See http://nginx.org/en/docs/ngx_core_module.html#include # for more information. include /etc/nginx/conf.d/*.conf; server { listen 80; #centos6更改,只寫端口 server_name localhosts; #centos6更改server_name root /usr/share/nginx/html; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; location / { } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } } # Settings for a TLS enabled server. # # server { # listen 443 ssl http2 default_server; # listen [::]:443 ssl http2 default_server; # server_name _; # root /usr/share/nginx/html; # # ssl_certificate "/etc/pki/nginx/server.crt"; # ssl_certificate_key "/etc/pki/nginx/private/server.key"; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 10m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # # # Load configuration files for the default server block. # include /etc/nginx/default.d/*.conf; # # location / { # } # # error_page 404 /404.html; # location = /40x.html { # } # # error_page 500 502 503 504 /50x.html; # location = /50x.html { # } # } }
[root@node1 templates]# ll total 8 -rw-r--r-- 1 root root 2646 Apr 11 00:30 centos6.conf -rw-r--r-- 1 root root 2619 Apr 11 00:20 nginx.conf
更改copyfile.yml
[root@localhost tasks]# cat copyfile.yml - name: copyfile template: dest=/etc/nginx/nginx.conf src=nginx.conf tags: copy notify: restart when: ansible_distribution_major_version=="7" - name: centos6 template: dest=/etc/nginx/nginx.conf src=centos6.conf tags: centos6 notify: restart when: ansible_distribution_major_version=="6"
執行nginx.yml文件
[root@node1 templates]# ansible-playbook -t copy,contos6 nginx.yml
查看版本號
[root@node1 opt]# ansible localhost -m setup -a "filter=*ansible_distribution_major_version*" localhost | SUCCESS => { "ansible_facts": { "ansible_distribution_major_version": "7" #版本號 }, "changed": false }
nginx/
├── files 存放靜態的文件
│ └── fstab
├── handlers 存放須要觸發的任務,裏面必須main.yml文件
│ └── main.yml
├── tasks 存放的執行的任務 ,裏面必須main.yml文件
│ ├── copyfile.yml
│ ├── install.yml
│ ├── main.yml
│ └── start.yml
├── templates 存放的模板文件
│ ├── centos6.conf
│ └── nginx.conf
└── vars 存放的是參數,裏面必須main.yml文件
└── main.yml
入口文件
[root@node1 data]# cat nginx.yml - hosts: web remote_user: root roles: - nginx
找tasks目錄下面的main.yml文件,若是import_tasks 就導入
若是遇到了templates,去找templates文件夾下面的對應文件
若是遇到了notify,去找handlers裏面的main.yml文件
若是遇到了copy,去找files裏面的對應文件