> For the complete documentation index, see [llms.txt](https://thorstenkramm.gitbook.io/saltstack/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://thorstenkramm.gitbook.io/saltstack/returner.md).

# Returner & Job-Cache

{% hint style="warning" %}
Seit der Einführung der [onedir-Instllation](https://docs.saltproject.io/salt/install-guide/en/latest/topics/upgrade-to-onedir.html#what-is-onedir) mit Salt 3006 sind Funktionen, welche zusätzliche Python Module benötigen sehr instabil. Die Installation zusätzliche Module gelingt oft nicht. Und bei einem Update von Python Modulen oder vom Salt-Master ist man immer dem Risiko ausgesetzt, dass Funktionen plötzlich nicht mehr funktionieren, weil die Abhängigkeiten der Module nicht aufgelöst werden können.

Die Anbindung des Salt-Masters an eine MySQL Datenbank ist so ein Wackelkandidat.&#x20;

Die Installation des MySQL Python Modules erfordert das Downgrade der Pip Version. Niemand garantiert, dass zukünftige Versionen des Salt-Master mit einer älteren Version von Pip oder dem dann aktuellen MySQL Modul kompatibel sind.

All Salt Funktionen, welche eine Installation eines Moduls mit `salt-pip` erfordern, sollten Sie mit äußerster Versicht verwenden.
{% endhint %}

### MySQL Job-Cache auf dem Master installieren <a href="#mysql-master-returner" id="mysql-master-returner"></a>

<https://docs.saltproject.io/en/latest/topics/jobs/external_cache.html>

#### MariaDB oder MySQL Server auf dem Master installieren

```
apt install mysql-server
/opt/saltstack/salt/bin/python3 -m pip install pip==24.0
salt-pip install PyMySQL
```

> Ab MySQL 5.7 oder MariaDB 10.2 wird der Datentyp JSON unterstützt, welcher für Salt sehr nützlich ist.

#### Benutzer und Datenbank anlegen

<pre class="language-bash" data-overflow="wrap"><code class="lang-bash"><strong>cat &#x3C;&#x3C; EOF | mysql
</strong><strong>CREATE USER IF NOT EXISTS 'salt'@'localhost' IDENTIFIED WITH mysql_native_password BY 'salt';
</strong>GRANT ALL ON salt.* TO 'salt'@'localhost';
CREATE DATABASE IF NOT EXISTS salt DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;
EOF
</code></pre>

Tabellen gemäß [Doku](https://docs.saltproject.io/en/latest/ref/returners/all/salt.returners.mysql.html#module-salt.returners.mysql) per Copy-and-paste anlegen und anschließend den Datentyp JSON verwenden.

```bash
cat << EOF | mysql salt
ALTER TABLE salt_returns MODIFY `return` json;
ALTER TABLE salt_returns MODIFY `full_ret` json;
ALTER TABLE jids MODIFY `load` json;
EOF
```

Oder die folgenden Zeilen per Copy & Paste übernehmen, um die Tabellen anzulegen.

```sql
USE `salt`;

--
-- Table structure for table `jids`
--

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

--
-- Table structure for table `salt_returns`
--

DROP TABLE IF EXISTS `salt_returns`;
CREATE TABLE `salt_returns` (
  `fun` varchar(50) NOT NULL,
  `jid` varchar(255) NOT NULL,
  `return` json NOT NULL,
  `id` varchar(255) NOT NULL,
  `success` varchar(10) NOT NULL,
  `full_ret` json NOT NULL,
  `alter_time` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  KEY `id` (`id`),
  KEY `jid` (`jid`),
  KEY `fun` (`fun`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Table structure for table `salt_events`
--

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;
```

#### Den MySQL-Job-Cache aktivieren

Datei `/etc/salt/master.d/mysql.conf` anlegen mit folgendem Inhalt

{% code title="/etc/salt/master.d/mysql.conf" %}

```
mysql.host: '127.0.0.1'
mysql.user: 'salt'
mysql.pass: 'salt'
mysql.db: 'salt'
mysql.port: 3306
```

{% endcode %}

Datei `/etc/salt/master.d/jobcache.conf` anlegen mit folgendem Inhalt

{% code title="/etc/salt/master.d/jobcache.conf" %}

```
master_job_cache: mysql
```

{% endcode %}

Den Salt-Master neu starten und dann mal ausprobieren

```
salt '*' status.loadavg
salt '*' status.diskusage
```

```sql
select alter_time, id,`return`->'$."/".available' 
from salt_returns 
where fun = 'status.diskusage';

select id,`return`->'$."5-min"' as loadAvg5, alter_time
from salt_returns 
where fun = 'status.loadavg';

select id,`full_ret`->'$.fun_args' as state,success,alter_time 
from salt_returns 
where fun="state.apply";

select id,`return`->'$."/".available' as available 
from salt_returns 
where fun = 'status.diskusage' 
having available>100;
```

Auslesen eines Job-Resultats, der z. B. mit der `state.apply <STATE> --async` ausgelöst wurde.

```sql
select 
  id,
  success,
  alter_time as date, 
  fun,
  `full_ret`->'$."fun_args"' as fun_args 
from salt_returns 
where jid="<JOB-ID>";
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://thorstenkramm.gitbook.io/saltstack/returner.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
