salt使用file,pillar及用jinja渲染模板

初學salt(file,pillar及用jinja渲染模板)

####salt配置rest

salt-master:code

  • ip:192.168.10.91

salt-minion:server

  • id:ops1 ip:192.168.10.91
  • id:192.168.10.92 ip:192.168.10.92
  • id:192.168.10.93 ip:192.168.10.93
  • id:192.168.10.94 ip:192.168.10.94
  • id:192.168.10.95 ip:192.168.10.95

####安裝salt-master及salt-minion並accecpt key.ip

####修改master的配置get

  1. master添加以下配置,使用file和pillar
file_roots:
  base:
    - /srv/salt/base
  dev:
    - /srv/salt/dev
  prod:
    - /srv/salt/prod
pillar_roots:
  base:
    - /srv/pillar/base
  dev:
    - /srv/pillar/dev
  prod:
    - /srv/pillar/prod

2.建立文件夾it

#mkdir -p /srv/salt/{base,dev,prod}
#mkdir -p /srv/pillar/{base,dev,prod}

3.重啓salt-masterio

# service salt-master restart
Stopping salt-master daemon:                               [  OK  ]
Starting salt-master daemon:                               [  OK  ]

使用pillar

1.建立pillar使用的sls 在/srv/pillar/base目錄下建立top.sls及zabbixagent.sls cat top.slsast

base:   #base環境
    'L@ops1,192.168.10.92':     #列表匹配,匹配ops1及192.168.10.92
        - zabbixagent          #引用zabbixagent.sls或者zabbixagent/init.sls

cat zabbixagent.sls模板

zabbix-agent:       
    serverhost: 192.168.10.91     #參數
    serverport: 10050             #參數

2.查看全部pillar,能夠看出,只有ops1和92匹配到了zabbix-agent的pillar #salt '*' pillar.item zabbix-agenttest

192.168.10.94:
    ----------
192.168.10.95:
    ----------
192.168.10.93:
    ----------
192.168.10.92:
    ----------
    zabbix-agent:
        ----------
        serverhost:
            192.168.10.91
        serverport:
            10050
ops1:
    ----------
    zabbix-agent:
        ----------
        serverhost:
            192.168.10.91
        serverport:
            10050

使用file

1.在/src/salt/base中建立文件

echo 123 >/src/salt/base/test.txt

2.使用salt去cp文件至minion # salt -E 'op*' cp.get_file salt://test.txt /tmp/test.txt

ops1:
    /tmp/test.txt

使用jinja模板和pillar生成文件

1.在/srv/salt/base目錄下建立top.sls,testjinja.sls cat top.sls

base:
  '*':
    - zabbix-agent-init
    - testjinja

cat testjinja.sls

testfile:
  file.managed:
    - name: /tmp/test.txt
    - source: salt://test.txt
    - template: jinja
    - defaults:
      zabbixserverport: {{ pillar['zabbix-agent']['serverport'] }}
      zabbixserverhost: {{ pillar['zabbix-agent']['serverhost'] }}

2.使用jinja生成模板 #salt 'ops1' state.sls test.jinja

ops1:
----------
          ID: testfile
    Function: file.managed
        Name: /tmp/test.txt
      Result: True
     Comment: File /tmp/test.txt updated
     Started: 17:55:59.683959
    Duration: 160.869 ms
     Changes:   
              ----------
              diff:
                  ---  
                  +++  
                  @@ -1,0 +1,2 @@
                  +192.168.10.91
                  +10050

Summary
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1

3.查看test.txt文件發現已改變

192.168.10.91
10050
相關文章
相關標籤/搜索