目录
1、建目录
2、tasks
3、handlers
4、files
5、templates
6、nginx_lb.yml
[root@zbx-server roles]# tree webservers/ webservers/ ├── files │ ├── installed_nginx.sh │ ├── nginx-1.18.0.tar.gz │ └── nginx.service ├── handlers │ └── main.yml ├── tasks │ ├── copy.yml │ ├── group.yml │ ├── main.yml │ ├── script.yml │ ├── template.yml │ ├── unarchive.yml │ ├── user.yml │ └── yum.yml ├── templates │ └── nginx.conf.j2 └── vars 5 directories, 13 files
1、 cat main.yml --- - include: group.yml - include: user.yml - include: unarchive.yml - include: yum.yml - include: script.yml - include: copy.yml - include: template.yml 2、cat group.yml - name: group www group: name=www system=yes 3、cat user.yml - name: user www user: name=www group=www system=yes shell=/sbin/nologin 4、 cat unarchive.yml - name: cp nginx.tar unarchive: src=/etc/ansible/roles/webservers/files/nginx-1.18.0.tar.gz dest=/opt 5、cat yum.yml - name: yum pcre-devel yum: name=pcre-devel state=installed - name: yum pcre-devel yum: name=zlib-devel state=installed - name: yum openssl-devel yum: name=openssl-devel state=installed - name: yum gcc yum: name=gcc state=installed - name: yum gcc-c++ yum: name=gcc-c++ state=installed 6、cat script.yml - name: script installed-nginx script: removes=/opt/nginx-1.18.0 /etc/ansible/roles/webservers/files/installed_nginx.sh 7、cat copy.yml - name: cp nginx.service copy: src=/etc/ansible/roles/webservers/files/nginx.service dest=/usr/lib/systemd/system/ - name: Refresh permissions shell: systemctl daemon-reload 8、cat template.yml - name: cp nginx.conf template: src=nginx.conf.j2 dest=/usr/local/nginx/conf/nginx.conf notify: restart nginx
1、 cat main.yml - name: restart nginx service: name=nginx state=restarted
1、cat installed_nginx.sh #!/bin/bash cd /opt/nginx-1.18.0 ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module make && make install 2、cat nginx.service [Unit] Description=nginx After=network.target [Service] Type=forking 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 3、准备: nginx-1.18.0.tar.gz
1、cat nginx.conf.j2 user www; worker_processes {{ ansible_processor_vcpus }}; events { accept_mutex on; #on可以work进程一起竞争一个请求链接请求多时可以关闭 multi_accept off; #off可以使一个work进程接多个请求 on是一个work接一个请求 worker_connections 1024; use epoll; } http { include mime.types; default_type application/octet-stream; sendfile on; #提高nginx处理静态资源的性能 keepalive_timeout 65; server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } include conf.d/*.conf; }
1、[root@zbx-server roles]# ls nginx_lb.yml webservers 2、 cat nginx_lb.yml - name: installed nginx hosts: 192.168.0.3 roles: - webservers 3、ansible-playbook nginx_lb.yml