> 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/salt-and-etcd.md).

# Salt & Etcd

### Was ist Etcd?

Etcd ist ein stark konsistenter, verteilter Key-Value-Speicher, der eine zuverlässige Möglichkeit zum Speichern von Daten bietet, auf die ein verteiltes System oder ein Cluster von Rechnern zugreifen muss. Er bewältigt die Wahl des Leitrechners während der Netzwerkpartitionierung und toleriert den Ausfall eines Rechners, sogar des Leitrechners. 📖 [Weitere Informationen](https://etcd.io/docs/v3.5/).

### Voraussetzungen

Installieren Sie den [Etcd](https://etcd.io/) und die vom Salt-Master benötigten Python-Module.

```bash
apt install etcd python3-etcd  
```

{% code title="/etc/default/etcd" %}

```
ETCD_ADVERTISE_CLIENT_URLS="http://0.0.0.0:2379"
ETCD_LISTEN_CLIENT_URLS="http://0.0.0.0:2379"
```

{% endcode %}

Teilen Sie dem Salt-Master mit, wo der Etcd lauscht und registrieren Sie den Etcd als weitere Quelle für Pillars.

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

```yaml
my_etcd_config:
  etcd.host: 127.0.0.1
  etcd.port: 2379
ext_pillar:
  - etcd: my_etcd_config
```

{% endcode %}

### Werte schreiben und lesen

Etcd stellt eine einfache [HTTP API](https://etcd.io/docs/v2.3/api/) zum Schreiben, lesen und löschen von Key-Value-Paaren bereit, die z.B. mit Curl nach dem Schema `http://<HOST>:<PORT>/v2/keys/<KEY> -X<VERB> -d value="<VALUE>"` bedient werden kann.

`curl http://127.0.0.1:2379/v2/keys/message -XPUT -d value="Hello world"`

Die Werte werden als globale, d.h. nicht an Hosts gebundene, Pillars verfügbar,

```
salt una pillar.items
una:
    ----------
    message:
        Hello world
```

In Etcd können aus "Ordner" erstellt werden. Key-Value-Paare innerhalb eines Ordners werden als Listen in Pillars abgebildet.

```
# Create a so-called directory that will respreseng a python list
curl http://127.0.0.1:2379/v2/keys/clusternodes -XPUT -d dir=true

# Append a key/value pair to the directory aka list
curl http://127.0.0.1:2379/v2/keys/clusternodes/una \
-XPUT -d value="192.168.1.1:8080"

# Append another key/value pair to the directory aka list
curl http://127.0.0.1:2379/v2/keys/clusternodes/delia \
-XPUT -d value="192.168.1.2:8080"
```

```
test:
  cmd.run:
    - name: |
        {%- for k,v in pillar['clusternodes'].items() %}
        echo {{k}} {{v}}
        {%- endfor %}
```

Mit der HTTP-Methode DELETE werden Keys aus dem Speicher gelöscht.

```
curl http://127.0.0.1:2379/v2/keys/clusternodes/una -XDELETE
```

### Werte per State in Etcd speichern

Um Werte in den Etcd zu speichern, haben Sie zwei Möglichkeiten.&#x20;

#### Curl + cmd.run

Der einfachste Weg, Daten im Etcd abzulegen, ist der Weg über `cmd.run` und `curl`.

```
join-cluster:
  cmd.run:
    - name: >-
        curl -fs http://{{ pillar['etcd'] }}/v2/keys/clusternodes/{{ grains['id'] }} 
        -XPUT 
        -d value="{{ grains['ipv4'] }}:8080"
```

{% hint style="info" %}
Achten Sie auf 👉 `-f` im curl Commando.

`man curl:`

`-f, --fail (HTTP)` Fail silently (no output at all) on server errors. This is mostly done to enable scripts etc to better deal with failed attempts. In normal cases when an HTTP server fails to deliver a document, it returns an HTML document stating so (which often also describes why and more). This flag will prevent curl from outputting that and return error 22.
{% endhint %}

#### Salt Etcd Modul

Der Minion kann auch mit einem 📖 [Salt-State-Modul](https://docs.saltproject.io/en/latest/ref/states/all/salt.states.etcd_mod.html) mit dem Etcd kommunizieren. Dies setzt aber voraus, dass die Python-Module und Zugangsdaten im Minion verfügbar sind.&#x20;


---

# 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/salt-and-etcd.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.
