【SaltStack】通过Master给Minion安装MySQL
【Master】
IP: 192.168.236.100
【Minion】
IP: 192.168.236.101
二、配置SaltStack
关于SaltStack Master和Minion的安装这里不再赘述!
【配置Master】
1.新建目录
mkdir -p /srv/salt/iso
mkdir -p /srv/salt/install_mysql
2.新建配置文件
(1) /srv/top.sls
base: '*': - cpitables.* - install_zabbix.* - install_mysql.*
(2) /srv/salt/install_mysql/install_mysql.sls
说明:该文件内,执行install_mysql.py脚本的顺序和安装mysql的顺序不能乱,因为安装mysql时需要mysql用户和mysql用户组必须存在,否则会报错!
/soft/install_mysql.py: file.managed: - unless: test -e /soft/install_mysql.py - source: salt://install_mysql/install_mysql.py - user: root - group: root - mode: 775 cmd.run: - name: /usr/bin/python /soft/install_mysql.py - require: - file: /soft/install_mysql.py /soft/mysql-5.7.18.tar.gz: file.managed: - unless: test -e /soft/mysql-5.7.18.tar.gz - source: salt://iso/mysql-5.7.18.tar.gz - user: root - group: root - mode: 644 cmd.run: - cwd: /soft - name: mv mysql-5.7.18.tar.gz /usr/local && cd /usr/local && tar zxf /usr/local/mysql-5.7.18.tar.gz && mv mysql-5.7.18-linux-glibc2.5-x86_64 mysql && cd mysql && chown -R mysql . && chgrp -R mysql . && mkdir -p /usr/local/mysql/log && chown -R mysql:mysql /usr/local/mysql/log && chmod 755 /usr/local/mysql/log && cd /usr/local/ mysql/log && touch mysql-error.log &&chmod 755 mysql-error.log && chown mysql:mysql mysql-error.log && /usr/local/mysql/bin/mysqld --initialize --user=mysql - unless: test -d /usr/local/mysql - require: - file: /soft/mysql-5.7.18.tar.gz /soft/edit_mysql.py: file.managed: - unless: test -e /soft/edit_mysql.py - source: salt://install_mysql/edit_mysql.py - user: root - group: root - mode: 775 cmd.run: - name: /usr/bin/python /soft/edit_mysql.py - require: - file: /soft/edit_mysql.py
(3) /srv/salt/install_mysql/install_mysql.py
#!/usr/bin/python # coding:utf-8 import socket import os content = ''' [client] port = 3306 socket = /tmp/mysql.sock [mysqld] user=mysql port = 3306 server_id = 1 socket=/tmp/mysql.sock basedir =/usr/local/mysql datadir =/usr/local/mysql/data pid-file=/usr/local/mysql/data/mysqld.pid log-error=/usr/local/mysql/log/mysql-error.log ''' def init_mysql(): #修改mysql的配置文件/etc/my.cnf with open('/etc/my.cnf','r+') as newfile: newfile.write(content) newfile.close() #新建mysql用户组和mysql用户 os.system('groupadd mysql ') os.system('useradd -g mysql mysql') init_mysql()
(4) /srv/salt/install_mysql/edit_mysql.py
#!/usr/bin/python # coding:utf-8 import os def edit_mysql(): #Setting mysql env variable os.system("sed -i 's/PATH=$PATH:$HOME\/bin/PATH=$PATH:$HOME\/bin:\/usr\/local\/mysql\/bin/' ~/.bash_profile") os.system('source ~/.bash_profile') #Settting mysql boot os.system('cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql.server') os.system('service mysql.server start') #os.system("set password for root@localhost = password('mysql')") #os.system('flush privileges') edit_mysql()
(6) 将MySQ安装包mysql-5.7.18.tar.gz复制到目录/srv/salt/iso
【Minion】
在客户端新建目录soft
mkdir /soft
三、操作说明
在Master执行如下命令进行操作:
salt '*' state.sls install_mysql.install_mysql
'*' : 表示所有可侦测到的Minion端
state.sls :为固定用法
install_mysql :表示install_mysql.sls文件所在的上次目录,该目录以/etc/salt/master文件中定义的目录为家目录
install_mysql : 为操作用到的sls文件
其他可能用到的命令:
检测Master与Minion的是否连接:
salt '*' test.ping
在安装zabbix前进行验证测试:
salt '*' state.sls install_mysql.install_mysql test=True