saltstack配置文件的模块简单写法

配置文件的模块写法

[root@master init]# cat zabbix_agent.sls 
zabbix_agent:
  pkg.installed:
    - names:
      - zabbix22-agent
  file.managed:
    - name: /etc/zabbix_agentd.conf
    - source: salt://init/files/zabbix_agentd.conf
    - user: root
    - group: root
    - mode: 644
  service.running:
    - name: zabbix-agentd
    - enable: True
    - reload: True
    - watch:
      - file: zabbix_agent

如上!

比如我们修改的是zabbix_agentd.conf中的Server=10.0.0.22
则,只需要将zabbix_agentd.conf的server端定义变量即可

Server={{Zabbix_Server}}

如下:

  • template: zabbixjinjia
  • defaults:
    Zabbix_Server: {{ pillar['zabbix-agent']['Zabbix_Server'] }}

如下:

[root@master init]# pwd
/etc/salt/status/init
[root@master init]# 
[root@master init]# vim zabbix_agent.sls 
zabbix_agent:
  pkg.installed:
    - names:
      - zabbix22-agent
  file.managed:
    - name: /etc/zabbix_agentd.conf
    - source: salt://init/files/zabbix_agentd.conf
    - user: root
    - group: root
    - mode: 644
    - template: jinja
    - defaults:
      Zabbix_Server: {{ pillar['zabbix-agent']['Zabbix_Server'] }}
  service.running:
    - name: zabbix-agentd
    - enable: True
    - reload: True
    - watch:
      - file: zabbix_agent

而后创建在pillar下zabbix_agent.sls

[root@master init]# pwd
/etc/salt/pillar/init
[root@master init]# 
[root@master init]# cat zabbix_agent.sls 
zabbix-agent:
  Zabbix_Server: 10.0.0.222 

注意:
Zabbix_Server: 10.0.0.222 ------>对应status/init下的zabbix_agent中的Zabbix_Server: {{ pillar['zabbix-agent']['Zabbix_Server'] }}的'Zabbix_Server'

而后在pillar下的top.sls中添加- init.zabbix_agent

[root@master pillar]# vim top.sls 
[root@master pillar]# pwd
/etc/salt/pillar
[root@master pillar]# cat top.sls 
base:
  '*':
    - init.rsyslog
    - init.zabbix_agent
[root@master pillar]# 

执行结果如下:

[root@master init]# salt '*' state.highstate
minion.23.com:
----------
          ID: pkg.init
    Function: pkg.installed
        Name: mtr
      Result: True
     Comment: Package mtr is already installed.
     Started: 08:19:01.343078
    Duration: 677.19 ms
     Changes:   
----------
          ID: pkg.init
    Function: pkg.installed
        Name: nginx
      Result: True
     Comment: Package nginx is already installed.
     Started: 08:19:02.020432
    Duration: 0.443 ms
     Changes:   
----------
          ID: pkg.init
    Function: pkg.installed
        Name: lrzsz
      Result: True
     Comment: Package lrzsz is already installed.
     Started: 08:19:02.020936
    Duration: 0.248 ms
     Changes:   
----------
          ID: zabbix_agent
    Function: pkg.installed
        Name: zabbix22-agent
      Result: True
     Comment: Package zabbix22-agent is already installed.
     Started: 08:19:02.021242
    Duration: 0.248 ms
     Changes:   
----------
          ID: zabbix_agent
    Function: file.managed
        Name: /etc/zabbix_agentd.conf
      Result: True
     Comment: File /etc/zabbix_agentd.conf updated
     Started: 08:19:02.023524
    Duration: 12.842 ms
     Changes:   
              ----------
              diff:
                  ---  
                  +++  
                  @@ -84,7 +84,7 @@
                   # Server=
                   
                   #Server=127.0.0.1
                  -Server=10.0.0.22
                  +Server=10.0.0.222
                   
                   ### Option: ListenPort
                   #    Agent will listen on this port for connections from the server.
----------
          ID: zabbix_agent
    Function: service.running
        Name: zabbix-agentd
      Result: True
     Comment: Service reloaded
     Started: 08:19:02.065563
    Duration: 178.509 ms
     Changes:   
              ----------
              zabbix-agentd:
                  True

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

第二种修改方式:

 - template: jinja
    - defaults:
      Zabbix_Server: {{ pillar['Zabbix_Server'] }}

如下:

[root@master init]# pwd
/etc/salt/status/init
[root@master init]# 
[root@master init]# vim zabbix_agent.sls 
zabbix_agent:
  pkg.installed:
    - names:
      - zabbix22-agent
  file.managed:
    - name: /etc/zabbix_agentd.conf
    - source: salt://init/files/zabbix_agentd.conf
    - user: root
    - group: root
    - mode: 644
    - template: jinja
    - defaults:
      Zabbix_Server: {{ pillar['Zabbix_Server'] }}
  service.running:
    - name: zabbix-agentd
    - enable: True
    - reload: True
    - watch:
      - file: zabbix_agent

[root@master init]# pwd
/etc/salt/status/init
[root@master init]# 

注释掉#zabbix-agent:,修改下ip以示区别

[root@master init]# vim ../../pillar/init/zabbix_agent.sls
#zabbix-agent:
  Zabbix_Server: 10.0.0.111------------------>key是和zabbix_agentd中的一样

跑一边查看!已经OK

[root@master init]# salt '*' state.highstate
minion.23.com:
----------
          ID: pkg.init
    Function: pkg.installed
        Name: mtr
      Result: True
     Comment: Package mtr is already installed.
     Started: 08:23:42.171235
    Duration: 686.816 ms
     Changes:   
----------
          ID: pkg.init
    Function: pkg.installed
        Name: nginx
      Result: True
     Comment: Package nginx is already installed.
     Started: 08:23:42.858330
    Duration: 0.674 ms
     Changes:   
----------
          ID: pkg.init
    Function: pkg.installed
        Name: lrzsz
      Result: True
     Comment: Package lrzsz is already installed.
     Started: 08:23:42.859121
    Duration: 0.274 ms
     Changes:   
----------
          ID: zabbix_agent
    Function: pkg.installed
        Name: zabbix22-agent
      Result: True
     Comment: Package zabbix22-agent is already installed.
     Started: 08:23:42.859455
    Duration: 0.267 ms
     Changes:   
----------
          ID: zabbix_agent
    Function: file.managed
        Name: /etc/zabbix_agentd.conf
      Result: True
     Comment: File /etc/zabbix_agentd.conf updated
     Started: 08:23:42.862150
    Duration: 9.588 ms
     Changes:   
              ----------
              diff:
                  ---  
                  +++  
                  @@ -84,7 +84,7 @@
                   # Server=
                   
                   #Server=127.0.0.1
                  -Server=10.0.0.222
                  +Server=10.0.0.111
                   
                   ### Option: ListenPort
                   #    Agent will listen on this port for connections from the server.
----------
          ID: zabbix_agent
    Function: service.running
        Name: zabbix-agentd
      Result: True
     Comment: Service reloaded
     Started: 08:23:42.898476
    Duration: 170.457 ms
     Changes:   
              ----------
              zabbix-agentd:
                  True

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

第三种方法!Zabbix_Server:直接填写IP即可!

zabbix_agent:
  pkg.installed:
    - names:
      - zabbix22-agent
  file.managed:
    - name: /etc/zabbix_agentd.conf
    - source: salt://init/files/zabbix_agentd.conf
    - user: root
    - group: root
    - mode: 644
    - template: jinja
    - defaults:
      Zabbix_Server: 10.0.0.33
  service.running:
    - name: zabbix-agentd
    - enable: True
    - reload: True
    - watch:
      - file: zabbix_agent

目录结构如下:

[root@master salt]# tree
.
├── master
├── minion
├── minion.d
│   └── _schedule.conf
├── pillar
│   ├── init
│   │   ├── rsyslog.sls
│   │   └── zabbix_agent.sls
│   └── top.sls
├── pki
│   ├── master
│   │   ├── master.pem
│   │   ├── master.pub
│   │   ├── minions
│   │   │   ├── master.22.com
│   │   │   ├── minion.21.com
│   │   │   └── minion.23.com
│   │   ├── minions_autosign
│   │   ├── minions_denied
│   │   ├── minions_pre
│   │   └── minions_rejected
│   └── minion
│       ├── minion_master.pub
│       ├── minion.pem
│       └── minion.pub
├── services
│   └── dev
└── status
    ├── init
    │   ├── files
    │   │   └── zabbix_agentd.conf
    │   ├── pkg.sls
    │   └── zabbix_agent.sls
    └── top.sls

0 分享

您可以选择一种方式赞助本站

支付宝扫码赞助

支付宝扫码赞助

日期: 2015-12-15分类: saltstack

标签: saltstack

发表评论