Deploy returner on saltstack
Sentiment: Have you ever heard of Salt's minion too many times? It takes a long time to view the execution results and the screen is full. It is not convenient to check whether the execution is successful. At this time, saltsack's returner function was on the stage. We can save the execution command results to the database, which is convenient to view through the database. However, you may encounter many problems during installation and testing. I would like to thank the O & M @ an of Chengdu O & M group for helping me find out why the database cannot be written. I would like to thank him here.
The execution result of saltstack is written to the MySQL database configuration.
1. create databases and tables
# Assume that our database server is 192.168.3.206, or you can install a database on the master.
Go to the database 192.168.3.206 and perform the following operations:
# Database creation
Create Database salt
Default Character Set utf8
Default collate utf8_general_ci;
# Open a database
Use salt
# Create a table structure
Create Table 'sale _ returns '(
'Fun 'varchar (50) not null,
'Jid' varchar (255) not null,
'Return 'mediumtext not null,
'Id' varchar (255) not null,
'Success' varchar (10) Not null,
'Full _ RET 'mediumtext not null,
Key 'id' ('id '),
Key 'jid' ('jid '),
Key 'fun '('fun ')
) Engine = InnoDB default charset = utf8;
# Create Permissions
Use MySQL
Grant all privileges on salt. * To 'salt' @ '2017. 192.% 'identified by 'Salt _ pass ';
# Only the network segment 192.168.3 is allowed to connect to the database. The user's password is salt_pass.
2. Install the mysqldb Module
Perform the following operations on MINION:
Note: All of my minion is started in/usr/bin/python2.6/usr/bin/salt-Minion-D mode.
This is the python file/usr/bin/python2.6 executed. The system also has a python 2.4.3 version.
Minion is used to operate the database. We will use the/usr/bin/python2.6 command to execute Python later. Otherwise, an error will be reported.
2. 1. Download setuptools
Http://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c8.tar.gz
CD/root/soft
Tar-zxf setuptools-0.6c8.tar.gz
CD setuptools-0.6c8
/Usr/bin/python2.6 setup. py build
/Usr/bin/python2.6 setup. py install
2. Download: mysql-Python
Yum-y install mysql-devel libxml2 libxml2-dev libxslt * zlib GCC OpenSSL python26-mysqldb python26-devel # install these dependency packages, or else an error is reported.
CD/root/soft
Unzip MySQL-python-1.2.5.zip
CD MySQL-python-1.2.5
/Usr/bin/python2.6 setup. py build
/Usr/bin/python2.6 setup. py install
2. 3. Test Module
CD/usr/local
/Usr/bin/python2.6-C 'import mysqldb'
# If no error is reported, it indicates it is normal. Do not test it in the mysql-Python-1.2.5 directory before the test, there will be a conflict.
You can test CD in another directory. Before I test CD/usr/local directory.
3. Write the mysql_return Module
Perform the following operations on the master:
3.1 compile the mysql_return.py File
Vim/srv/salt/_ returners/mysql_return.py
import MySQLdb,json,time__virtualname__ = "mysql_return"def __virtual__(): return __virtualname__def returner(ret): MysqlConn=MySQLdb.connect(host=‘192.168.3.206‘,user=‘salt‘,passwd=‘salt_pass‘,db=‘salt‘,charset="utf8") MysqlCursor=MysqlConn.cursor() SaltSql="insert into salt_returns values (%s,%s,%s,%s,%s,%s)" % (str(json.dumps(ret[‘fun‘])),str(json.dumps(ret[‘jid‘])),"‘"+str(json.dumps(ret[‘return‘]))+"‘",str(json.dumps(ret[‘id‘])),"‘"+str(json.dumps(ret[‘success‘]))+"‘","‘"+str(json.dumps(ret))+"‘") try: MysqlCursor.execute(SaltSql) except: SaltSql="insert into salt_returns values (%s,%s,%s,%s,%s,%s)" % (str("false"),str("false"),"‘"+str(time.strftime("%F %T"))+"‘",str("false"),"‘"+str("false")+"‘","‘"+str(json.dumps(ret))+"‘") MysqlCursor.execute(SaltSql) MysqlConn.commit() MysqlCursor.close() MysqlConn.close()
# Note: the IP address here is the database IP address and the user and password connecting to salt.
3.2 synchronize returner to all minion machines
Salt '*' saltutil. sync_returners
Web100:
-Returners. mysql_return
Web101:
-Returners. mysql_return # If the information on the left is displayed, the module is synchronized successfully.
3.3 run the command to view the result
Salt "*" cmd. Run "hostname" -- return = mysql_return
3.4 Go To The 192.168.3.206 database and perform the following operations:
Mysql-u root-p123
Mysql> use salt
Mysql> select * From salt_returns;
+ --------- + -------------------- + -------------- + -------- + --------- + ----------------------- +
| Fun | jid | return | ID | Success | full_ret |
+ --------- + -------------------- + -------------- + -------- + --------- + ----------------------- +
| Cmd. run | 20140723172632818736 | "srv100.com" | web100 | true | {"fun_args": ["hostname"], "jid": "20140723172632818736", "Return": "srv100.com ", "retcode": 0, "success": True, "fun": "cmd. run "," ID ":" web100 "} |
| Cmd. run | 20140723172632818736 | "srv101.com" | web101 | true | {"fun_args": ["hostname"], "jid": "20140723172632818736", "Return": "srv101.com ", "retcode": 0, "success": True, "fun": "cmd. run "," ID ":" web101 "} |
Note: if there are records in the database, the execution log is successfully written to the database.
This article from the "Chengdu @ A like" blog, please be sure to keep this source http://azhuang.blog.51cto.com/9176790/1517039
Deploying returner on saltstack [3]