上一篇介绍了使用playbook批量部署zabbix客户端,当时所有的任务全部都是写在一个playbook中,比较臃肿,且代码不可复用。这篇文章我们将介绍通过ansible的roles、include等模块实现批量安装Nginx。
以下为具体实现步骤:
1、目录结构:
[root@deploy playbook-test]# tree . ├── Nginx.yml └── roles └── Nginx ├── files │ ├── Nginx-1.8.0.tar.gz │ ├── Nginx.service │ ├── openssl-1.0.1h.tar.gz │ ├── pcre-8.12.tar.gz │ └── zlib-1.2.11.tar.gz ├── handlers │ └── main.yml ├── tasks │ ├── copy.yml │ ├── group.yml │ ├── install.yml │ ├── main.yml │ ├── service.yml │ ├── template.yml │ ├── unarchive.yml │ └── user.yml ├── templates │ ├── Nginx.conf.j2 │ └── temp.conf.j2 └── vars └── main.yml
cat Nginx.yml
[root@deploy playbook-test]# cat Nginx.yml --- - hosts: web remote_user: root roles: - Nginx
cat tasks/group.yml
[root@deploy Nginx]# cat tasks/group.yml --- - name: 创建Nginx组 group: name: Nginx gid: 202 system: yes state: present
cat tasks/user.yml
[root@deploy Nginx]# cat tasks/user.yml --- - name: 创建Nginx用户 user: name: Nginx uid: 202 group: Nginx shell: /sbin/nologin
cat tasks/copy.yml
[root@deploy Nginx]# cat tasks/copy.yml --- - name: 拷贝源码包 copy: src: "{{ item.src }}" dest: "{{ item.dest }}" with_items: - { src: "Nginx-1.8.0.tar.gz", dest: "/tmp/Nginx-1.8.0.tar.gz" } - { src: "openssl-1.0.1h.tar.gz", dest: "/tmp/openssl-1.0.1h.tar.gz" } - { src: "pcre-8.12.tar.gz", dest: "/tmp/pcre-8.12.tar.gz" } - { src: "zlib-1.2.11.tar.gz", dest: "/tmp/zlib-1.2.11.tar.gz" }
cat tasks/unarchive.yml
--- - name: 解压源码包 unarchive: src: "{{ item.src }}" dest: "{{ item.dest }}" remote_src: yes with_items: - { src: "/tmp/Nginx-1.8.0.tar.gz",dest: "/opt/" } - { src: "/tmp/openssl-1.0.1h.tar.gz",dest: "/opt/" } - { src: "/tmp/pcre-8.12.tar.gz",dest: "/opt/" } - { src: "/tmp/zlib-1.2.11.tar.gz",dest: "/opt/" }
cat tasks/install.yml
--- - name: Start Install Nginx shell: cd /opt/Nginx-1.8.0 && ./configure --prefix=/opt/Nginx --with-pcre=/opt/pcre-8.12 --with-openssl=/opt/openssl-1.0.1h --with-zlib=/opt/zlib-1.2.11 --with-http_stub_status_module --with-http_ssl_module --user=Nginx --group=Nginx && make && make install
cat tasks/template.yml
--- - name: 拷贝配置文件 template: src: "{{ item.src }}" dest: "{{ item.dest }}" with_items: - { src: "Nginx.conf.j2",dest: "/opt/Nginx/conf/Nginx.conf" } #- { src: "temp.conf.j2",dest: "/opt/Nginx/vhosts/temp.conf" } notify: - restart Nginx
cat tasks/service.yml
--- - name: 拷贝启动脚本 copy: src="Nginx.service" dest="/lib/systemd/system/Nginx.service"
cat tasks/main.yml
--- - include: group.yml - include: user.yml - include: copy.yml - include: unarchive.yml - include: install.yml - include: template.yml tags: [conf] - include: service.yml
cat vars/main.yml
root@deploy Nginx]# cat vars/main.yml --- ngxport: "8000" server_name: "www.xxx.com" root_dir: "/web"
cat Nginx.conf.j2
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 2048; } 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 {{ ngxport }}; 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; # } #} include /opt/Nginx/vhosts/*.conf; }
3、批量安装
[root@deploy playbook-test]# ansible-playbook Nginx.yml
[root@deploy playbook-test]# ansible-playbook Nginx.yml PLAY [web] ************************************************************************************************************************************************** TASK [Gathering Facts] ************************************************************************************************************************************** ok: [192.168.131.15] ok: [192.168.131.10] TASK [Nginx : 创建Nginx组] ************************************************************************************************************************************* ok: [192.168.131.15] ok: [192.168.131.10] TASK [Nginx : 创建Nginx用户] ************************************************************************************************************************************ ok: [192.168.131.10] ok: [192.168.131.15] TASK [Nginx : 开始拷贝源码包] ************************************************************************************************************************************** changed: [192.168.131.10] => (item={u'dest': u'/tmp/Nginx-1.8.0.tar.gz', u'src': u'Nginx-1.8.0.tar.gz'}) changed: [192.168.131.15] => (item={u'dest': u'/tmp/Nginx-1.8.0.tar.gz', u'src': u'Nginx-1.8.0.tar.gz'}) changed: [192.168.131.15] => (item={u'dest': u'/tmp/openssl-1.0.1h.tar.gz', u'src': u'openssl-1.0.1h.tar.gz'}) changed: [192.168.131.10] => (item={u'dest': u'/tmp/openssl-1.0.1h.tar.gz', u'src': u'openssl-1.0.1h.tar.gz'}) changed: [192.168.131.15] => (item={u'dest': u'/tmp/pcre-8.12.tar.gz', u'src': u'pcre-8.12.tar.gz'}) changed: [192.168.131.10] => (item={u'dest': u'/tmp/pcre-8.12.tar.gz', u'src': u'pcre-8.12.tar.gz'}) changed: [192.168.131.15] => (item={u'dest': u'/tmp/zlib-1.2.11.tar.gz', u'src': u'zlib-1.2.11.tar.gz'}) changed: [192.168.131.10] => (item={u'dest': u'/tmp/zlib-1.2.11.tar.gz', u'src': u'zlib-1.2.11.tar.gz'}) TASK [Nginx : 开始解压源码包] ************************************************************************************************************************************** changed: [192.168.131.15] => (item={u'dest': u'/opt/', u'src': u'/tmp/Nginx-1.8.0.tar.gz'}) changed: [192.168.131.10] => (item={u'dest': u'/opt/', u'src': u'/tmp/Nginx-1.8.0.tar.gz'}) changed: [192.168.131.15] => (item={u'dest': u'/opt/', u'src': u'/tmp/openssl-1.0.1h.tar.gz'}) changed: [192.168.131.10] => (item={u'dest': u'/opt/', u'src': u'/tmp/openssl-1.0.1h.tar.gz'}) changed: [192.168.131.15] => (item={u'dest': u'/opt/', u'src': u'/tmp/pcre-8.12.tar.gz'}) changed: [192.168.131.10] => (item={u'dest': u'/opt/', u'src': u'/tmp/pcre-8.12.tar.gz'}) changed: [192.168.131.15] => (item={u'dest': u'/opt/', u'src': u'/tmp/zlib-1.2.11.tar.gz'}) changed: [192.168.131.10] => (item={u'dest': u'/opt/', u'src': u'/tmp/zlib-1.2.11.tar.gz'}) TASK [Nginx : Start Install Nginx] ************************************************************************************************************************** changed: [192.168.131.15] changed: [192.168.131.10] TASK [Nginx : 拷贝配置文件] *************************************************************************************************************************************** changed: [192.168.131.10] => (item={u'dest': u'/opt/Nginx/conf/Nginx.conf', u'src': u'Nginx.conf.j2'}) changed: [192.168.131.15] => (item={u'dest': u'/opt/Nginx/conf/Nginx.conf', u'src': u'Nginx.conf.j2'}) TASK [Nginx : 开始拷贝源码包] ************************************************************************************************************************************** changed: [192.168.131.10] changed: [192.168.131.15] RUNNING HANDLER [Nginx : restart Nginx] ********************************************************************************************************************* changed: [192.168.131.10] changed: [192.168.131.15] PLAY RECAP ************************************************************************************************************************************************** 192.168.131.10 : ok=9 changed=6 unreachable=0 Failed=0 skipped=0 rescued=0 ignored=0 192.168.131.15 : ok=9 changed=6 unreachable=0 Failed=0 skipped=0 rescued=0 ignored=0
4、验证客户端上Nginx是否安装成功并启动
[root@deploy playbook-test]# ansible web -m shell -a "ps -ef | grep Nginx|grep -v grep" 192.168.131.10 | CHANGED | rc=0 >> root 71714 1 0 01:25 ? 00:00:00 Nginx: master process /opt/Nginx/sbin/Nginx Nginx 71715 71714 0 01:25 ? 00:00:00 Nginx: worker process 192.168.131.15 | CHANGED | rc=0 >> root 71654 1 0 01:25 ? 00:00:00 Nginx: master process /opt/Nginx/sbin/Nginx Nginx 71655 71654 0 01:25 ? 00:00:00 Nginx: worker process
5、如果我们要增加Nginx站点,可写好对应的模板,将之前template.yml文件中的注释打开,然后推送到vhosts目录,重载Nginx配置文件即可生效。
cat temp.conf.j2
[root@deploy templates]# cat temp.conf.j2 server { listen 80; server_name {{ server_name }}; index index.html index.PHP; root {{ root_dir }}; }
[root@deploy playbook-test]# ansible-playbook Nginx.yml --tags=conf PLAY [web] ************************************************************************************************************************************************** TASK [Gathering Facts] ************************************************************************************************************************************** ok: [192.168.131.15] ok: [192.168.131.10] TASK [Nginx : 拷贝配置文件] *************************************************************************************************************************************** ok: [192.168.131.10] => (item={u'dest': u'/opt/Nginx/conf/Nginx.conf', u'src': u'Nginx.conf.j2'}) ok: [192.168.131.15] => (item={u'dest': u'/opt/Nginx/conf/Nginx.conf', u'src': u'Nginx.conf.j2'}) changed: [192.168.131.15] => (item={u'dest': u'/opt/Nginx/vhosts/temp.conf', u'src': u'temp.conf.j2'}) changed: [192.168.131.10] => (item={u'dest': u'/opt/Nginx/vhosts/temp.conf', u'src': u'temp.conf.j2'}) RUNNING HANDLER [Nginx : restart Nginx] ********************************************************************************************************************* changed: [192.168.131.10] changed: [192.168.131.15] PLAY RECAP ************************************************************************************************************************************************** 192.168.131.10 : ok=3 changed=2 unreachable=0 Failed=0 skipped=0 rescued=0 ignored=0 192.168.131.15 : ok=3 changed=2 unreachable=0 Failed=0 skipped=0 rescued=0 ignored=0
测试:
[root@deploy playbook-test]# ansible web -m shell -a "netstat -antlp | grep 80" 192.168.131.15 | CHANGED | rc=0 >> tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 72534/Nginx: master tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN 72534/Nginx: master 192.168.131.10 | CHANGED | rc=0 >> tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 72596/Nginx: master tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN 72596/Nginx: master
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。