Automatic deployment of nginx source code through saltstack
Experimental topology
Node name | IP | function |
---|---|---|
server1 | 172.25.21.1 | salt-server |
server3 | 172.25.21.3 | Salt minion, nginx server |
The basic configuration of each node will not be described in detail. See my blog for details Saltstack automation management tool
The configuration on the server side is as follows:
In the file roots directory, create the directory and sls file
[root@server1 salt]# mkdir nginx # Create nginx directory
[root@server1 salt]# cd nginx/ # Entry directory
Edit install build script
[root@server1 nginx]# vim install.sls
//Edit as follows:
nginx_install: # Dependency installation required by nginx
pkg.installed:
- pkgs:
- gcc-c++
- openssl-devel
- pcre-devel
- zlib-devel
file.managed: # File management, source package push
- name: /root/nginx-1.14.0.tar.gz
- source: salt://nginx/files/nginx-1.14.0.tar.gz
cmd.run: # Execute the command. Decompress and compile
- name: cd /root/ && tar zxf nginx-1.14.0.tar.gz && cd nginx-1.14.0 && sed -i.bak 's/#define NGINX_VER "nginx\/" NGINX_VERSION/#define NGINX_VER "nginx"/g' src/core/nginx.h && sed -i.bak 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && ./configure --prefix=/usr/local/nginx --with-file-aio --with-threads --with-http_ssl_module --with-http_stub_status_module &> /dev/null && make &>/dev/null && make install &>/dev/null
- creates: /usr/local/nginx
Edit complete, push to minion
[root@server1 salt]# salt server3 state.sls nginx.install
The results are as follows:
Push succeeded, nginx compilation and installation on server3 completed
Can be viewed in server3
Edit user script
Create a script under / srv/salt for creating service users
[root@server1 salt]# mkdir users
[root@server1 salt]# cd users
[root@server1 users]# vim nginx.sls
//The editing contents are as follows:
nginx-group:
group.present: # Set up user groups
- name: nginx # Group name
- gid: 800 # Group ID
nginx-user: # Build user
user.present:
- name: nginx # Username
- uid: 800 # Specify user ID
- gid: 800 # Specified group ID
- shell: /sbin/nologin # Specify Shell type
- createhome: False # Not created. User home directory
- home: /usr/local/nginx # Specify user home directory
~
After editing, push to server3 (green indicates successful execution, blue indicates modified content)
[root@server1 users]# salt server3 state.sls users.nginx
View on server3
Write scripts related to service startup
[root@server1 salt]# vim nginx/service.sls
include: # Other scripts included
- nginx.install
- users.nginx
- pkgs.make
/usr/local/nginx/conf/nginx.conf: # Profile of the service
file.managed:
- source: salt://nginx/files/nginx.conf
nginx-service: # Information about service startup
file.managed:
- name: /etc/init.d/nginx # Start your steps
- source: salt://nginx/files/nginx
- mode: 755
service.running: # running method of service module
- name: nginx
- reload: True
- watch:
- file: /usr/local/nginx/conf/nginx.conf
Push
[root@server1 salt]# salt server3 nginx.service
After push, view on server3: