Saltstack数据系统

Posted 一把纯钧

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Saltstack数据系统相关的知识,希望对你有一定的参考价值。

1、grains
grains 是在 minion(客户端)启动时收集到的一些信息,比如操作系统类型、网卡ip等。强调是minion启动时收集到的数据,所以如果改了什么硬件啥的,要重启minion才能收集,要不然还是老数据。

1.1)列出grains的key

[[email protected] salt]# salt ‘192.168.222.145‘ grains.ls

1.2)显示所有的key及对应key的值

[[email protected] salt]# salt ‘192.168.222.145‘ grains.items

1.3)显示单个key对应的值

[[email protected] salt]# salt ‘192.168.222.145‘ grains.item fqdn
192.168.222.145:
    ----------
    fqdn:
        master
[[email protected] salt]# salt ‘192.168.222.145‘ grains.get fqdn
192.168.222.145:
    master

1.4)根据grains匹配并操作,如下匹配所有操作系统是centos的机器并执行w

[[email protected] salt]# salt -G os:CentOS cmd.run ‘w‘
192.168.222.146:
     01:42:30 up  5:59,  1 user,  load average: 0.00, 0.00, 0.00
    USER     TTY      FROM              [email protected]   IDLE   JCPU   PCPU WHAT
    root     pts/1    192.168.222.1    19:43   56:20   0.34s  0.34s -bash
192.168.222.145:
     01:42:32 up  6:01,  1 user,  load average: 0.10, 0.05, 0.01
    USER     TTY      FROM              [email protected]   IDLE   JCPU   PCPU WHAT
    root     pts/1    192.168.222.1    19:41    1.00s  1.07s  0.45s /usr/bin/python

1.5)也可以自定义grains,然后来匹配,在minion的配置文件中修改

[[email protected] ~]# vim /etc/salt/minion
grains:
  roles:
    - webserver
    - memcache

匹配角色是memcache的机器并输出hehe

[[email protected] ~]# salt -G roles:memcache cmd.run ‘echo hehe‘
192.168.222.145:
    hehe

1.6)也可以自己创建一个文件来匹配grains

[[email protected] ~]# vim /etc/salt/grains
web: nginx
这个web不要跟minion里面的重复
[[email protected] ~]# salt -G web:nginx cmd.run ‘uptime‘
192.168.222.145:
     01:52:23 up  6:10,  1 user,  load average: 0.08, 0.05, 0.00

1.7)top.sls里面也可以通过grains匹配

[[email protected] ~]# vim /srv/salt/top.sls 
base:
  ‘web:nginx‘:
    - match: grain
    - apache

[[email protected] ~]# salt ‘*‘ state.highstate
192.168.222.146:
----------
          ID: states
    Function: no.None
      Result: False
     Comment: No Top file or external nodes data matches found.
     Started: 
    Duration: 
     Changes:   

Summary
------------
Succeeded: 0
Failed:    1
------------
Total states run:     1
192.168.222.145:
----------
          ID: apache-install
    Function: pkg.installed
        Name: httpd
      Result: True
     Comment: Package httpd is already installed.
     Started: 03:02:16.972245
    Duration: 1319.294 ms
     Changes:   
----------
          ID: apache-install
    Function: pkg.installed
        Name: httpd-devel
      Result: True
     Comment: Package httpd-devel is already installed.
     Started: 03:02:18.291724
    Duration: 0.646 ms
     Changes:   
----------
          ID: apache-service
    Function: service.running
        Name: httpd
      Result: True
     Comment: Service httpd is already enabled, and is in the desired state
     Started: 03:02:18.293048
    Duration: 50.703 ms
     Changes:   

Summary
------------
Succeeded: 3
Failed:    0
------------
Total states run:     3
ERROR: Minions returned with non-zero exit code

  

 

以上是关于Saltstack数据系统的主要内容,如果未能解决你的问题,请参考以下文章

SaltStack之数据系统

SaltStack之数据系统

saltstack自动化运维系列②之saltstack的数据系统

SaltStack数据系统

Saltstack学习-数据系统

SaltStack数据系统之GrainsPillar