Salt - rulare comenzi remote

Postat la Sat 02 March 2019 in tutoriale

Salt pe langa ca aduce si mentine sistemele in starea dorita (ca pachete si configurare), ne permite si executia de comenzi pe masinile slave (minioni) prin module proprii.

Target-area minionilor

Selectia minionilor pe care se vor rula comenzile dorite se poate face prin mai multe metode:

  • prin id - numele - minionului:
salt minion_id test.version

salt 'web*' test.version
  • prin IP minion:
salt -S '192.168.5.9' test.version
  • ca lista de minion:
salt -L web-1,db-2 test.version
  • printr-o variabla din grain:
salt -G 'role:web' test.ping
  • printr-o variabla din stocata in pillar:
salt -I 'variabla:valoare' test.ping
  • prin filtrari compuse:
salt -C 'G@role:web or G@role' test.ping
  • prin expresii regulate:
salt -E '.*' cmd.run 'ls -l | grep foo'

Sunt cazuri cand mumarul de minioni este ridicat si dorim executia comenzilor simultan in loturi (speciifcat ca numar sau procent):

salt minion_id -b 10 test.ping

salt -G 'os:RedHat' --batch-size 25% service.restart httpd

Comenzi utile

Salt include peste 160 de module in versiunea 2019.2.0 din care multe sunt independente de OS.

Cateva din cele mai uzuale comenzi ce pot fi folosite sunt:

  • administrarea pachetelor instalate:
salt minion_id pkg.installed zsh

salt minion_id pkg.remove zsh

salt minion_id pkg.purge httpd

Comanda este OS agnostica, convertind la package managerul sistemului de operare (yum, apt, pkg)

  • informatii despre pachetele instalate:
salt minion_id pkg.list_installed

salt minion_id pkg.version php
  • administrarea serviciilor:
salt minion_id service.start firewalld

salt minion_id service.restart firewalld

salt minion_id service.enable ntpd

salt minion_id service.
  • informatii despre partitii:
salt minion_id disk.usage

salt minion_id disk.percentage

salt minion_id disk.blkid

salt minion_id disk.format /dev/sdX1

salt minion_id mount.filesystems

salt minion_id mount.mount /mnt/foo /dev/sdz1 True
  • adminstrare cron:
salt minion_id cron.ls root

salt minion_id cron.set_job root '30' '4' '*' '*' '*' /usr/local/bin/jobs
  • administrare instrari in /etc/hosts:
salt minion_id hosts.add_host '192.168.25.3' 'db-1'

salt minion_id hosts.get_host 'db-1'

salt minion_id hosts.list_hosts

salt minion_id hosts.rm_host '192.168.25.3' 'db-1'
  • administrarea informatiilor grain (/etc/salt/grains):
salt minion_id grains.set 'apps:myApp:port' 2209

salt minion_id grains.setval role webserver

salt minion_id grains.remove role webserver

salt minion_id grains.ls
  • extragea informatiilor despre minion:
salt minion_id status.all_status

salt minion_id status.diskstats

salt minion_id status.load_avg

salt minion_id status.meminfo
  • administrarea userilor:
salt minion_id user.add name <uid> <gid> <groups> <home> <shell>

salt minion_id user.chgroups foo wheel,root

salt minion_id user.delete name remove=True force=True

De asemenea putem rula comenzi clasice de consola:

salt minion_id cmd.run 'update'

Putem transfera un script si rula pe minioni:

salt minion_id cmd.script salt://my_script.sh

Formatare raspuns

La o prima vedere raspunsul comenzilor lansate de master poate fi greu de interpretat daca am dori integrarea cu alte sisteme.

Pentru un formatarea raspunsului Salt ne pune la dispozitie mai multe formate:

  • json
  • yaml
  • pprint
  • text
salt minion_id grains.items --output json

salt minion_id grains.items --output json --static --output_ident Null

salt minion_id grains.items --output yaml

Articolul face parte din seria Aplicatii pentru managementul configuratiilor