Configuring SaltStack Job Results Storage in MySQL

MySQL Client Installation

Install the MySQL Python client package:

yum install MySQL-python -y

Master Configuration

Edit /etc/salt/master to enable MySQL job caching:

master_job_cache: mysql
mysql.host: '10.240.17.103'
mysql.user: 'salt'
mysql.pass: 'salt'
mysql.db: 'salt'
mysql.port: 3306

MySQL Server Setup

Install and configure MySQL server:

yum install mysql-server
/etc/init.d/mysqld start
mysql_secure_installation

Database Schema Creation

Crreate the required database and tables:

CREATE DATABASE `salt` DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;
USE `salt`;

DROP TABLE IF EXISTS `jids`;
CREATE TABLE `jids` (
  `jid` varchar(255) NOT NULL,
  `load` mediumtext NOT NULL,
  UNIQUE KEY `jid` (`jid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE INDEX jid ON jids(jid) USING BTREE;

DROP TABLE IF EXISTS `salt_returns`;
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;

DROP TABLE IF EXISTS `salt_events`;
CREATE TABLE `salt_events` (
`id` BIGINT NOT NULL AUTO_INCREMENT,
`tag` varchar(255) NOT NULL,
`data` mediumtext NOT NULL,
`alter_time` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
`master_id` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
KEY `tag` (`tag`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

grant all on salt.* to salt@10.240.17.103 identified by 'salt';

Service Restart and Verification

Restart the Salt master and verify database connnectivity:

/etc/init.d/salt-master restart
mysql -h 10.240.17.103 -u salt -p salt

Test the configuration:

salt '*' test.ping

Verify job results are stored in the database:

USE salt;
SELECT * FROM salt_returns;

Network Module Examples

salt '*' network.active_tcp
salt '*' network.arp
salt '*' network.connect archlinux.org 80

State Management

View state configuration:

salt 'node1' state.show_top

Infrastructure Management Commands

salt-run manage.status
salt-run manage.versions

Zabbix Agent Automation

Create directory structure:

mkdir -p init/zabbix/logstash
cd init && mkdir files

Download repository configuration:

wget http://mirrors.aliyun.com/repo/epel-7.repo

Create YUM repository state:

/etc/yum.repos.d/epel-7.repo:
  file.managed:
    - source: salt://init/files/epel-7.repo
    - user: root
    - group: root
    - mode: 644

Zabbix agent installation state:

include:
  - init.yum_repo

zabbix-agent:
  pkg.installed:
    - name: zabbix22-agent
    - require:
      - file: /etc/yum.repos.d/epel-7.repo

  file.managed:
    - name: /etc/zabbix/zabbix_agentd.conf
    - source: salt://zabbix/files/zabbix_agentd.conf
    - user: root
    - group: root
    - mode: 644
    - template: jinja
    - defaults:
        ZABBIX_SERVER: 10.240.17.103
        AGENT_HOSTNAME: {{ grains['fqdn'] }}
    - require:
      - pkg: zabbix-agent

  service.running:
    - name: zabbix-agent
    - enable: True
    - watch:
      - file: zabbix-agent
      - pkg: zabbix-agent

zabbix_agentd.conf.d:
  file.directory:
    - name: /etc/zabbix/zabbix_agentd.conf.d
    - watch_in:
      - service: zabbix-agent
    - require:
      - pkg: zabbix-agent
      - file: zabbix-agent

Agent configuration template:

Server={{ ZABBIX_SERVER }}
Hostname={{ AGENT_HOSTNAME }}
INCLUDE=/etc/zabbix_agentd.conf.d/

Redis Cluster Configuration

Redis installation state:

redis-install:
  pkg.installed:
    - name: redis

Redis master configuration:

include:
  - modules.redis.redis-install

redis-master-config:
  file.managed:
    - name: /etc/redis.conf
    - source: salt://redis-cluster/files/redis-master.conf
    - user: root
    - group: root
    - mode: 644
    - template: jinja
    - defaults:
        REDIS_MEM: 1G

redis-master-service:
  service.running:
    - name: redis
    - enable: True
    - watch:
      - file: redis-master-config

Redis configuration modifications:

bind 0.0.0.0
daemonize yes
maxmemory {{ REDIS_MEM }}

Test the state:

salt 'node1' state.sls redis-cluster.redis-master test=True saltenv=prod

Salt SSH Configuration

Install Salt SSH:

yum install salt-ssh -y

Configure roster file /etc/salt/roster:

node1:
  host: 10.240.17.100
  user: root
  passwd: 123.com
  port: 22

node2:
  host: 10.240.17.103
  user: root
  passwd: 123.com
  port: 22

Test Salt SSH connectivity:

salt-ssh '*' test.ping -i
salt-ssh '*' -r 'w'

Salt API Configuration

Install required packages:

yum install salt-api pyOpenSSL

Generate SSL certificates:

salt-call --local tls.create_self_signed_cert

Create API configuration in /etc/salt/master.d/api.conf:

rest_cherrypy:
  host: 10.240.17.103
  port: 8000
  ssl_crt: /etc/pki/tls/certs/localhost.crt
  ssl_key: /etc/pki/tls/certs/localhost.key

Create API user and authentication:

useradd -M -s /sbin/nologin saltapi
echo "saltapi" | passwd saltapi --stdin

Authentication configuration in /etc/salt/master.d/auth.conf:

external_auth:
  pam:
    saltapi:
      - .*
      - '@wheel'
      - '@runner'
      - '@jobs'

Restart services:

/etc/init.d/salt-master restart
/etc/init.d/salt-api restart

API usage examples:

curl -sSk https://10.240.17.103:8000/login \
  -H 'Accept: application/x-yaml' \
  -d username='saltapi' \
  -d password='saltapi' \
  -d eauth='pam'

curl -sSk https://10.240.17.103:8000 \
  -H 'Accept: application/x-yaml' \
  -H 'X-Auth-Token: TOKEN' \
  -d client=local \
  -d tgt='*' \
  -d fun=test.ping

High Availability Master Setup

Configure multi-master in /etc/salt/master:

master:
  - 10.240.17.100
  - 10.240.17.103

Set up shared storage with NFS:

yum install nfs-utils

Configure exports in /etc/exports:

/etc/salt/pki/master 10.240.17.100 *(rw,sync,no_root_squash,no_all_squash)
/srv/salt 10.240.17.100 *(rw,sync,no_root_squash,no_all_squash)

Mount shared directories on secondary master:

mkdir /etc/salt/pki/master
mkdir /srv/salt
mount -t nfs 10.240.17.103:/etc/salt/pki/master /etc/salt/pki/master
mount -t nfs 10.240.17.103:/srv/salt /srv/salt

Tags: SaltStack MySQL automation configuration-management devops

Posted on Tue, 16 Jun 2026 17:30:23 +0000 by Hepp