> 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/minions-adressieren/grains.md).

# Grains

Was ist unter der Oberfläche eines Systems?

### Wie sieht es unter der Oberfläche aus?

> Salt comes with an interface to derive information about the underlying system. This is called the grains interface, because it presents salt with grains of information. Grains are collected for the operating system, domain name, IP address, kernel, OS type, memory, and many other system properties.

```bash
salt '*' grains.items
salt '*' grains.fetch roles
salt '*' grains.fetch ipv4
salt '*' grains.fetch osfinger
```

### Adressieren Sie Minions über Grains

#### Windows oder Linux?

```bash
salt -G 'kernel:linux' test.ping
salt -G 'Os:Windows' test.ping
salt -G 'osfinger:Ubuntu-18.04' test.ping
```

#### Subnetze ansprechen

```bash
salt -G 'ip_interfaces:eth0:10.8.178.23' test.ping
salt -G 'ipv4:10.8.178.*' test.ping
```

#### Mehrere Grains zum Filtern verwenden

```
salt -C 'G@ipv4:10.8.178.* or G@ipv4:192.168.*' test.ping
salt -C 'G@ipv4:172.* and ( G@kernel:windows or G@kernel:linux )' test.ping
```

{% hint style="info" %}
Achten Sie bei Klammern auf ein Leerzeichen nach der Klammer.
{% endhint %}

📖 <https://docs.saltproject.io/en/latest/topics/targeting/compound.html>

### Eigene Grains setzen

{% code title="/etc/salt/minion.d/grains.conf" %}

```yaml
grains:
  customer: kramm
  city: berlin
  contact:
    - phone: 030 09876
    - street: bla
```

{% endcode %}

Grains kann man auch vom Master den Minions "zuweisen":

```bash
salt '<SOME_MINION>' grains.set role web
salt '<SOME_MINION>' grains.set roles [web,database]
salt 'delia' grains.set 'apps:myApp' '{port: 2209}'
salt delia grains.set test \
  "{'location': {'city':'Essen', 'country':'Germany'}, 'newkey':'value'}" \
  force=True
```

{% hint style="warning" %}
Beachten Sie, dass mit Sie mit `grains.set` vorhandene Grains ggf. überschreiben. Sie können auch `grains.append` verwenden.
{% endhint %}

Grains, welche Sie mit dem zuvor genannten Befehl setzen, werden vom Minion in die Datei `/etc/salt/grains` gespeichert. Sollte eine weitere Grains-Konfigurationsdatei in `/etc/salt/minion.d/` existieren, hat diese Vorrang und die per `grains.set` oder `grains.append` gesetzten Grains werden ignoriert.

📖 Mehr Informationen in der [Projektdokumentation](https://docs.saltproject.io/en/latest/ref/modules/all/salt.modules.grains.html).&#x20;
