[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目录,任务目录
[root@node1 tasks]# cat install.yml - name: installNginx yum: name=Nginx
[root@node1 tasks]# cat copyfile.yml - name: copyfile template: dest=/etc/Nginx/Nginx.conf src=Nginx.conf
创建启动文件
[root@node1 tasks]# cat start.yml - name: start service: name=Nginx state=started
将三个目录关联起来,创建main.yml文件
[root@node1 tasks]# cat main.yml - import_tasks: install.yml #导入文件,注意顺序 - import_tasks: copyfile.yml - import_tasks: start.yml
在服务目录中创建templates,要与tasks目录同级
[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目录下
[root@node1 Nginx]# cd templates/
[root@node1 templates]# cp /etc/Nginx/Nginx.conf .
更改配置文件
查看cpu的个数
[root@node1 opt]# ansible localhost -m setup -a "filter=*vcpus*" localhost | SUCCESS => { "ansible_facts": { "ansible_processor_vcpus": 1 #cpu的个数 }, "changed": false }
在与roles同级的目录下创建启动文件
[root@node1 data]# cat Nginx.yml - hosts: web remote_user: root roles: - Nginx
检查语法并执行
[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
[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
如果机器的版本不一致,既有centos7也有centos6
[root@localhost templates]# cat centos6.conf # For more @R_333_4045@ion 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/log/Nginx/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 @R_333_4045@ion. 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 { # } # } }centos6.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"
[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 }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。