添加变量
配置文件修改
[root@master ~]# vim /etc/salt/master
# highstate format, and is generally just key/value pairs.
pillar_roots: ##添加该内容
base:
- /srv/pillar/base
prod:
- /srv/pillar/prod
#
#ext_pillar:
# - hiera: /etc/hiera.yaml
# - cmd_yaml: cat /etc/salt/yaml
添加变量
[root@master ~]# ls /srv/pillar/prod/
1 mysql.sls nginx.sls php.sls top.sls
[root@master ~]# cat /srv/pillar/prod/mysql.sls
mysql_installdir: /usr/local
mysql_password: 123456
[root@master ~]# cat /srv/pillar/prod/nginx.sls
nginx_installdir: /usr/local
[root@master ~]# cat /srv/pillar/prod/php.sls
php_installdir: /usr/local
php_start: /etc/init.d
[root@master ~]# cat /srv/pillar/prod/top.sls
prod:
'node2':
- nginx
- mysql
- php
[root@master ~]# ls /srv/pillar/prod/
mysql.sls nginx.sls php.sls top.sls
[root@master ~]# cat /srv/pillar/prod/mysql.sls
mysql_installdir: /usr/local
mysql_password: 123456
[root@master ~]# cat /srv/pillar/prod/nginx.sls
nginx_installdir: /usr/local
[root@master ~]# cat /srv/pillar/prod/php.sls
php_installdir: /usr/local
php_start: /etc/init.d
[root@master ~]# cat /srv/pillar/prod/top.sls
prod:
'node2':
- nginx
- mysql
- php
##查看
[root@master prod]# salt node2 pillar.items
node2:
----------
mysql_installdir:
/usr/local
mysql_password:
123456
nginx_installdir:
/usr/local
php_installdir:
/usr/local
php_start:
/etc/init.d
nginx
[root@master nginx]# tree
.
├── files
│ ├── install.sh
│ ├── nginx-1.20.1.tar.gz
│ └── nginx.service.j2
└── install.sls
1 directory, 4 files
[root@master nginx]# cat install.sls
nginc-dev-package:
pkg.installed:
- pkgs:
- pcre-devel
- openssl
- openssl-devel
- gd-devel
- gcc
- gcc-c++
- make
- wget
nginx:
user.present:
- shell: /sbin/nologin
- createhome: false
- system: true
/usr/src/nginx-1.20.1.tar.gz:
file.managed:
- source: salt://modules/web/nginx/files/nginx-1.20.1.tar.gz
nginx-installsh:
cmd.script:
- name: salt://modules/web/nginx/files/install.sh
- unless: test -d {{ pillar['nginx_installdir'] }}/nginx/
/usr/lib/systemd/system/nginx.service:
file.managed:
- source: salt://modules/web/nginx/files/nginx.service.j2
- user: root
- group: root
- mode: '0644'
- template: jinja
systemctl daemon-reload:
cmd.run
[root@master nginx]# cat files/install.sh
#!/bin/bash
cd /usr/src
rm -rf nginx-1.20.1
tar xf nginx-1.20.1.tar.gz
cd nginx-1.20.1
./configure \
--prefix="{{ pillar['nginx_installdir'] }}"/nginx \
--user=nginx \
--group=nginx \
--with-debug \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_image_filter_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--http-log-path=/var/log/nginx/access.log \
--error-log-path=/var/log/nginx/error.log && make && make install
[root@master nginx]# cat files/nginx.service.j2
[Unit]
Description=nginx server daemon
After=network.target
[Service]
Type=forking
ExecStart={{ pillar['nginx_installdir'] }}/nginx/sbin/nginx
ExecStop={{ pillar['nginx_installdir'] }}/nginx/sbin/nginx -s stop
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
mysql
[root@master mysql]# tree
.
├── files
│ ├── install.sh
│ ├── mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz
│ ├── mysqld.service.j2
│ └── mysql.server
└── install.sls
1 directory, 5 files
[root@master mysql]# cat install.sls
ncurses-compat-libs:
pkg.installed
create-mysql-user:
user.present:
- name: mysql
- createhome: false
- system: true
- shell: /sbin/nologin
create-datadir:
file.directory:
- name: /opt/data
- user: mysql
- group: mysql
- mode: '0755'
- makedirs: true
/usr/src/mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz:
file.managed:
- source: salt://modules/database/mysql/files/mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz
- user: root
- group: root
- mode: '0644'
mysql-installsh:
cmd.script:
- name: salt://modules/database/mysql/files/install.sh
- unless: test -d {{ pillar['mysql_installdir'] }}/mysql
{{ pillar['mysql_installdir'] }}/mysql/support-files/mysql.server:
file.managed:
- source: salt://modules/database/mysql/files/mysql.server
/usr/lib/systemd/system/mysqld.service:
file.managed:
- source: salt://modules/database/mysql/files/mysqld.service.j2
- template: jinja
[root@master mysql]# cat files/install.sh
cd /usr/src
tar xf mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz -C /usr/local
ln -s mysql-5.7.34-linux-glibc2.12-x86_64 /usr/local/mysql
chown -R mysql.mysql /usr/local/mysql*
/usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --datadir=/opt/data/
echo "export PATH=/usr/local/mysql/bin:\$PATH" > /etc/profile.d/mysqld.sh
[root@master mysql]# cat files/mysqld.service.j2
[Unit]
Description=Mysql server daemon
After=network.target
[Service]
Type=forking
ExecStart={{ pillar['mysql_installdir'] }}/mysql/support-files/mysql.server start
ExecStop={{ pillar['mysql_installdir'] }}/mysql/support-files/mysql.server stop
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
php
[root@master php]# tree
.
├── files
│ ├── install.sh
│ ├── oniguruma-devel-6.8.2-2.el8.x86_64.rpm
│ ├── php-7.4.24.tar.gz
│ ├── php-fpm
│ ├── php-fpm.conf
│ ├── php-fpm.service
│ ├── php.ini
│ └── www.conf
└── install.sls
1 directory, 9 files
[root@master php]# cat install.sls
/usr/src/oniguruma-devel-6.8.2-2.el8.x86_64.rpm:
file.managed:
- source: salt://modules/application/php/files/oniguruma-devel-6.8.2-2.el8.x86_64.rpm
- user: root
- group: root
- mode: '0644'
cmd.run:
- name: yum -y install /usr/src/oniguruma-devel-6.8.2-2.el8.x86_64.rpm
- unless: yum -y provides oniguruma-devel
dnf -y install epel-release:
cmd.run
dep-pkckages-install:
pkg.installed:
- pkgs:
- sqlite-devel
- libzip-devel
- libxml2
- libxml2-devel
- openssl
- openssl-devel
- bzip2
- bzip2-devel
- libcurl
- libcurl-devel
- libicu-devel
- libjpeg-turbo
- libjpeg-turbo-devel
- libpng
- libpng-devel
- openldap-devel
- pcre-devel
- freetype
- freetype-devel
- gmp
- gmp-devel
- libmcrypt
- libmcrypt-devel
- readline
- readline-devel
- libxslt
- libxslt-devel
- mhash
- mhash-devel
/usr/src/php-7.4.24.tar.gz:
file.managed:
- source: salt://modules/application/php/files/php-7.4.24.tar.gz
- user: root
- group: root
- mode: '0644'
php-installsh:
cmd.script:
- name: salt://modules/application/php/files/install.sh
- unless: test -d {{ pillar['php_installdir'] }}/php7
copy-php:
file.managed:
- names:
- /etc/init.d/php-fpm:
- source: salt://modules/application/php/files/php-fpm
- user: root
- group: root
- mode: '0755'
- /usr/local/php7/etc/php-fpm.conf:
- source: salt://modules/application/php/files/php-fpm.conf
- /usr/local/php7/etc/php-fpm.d/www.conf:
- source: salt://modules/application/php/files/www.conf
- /etc/php.ini:
- source: salt://modules/application/php/files/php.ini
/usr/lib/systemd/system/php-fpm.service:
file.managed:
- source: salt://modules/application/php/files/php-fpm.service.j2
- template: jinja
php-fpm.service:
service.running:
- enable: true
- reload: true
- require:
- cmd: php-installsh
- file: copy-php
[root@master php]# cat files/install.sh
#!/bin/bash
cd /usr/src
rm -rf php-7.4.24
tar xf php-7.4.24.tar.gz -C /usr/local
cd /usr/local/php-7.4.24
./configure --prefix=/usr/local/php7 \
--with-config-file-path=/etc \
--enable-fpm \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-soap \
--with-openssl \
--enable-bcmath \
--with-iconv \
--with-bz2 \
--enable-calendar \
--with-curl \
--enable-exif \
--enable-ftp \
--enable-gd \
--with-jpeg \
--with-zlib-dir \
--with-freetype \
--with-gettext \
--enable-mbstring \
--enable-pdo \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-readline \
--enable-shmop \
--enable-simplexml \
--enable-sockets \
--with-zip \
--enable-mysqlnd-compression-support \
--with-pear \
--enable-pcntl \
--enable-posix && make && make install
[root@master php]# cat files/php-fpm.service.j2
[Unit]
Description=php-fpm server daemon
After=network.target
[Service]
Type=forking
ExecStart={{ pillar['php_start'] }}/php-fpm start
ExecStop={{ pillar['php_start'] }}/php-fpm stop
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
lnmp
[root@master lnmp]# cat main.sls
include:
- lnmp.nginx
- lnmp.mysql
- modules.application.php.install
##nginx
[root@master lnmp]# cat nginx.sls
"Development Tools":
pkg.group_installed
include:
- modules.web.nginx.install
/var/log/nginx:
file.directory:
- user: nginx
- group: nginx
- mode: '0755'
- makedirs: true
{{ pillar['nginx_installdir'] }}/nginx/html/index.php:
file.managed:
- source: salt://zabbix/files/index.php
- user: nginx
- group: nginx
- mode: '0644'
{{ pillar['nginx_installdir'] }}/nginx/conf/nginx.conf:
file.managed:
- source: salt://zabbix/files/nginx.conf
- user: root
- group: root
- mode: '0644'
zabbix-nginx-service:
service.running:
- name: nginx
- enable: true
- reload: true
- watch:
- file: {{ pillar['nginx_installdir'] }}/nginx/conf/nginx.conf
- require:
- cmd: nginx-installsh
- file: {{ pillar['nginx_installdir'] }}/nginx/conf/nginx.conf
##mysql
[root@master lnmp]# cat mysql.sls
lamp-dep-package:
pkg.installed:
- pkgs:
- ncurses-devel
- openssl-devel
- openssl
- cmake
- mariadb-devel
- ncurses-compat-libs
include:
- modules.database.mysql.install
provides-mysql-file:
file.managed:
- user: root
- group: root
- mode: '0644'
- names:
- /etc/my.cnf:
- source: salt://zabbix/files/my.cnf
- /etc/ld.so.conf.d/mysql.conf:
- source: salt://zabbix/files/mysql.conf
/usr/local/include/mysql:
file.symlink:
- target: /usr/local/mysql/include
- unless: test -d /usr/local/mysql
mysqld-start:
service.running:
- name: mysqld
- enable: true
- require:
- cmd: mysql-installsh
set-password:
cmd.run:
- name: /usr/local/mysql/bin/mysql -e "set password=password('{{ pillar['mysql_password'] }}');"
- require:
- service: mysqld-start
- unless: /usr/local/mysql/bin/mysql -uroot -p{{ pillar['mysql_password'] }} -e "