saltstack pillar与ansible 变量

需求,由不同主机,生成不同的配置项目。


1、在ansible中如果要实现配置文件的参数替换,只需要在inventory中配置,并使用{{}}在template中引用变量即可

示例:

[test]
had5 port=111  var1=1
had6 port=222  var1=2

vi templates/test.conf.j2 
port = {{ port }}
var = {{ var1 }}

生成的配置文件内容如下:

[root@hadm vartest]# ansible all -i hosts -m shell -a "cat /tmp/test.conf"
had5 | success | rc=0 >>
port = 111
var = 1

had6 | success | rc=0 >>
port = 222
var = 2


2、在slatstack,要实现上述需求,需要借助pillar与grains。


先看pillar的配置:

[root@hadm base]# tree /srv/pillar/vartest/
/srv/pillar/vartest/
└── init.sls

0 directories, 1 file
[root@hadm base]# cat /srv/pillar/top.sls 
base:
   '*':
      - vartest

[root@hadm base]# cat /srv/pillar/vartest/init.sls 
vartest:
   conn: 100
   {% if grains['id'] == 'had5.com' %}
   port: 111
   var1: 1
   {% elif grains['id'] == 'had6.com' %}
   port: 222
   var1: 2
   {% else %}
   port: 888
   var1: 8
   {% endif %}

我们是通过 grains来指定特定主机的变量参数值。


state配置:

[root@hadm base]# tree /srv/salt/base/vartest/
/srv/salt/base/vartest/
├── init.sls
└── test.conf.j2

0 directories, 2 files
[root@hadm base]# 
[root@hadm base]# 
[root@hadm base]# 
[root@hadm base]# 
[root@hadm base]# cat /srv/salt/base/vartest/init.sls 
vartest:
   file.managed:
      - source: salt://vartest/test.conf.j2
      - name: /tmp/test.conf.salt
      - user: root
      - group: root
      - mode: 644
      - template: jinja
[root@hadm base]# 
[root@hadm base]# 
[root@hadm base]# cat /srv/salt/base/vartest/test.conf.j2 
port = {{ pillar['vartest']['port'] }}
var = {{ pillar['vartest']['var1'] }}

--刷新pillar
[root@hadm base]# salt '*' saltutil.refresh_pillar
had6.com:
    True
had5.com:
    True
hadm.com:
    True
	
--查看pillar是否生效	
[root@hadm base]# salt '*' pillar.data vartest
hadm.com:
    ----------
    vartest:
        ----------
        conn:
            100
        port:
            888
        var1:
            8
had5.com:
    ----------
    vartest:
        ----------
        conn:
            100
        port:
            111
        var1:
            1
had6.com:
    ----------
    vartest:
        ----------
        conn:
            100
        port:
            222
        var1:
            2
[root@hadm base]# salt '*' state.sls vartest
hadm.com:
----------
          ID: vartest
    Function: file.managed
        Name: /tmp/test.conf.salt
      Result: True
     Comment: File /tmp/test.conf.salt updated
     Started: 14:38:25.607505
    Duration: 22.97 ms
     Changes:   
              ----------
              diff:
                  New file
              mode:
                  0644

Summary
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1
had6.com:
----------
          ID: vartest
    Function: file.managed
        Name: /tmp/test.conf.salt
      Result: True
     Comment: File /tmp/test.conf.salt updated
     Started: 14:38:31.355029
    Duration: 36.952 ms
     Changes:   
              ----------
              diff:
                  New file
              mode:
                  0644

Summary
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1
had5.com:
----------
          ID: vartest
    Function: file.managed
        Name: /tmp/test.conf.salt
      Result: True
     Comment: File /tmp/test.conf.salt updated
     Started: 14:38:42.161422
    Duration: 24.327 ms
     Changes:   
              ----------
              diff:
                  New file
              mode:
                  0644

Summary
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1
[root@hadm base]# 
[root@hadm base]# salt '*' cmd.run 'cat /tmp/test.conf.salt'
hadm.com:
    port = 888
    var = 8
had6.com:
    port = 222
    var = 2
had5.com:
    port = 111
    var = 1


salt还是要复杂一些的。