saltstack -- online environment (1)

Keywords: Linux MySQL vim Python JSON

Install mysql on 11
yum install mariadb mariadb-server -y

 

Add in mysql configuration file my.cnf
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake

 

Start mysql after configuration
Set the password before logging in mysql for the first time:
mysql_secure_installation

Add the configuration files at the end of all master and minion

mysql.host: 172.16.1.11
mysql.user: root
mysql.pass: 123456
mysql.db: salt
mysql.port: 3306
Create mysql Library
create database salt;

 

 

Create table
create table `salt_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,
`alter_time` TIMESTAMP default current_timestamp,
key `id` (`id`),
key `jid` (`jid`),
key `fun` (`fun`)
) engine=Innodb default charset=utf8;

Install MySQL Python on both mater and minion

salt '*' cmd.run 'yum install MySQL-python -y'
Configure the PY script VIM salt? Event? To? Mysql.py on the master side
 
#!/bin/env python
#coding=utf8

import json
import salt.config
import salt.utils.event
import MySQLdb

__opts__ = salt.config.client_config('/etc/salt/master')

conn = MySQLdb.connect(host=__opts__['mysql.host'], user=__opts__['mysql.user'], passwd=__opts__['mysql.pass'], db=__opts__['mysql.db'],port=__opts__['mysql.port'],charset='utf8')
corsor = conn.cursor()

event = salt.utils.event.MasterEvent(__opts__['sock_dir'])
for eachevent in event.iter_events(full=True):
        ret = eachevent['data']
        if "salt/job/" in eachevent['tag']:
                if ret.has_key('id') and ret.has_key('return'):
                        if ret['fun'] == "saltutil.find_job":
                                continue
                        sql = '''INSERT INTO `salt_returns`
                                (`fun`,`jid`,`return`,`id`,`success`,`full_ret`)
                                VALUES (%s,%s,%s,%s,%s,%s)'''
                        cursor.execute(sql, (ret['fun'],ret['jid'],json.dumps(ret['return']),ret['id'],ret['success'],json.dumps(ret)))
                        cursor.execute("COMMIT")
        else:
                pass
(Pay attention to the script mysql Where to connect, conn = MySQLdb.connect(host="172.16.1.11",user="root",passwd="930829",db="salt"))

 

Startup script

python salt_event_to_mysql.py &

Check the mysql situation (after executing test.ping in the mater, data in the salt'returns table indicates normal)

minion end group management
Edit master profile
vim /etc/salt/master
nodegroups:
test1: 'E@salt-client*'
test2: 'E@salt-web*' regular
test3: 'N@test1 or N@test2' and
test4 'N@test1 and N@test2' or
Execute in master after configuration
 
salt -N test1 test.ping

 

Modular:
View module
salt '11' sys.doc
 
Test batch execution script
Modify master profile
file_roots:
base:
- /srv/salt
dev:
- /srv/salt/dev
prod:
- /srv/salt/prod

 

Create the above directory
Create etc/script directory under / srv/salt
Create test script in script directory
vim test.sh
while true
do
  sleep 1
  echo 1 > /tmp/log
done

 

Execute in master
salt '*' cmd.script salt://etc/script/test.sh
(Note: the test script will generate a script file starting with tmp in / tmp / directory on minion.)
View tasks running in minion
salt '*' saltutil.running
Kill running tasks
 
salt '*' saltutil.term_job jid

 

Deploy master's files to minion
vim /srv/salt/hosts.sls
/tmp/hosts:
  file.managed:
    - source: salt://etc/hosts
    - user: root
    - group: root
    - mode: 600

 

vim /srv/salt/top.sls
base:
  '*':
    - hosts

 

(the two configuration files are to deploy the hosts file on the master to / TMP / hosts of minion.)
master start deployment
salt '*' state.highstate
Testing another way to deploy
Change etc/hosts in master
salt '*' state.sls hosts
Test another deployment method
Create a new directory hosts in the / srv/salt directory
Copy the hosts.sls file to this directory
Modify the hosts file
salt '*' state.sls hosts.hosts
The fourth method
Rename hosts.sls under the hosts directory to init.sls
implement
 
salt '*' state.sls hosts

Posted by crusty_php on Tue, 31 Dec 2019 15:45:00 -0800