• 欢迎访问蜷缩的蜗牛博客 蜷缩的蜗牛
  • 微信搜索: 蜷缩的蜗牛 | 联系站长 kbsonlong@qq.com
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

saltstack—–state.sls引用grains

Saltstack 蜷缩的蜗牛 2年前 (2017-02-18) 96次浏览 已收录 0个评论

在生产环境中普遍会有很多主机,当需要批量修改配置文件的时候,某些配置文件可能跟主机信息相关,需要作出对应的调整,那此时可以利用 grains 获取不同主机来做相应的修改同步,批量的修改配置文件。

例如在生产环境中配置 nagios 插件 nrpe 配置文件是,server_address 是客户端主机 IP 地址,当需要批量修改客户端配置的时候,此时客户端的配置文件中 server_address 肯定不是固定的,需要配置客户端自身地址。

1、创建 nrped.sls

# cat nagios/nrped.sls   
nrpe.cfg:  
  file.managed:  
    - name: /usr/local/nagios/etc/nrpe.cfg  
    - source: salt://nagios/files/nrpe.cfg.jinja  
    - user: support  
    - group: support  
    - mode: 644  
    - template: jinja

2、创建 nrpe.cfg.jinja 配置文件模板

# cat nagios/files/nrpe.cfg.jinja   
log_facility=daemon  
pid_file=/var/run/nrpe.pid  
server_port=5666  
server_address={{ grains['fqdn_ip4'][0] }}     ##利用 grains 来获取客户端自身地址  
nrpe_user=nagios  
nrpe_group=nagios  
allowed_hosts=xxx.xxx.xxx.xxx,xxx.xxx.xxx.xxx   ###nagios 主机地址,多个主机用,逗号隔开  
dont_blame_nrpe=0  
allow_bash_command_substitution=0  
debug=0  
command_timeout=60  
connection_timeout=300  
  
  
####### command #######  
command[check_proc]=/usr/local/nagios/libexec/proc.sh  
command[check_local_swap]=/usr/local/nagios/libexec/check_swap -w 40% -c 30%  
command[check_local_disk]=/usr/local/nagios/libexec/check_disk -w 20% -c 10%

注意:nrped.sls 配置中的 template: jinja 这个非常关键,如果没有配置这个选项,nrpe.cfg.jinja 配置文件无法引用到 grains 数据,同步到客户端会直接同步为 server_address={{ grains[‘fqdn_ip4’][0] }};如下

# cat nrped.sls  
nrpe.cfg:  
  file.managed:  
    - name: /usr/local/nagios/etc/nrpe.cfg  
    - source: salt://nagios/files/nrpe.cfg.jinja  
    - user: support  
    - group: support  
    - mode: 644
# salt \*-vm234-158 state.sls nagios.nrped test=True  
*-vm234-158:  
----------  
          ID: nrpe.cfg  
    Function: file.managed  
        Name: /usr/local/nagios/etc/nrpe.cfg  
      Result: None  
     Comment: The file /usr/local/nagios/etc/nrpe.cfg is set to be changed  
     Started: 22:34:31.157748  
    Duration: 34.201 ms  
     Changes:     
              ----------  
              diff:  
                  ---    
                  +++    
                  @@ -1,7 +1,7 @@  
                   log_facility=daemon  
                   pid_file=/var/run/nrpe.pid  
                   server_port=5666  
                  -server_address=192.168.xxx.xxx  
                  +server_address={{ grains['fqdn_ip4'][0] }}  
                   nrpe_user=nagios  
                   nrpe_group=nagios  
                   allowed_hosts=xxx.xxx.xxx.xxx,xxx.xxx.xxx.xxx     
  
  
Summary for *-vm234-158  
------------  
Succeeded: 1 (unchanged=1, changed=1)  
Failed:    0  
------------  
Total states run:     1  
Total run time:  34.201 ms
# salt \*-vm234-158 state.sls nagios.nrped test=True  
*-vm234-158:  
----------  
          ID: nrpe.cfg  
    Function: file.managed  
        Name: /usr/local/nagios/etc/nrpe.cfg  
      Result: True  
     Comment: The file /usr/local/nagios/etc/nrpe.cfg is in the correct state  
     Started: 22:40:31.532780  
    Duration: 26.22 ms  
     Changes:     
  
  
Summary for *-vm234-158  
------------  
Succeeded: 1  
Failed:    0  
------------  
Total states run:     1  
Total run time:  26.220 ms
# cat /usr/local/nagios/etc/nrpe.cfg   
log_facility=daemon  
pid_file=/var/run/nrpe.pid  
server_port=5666  
server_address=192.168.xxx.xxx  
nrpe_user=nagios  
nrpe_group=nagios  
allowed_hosts=xxx.xxx.xxx.xxx,xxx.xxx.xxx.xxx  
dont_blame_nrpe=0  
allow_bash_command_substitution=0  
debug=0  
command_timeout=60  
connection_timeout=300  
  
  
####### command #######  
command[check_proc]=/usr/local/nagios/libexec/proc.sh  
command[check_local_swap]=/usr/local/nagios/libexec/check_swap -w 40% -c 30%  
command[check_local_disk]=/usr/local/nagios/libexec/check_disk -w 20% -c 10%

蜷缩的蜗牛 , 版权所有丨如未注明 , 均为原创丨 转载请注明saltstack—–state.sls 引用 grains
喜欢 (0)
[]
分享 (0)

您必须 登录 才能发表评论!