当前位置: 首页 > linux, 云计算, 系统, 系统工具 > 正文

saltstack 使用 grains 变量进行条件匹配

saltstack 在实际使用中,由于服务器的硬件,软件亦或操作系统不同,如果要实现一个通用配置往往要考虑到兼容性,简单的话可以使用单独的配置文件或者模块来针对不同node进行配置,如果使用一个通用的配置匹配多种条件就需要使用 saltstack grains 了。

SaltStack官网文档这样说到:
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.

The grains interface is made available to Salt modules and components so that the right salt minion commands are automatically available on the right systems.

It is important to remember that grains are bits of information loaded when the salt minion starts, so this information is static. This means that the information in grains is unchanging, therefore the nature of the data is static. So grains information are things like the running kernel, or the operating system.

注意一点: Grains 不区分大小写,F00 和 f00 表示同样的grains。

Grains的一些命令:

salt '*' grains.item osfinger
salt '*' grains.items
salt '*' grains.item osfinger
salt '*' grains.item productname
salt -G 'osfinger:CentOS-6' cmd.run "uptime"
salt -G 'cpuarch:x86_64' grains.item num_cpus
salt -G 'productname:KVM' cmd.run "uptime"

下面是一个简单的使用例子,salt使用grains区分不同的操作系统版本来安装vim和推送sshd_config配置:

# cat top.sls 
base:
  '*':
    - common.baseinit
# cat common/baseinit.sls
{% if grains['osfinger'] == 'CentOS-6' %}
vim-enhanced:
  pkg:
    - name: vim-enhanced
    - installed
centos6_sysctl_conf:
  file.managed:
    - name: /etc/sysctl.conf
    - source:
      - salt://file/sysctl_conf_centos6.file
openssh-server:
  pkg.installed
openssh-clients:
  pkg.installed
sshd_config:
  file.managed:
    - user: root
    - group: root
    - mode: 644  
    - name: /etc/ssh/sshd_config
    - source:
      - salt://file/sshd_config.file
    - require:
      - pkg: openssh-server      

sshd_banner:
  file.managed:
    - user: root
    - group: root
    - mode: 644
    - name: /etc/ssh/sshd_banner
    - source: salt://file/sshd_banner.file
    - require:
      - pkg: openssh-server

sshd:
  service.running:
    - require:
      - pkg: openssh-clients
      - pkg: openssh-server
      - file: /etc/ssh/sshd_banner
      - file: /etc/ssh/sshd_config
      
{% elif grains['osfinger'] == 'CentOS-5' %}
vim-enhanced:
  pkg:
    - name: vim-enhanced
    - installed

{% elif grains['oscodename'] == 'wheezy' %}
vim:
  pkg:
    - name: vim
    - installed

{% elif grains['oscodename'] == 'squeeze' %}
vim:
  pkg:
    - name: vim
    - installed

{% endif %}

最后然后执行:

salt 'salttest.sudops.com' state.highstate

结果如下:

salttest.sudops.com:
----------
          ID: vim-enhanced
    Function: pkg.installed
      Result: True
     Comment: Package vim-enhanced is already installed.
     Started: 18:59:22.887836
    Duration: 3217.974 ms
     Changes:     
----------
          ID: openssh-server
    Function: pkg.installed
      Result: True
     Comment: Package openssh-server is already installed.
     Started: 18:59:26.714635
    Duration: 0.84 ms
     Changes:   
----------
          ID: openssh-clients
    Function: pkg.installed
      Result: True
     Comment: Package openssh-clients is already installed.
     Started: 18:59:26.715680
    Duration: 0.758 ms
     Changes:   
----------
          ID: sshd_banner
    Function: file.managed
        Name: /etc/ssh/sshd_banner
      Result: True
     Comment: File /etc/ssh/sshd_banner is in the correct state
     Started: 18:59:26.721984
    Duration: 130.937 ms
     Changes:   
----------
          ID: sshd_config
    Function: file.managed
        Name: /etc/ssh/sshd_config
      Result: True
     Comment: File /etc/ssh/sshd_config is in the correct state
     Started: 18:59:26.853549
    Duration: 157.529 ms
     Changes:   
----------
          ID: sshd
    Function: service.running
      Result: True
     Comment: The service sshd is already running
     Started: 18:59:27.012826
    Duration: 332.724 ms
     Changes:   
... 
...

Summary
-------------
Succeeded: 29
Failed:     0
-------------
Total states run:     29

测试的服务器操作系统是CentOS6.6,grains 执行成功,感觉如何?可以使用if for等进行条件判断和循环操作。
接下来可以继续研究下saltstack的pillar。

本文固定链接: https://sudops.com/saltstack-use-grains.html | 运维速度

该日志由 u2 于2015年01月20日发表在 linux, 云计算, 系统, 系统工具 分类下,
原创文章转载请注明: saltstack 使用 grains 变量进行条件匹配 | 运维速度
关键字: , ,

报歉!评论已关闭.