Shell Kommandos

native Kommandos oder Shell-Skripte ausführen

Obwohl Salt zahlreiche fertige Sate-Module bietet, geht es nicht ohne native Kommandos oder Shell-Skripte

Beispiel Netdata

/srv/salt/init/netdata.sls
#
# Install the Netdata monitoring
# Systemstate is displayed nicely on http port 19999
#
tar:
  pkg.installed: []
  
install-netdata:
  cmd.run:
    - name: |
        set -e
        curl -s -L -O https://github.com/netdata/netdata/releases/download/v1.33.0/netdata-v1.33.0.gz.run
        chmod +x netdata-v1.33.0.gz.run
        ./netdata-v1.33.0.gz.run --accept
        rm -f ./netdata-v1.33.0.gz.run
    - runas: root
    - use_vt: true
    - cwd: /tmp
    - unless: /opt/netdata/bin/netdata -v|grep -q "netdata v1.33.0"

open-firewall:
  cmd.run: 
    - name: |
        ufw allow 19999
        ufw reload
    - onlyif: >-
        test -e /usr/sbin/ufw && ufw status|grep -q "Status: active"
    - unless: ufw status|grep -q 19999

Leiten Sie eine Sammlung von Shell-Kommandos immer mit set -e ein. Andernfalls weted der Salt-Minion nur der Exit-Code des letzten Kommdos aus, um den Erfolg des States zu definieen.

Achten Sie auf >- hinter - onlyif und die nachfolgende sechsfache Einrückung. Diese Syntax verhindert, dass der Doppelpunkt aus Status: active von Yaml interpretiert wird und stattdessen ungefiltert an die Shell weitergereicht wird.

onlyif: A command to run as a check, run the named command only if the command passed to the onlyifoption returns a zero exit status

unless: A command to run as a check, only run the named command if the command passed to the unlessoption returns a non-zero exit status

Mehr Informationen zum Thema Shell-Kommandos hier.

Netdata deinstallieren:

salt '*' cmd.run "pkill netdata"
salt '*' cmd.run "rm -rf /opt/netdata /lib/systemd/system/netdata.service"
salt '*' user.delete netdata remove=True force=True

Last updated