saltstack之grains详解

什么是grains:

是salt的一个组件,负责minion第一次启动的时候采集的静态数据,可以用在salt的模块和其他组件中。每次的minion启动(重启)的时候都会采集。

应用场景:

1.在state中使用 用于配置管理模块
2.在target中使用,用来匹配minion,例如匹配操作系统使用-G的选项
3.用于信息查询,grains保存收集到的客户端的详细信息

查看grains:

# salt 'node23.com' grains.ls  #查看grains的名称  salt 'node23.com' grains.items 查看名称和值
node23.com:
    - SSDs
    - biosreleasedate
    - biosversion
    - cpu_flags
    - cpu_model
    - cpuarch
    - domain
    - fqdn
    - fqdn_ip4
    - fqdn_ip6
    - gpus
    - host
    - hwaddr_interfaces
    - id
    - init
    - ip4_interfaces
    - ip6_interfaces
    - ip_interfaces
    - ipv4
    - ipv6
    - kernel
    - kernelrelease
    - locale_info
    - localhost
    - lsb_distrib_codename
    - lsb_distrib_id
    - lsb_distrib_release
    - machine_id
    - manufacturer
    - master
    - mdadm
    - mem_total
    - nodename
    - num_cpus
    - num_gpus
    - os
    - os_family
    - osarch
    - oscodename
    - osfinger
    - osfullname
    - osmajorrelease
    - osrelease
    - osrelease_info
    - path
    - productname
    - ps
    - pythonexecutable
    - pythonpath
    - pythonversion
    - saltpath
    - saltversion
    - saltversioninfo
    - selinux
    - serialnumber
    - server_id
    - shell
    - virtual
    - zmqversion

使用grains:

1.在命令行中使用:

# salt -G 'os:CentOS' test.ping
node23.com:
    True
node24.com:
    True

# salt -G 'ip_interfaces:eth0:192.168.0.*' cmd.run 'w'
node23.com:
     19:00:46 up 3 days, 12:21,  4 users,  load average: 0.00, 0.00, 0.00
    USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
    root     tty1     -                Sat10    3days  0.08s  0.08s -bash
    root     pts/0    192.168.0.113    18:56    1.00s  0.48s  0.42s /usr/bin/python
    root     pts/1    192.168.0.113    17:33    1:26m  0.04s  0.04s -bash
    root     pts/3    192.168.0.113    16:30    1:30m  0.54s  0.54s -bash
node24.com:
     02:44:30 up 3 days,  8:34,  5 users,  load average: 0.31, 0.08, 0.02
    USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
    root     tty1     :0               Sat18    3days  7.15s  7.15s /usr/bin/Xorg :
    root     pts/0    :0.0             Sat18    3days  0.03s  0.03s /bin/bash
    root     pts/1    192.168.0.113    00:14    2:18m  0.13s  0.13s -bash
    root     pts/3    192.168.0.113    01:17    1:26m  0.03s  0.03s -bash
    root     pts/4    192.168.0.113    02:39    4:33   0.04s  0.04s -bash

2.在SLS中使用grains

base:
  'os:CentOS':
    - match: grain
    - nginx

自定义grains

  1. minon文件中定义
vim /etc/salt/minion
grains:
  web:
    - nginx

/etc/init.d/salt-minion restart 

# salt 'node23.com' grains.get web
node23.com:
    - nginx

2.grains文件中定义

vim /etc/salt/grains
bash: shell
OS: centos6.6

/etc/init.d/salt-minion restart   

# salt 'node23.com' grains.get OS
node23.com:
centos6.6

# salt 'node23.com' grains.get bash
node23.com:
shell

推荐使用自定义grains

posted @ 2016-06-06 23:54  西红柿圆  阅读(1461)  评论(0编辑  收藏  举报