saltstack笔记

一.grains的两种使用方法:参考http://www.21yunwei.com/archives/3155

  1. minion端:脚本:vim /etc/salt/minion.d/*.conf
    [root@99bill minion.d]# pwd
    /etc/salt/minion.d
    [root@99bill minion.d]# ls
    _schedule.conf  test.conf
    [root@99bill minion.d]# cat test.conf
    grains:
        name:
          -lanxiaohui
    

     测试:

    [root@99bill minion.d]# salt 'minion102' grains.item name
    minion102:
        ----------
        name:
            -lanxiaohui
    

      

     

  2. master端:vim /srv/salt/_grains/cpu.py通过python脚本写入grains读取系统信息
    #!/usr/bin/python
    #coding:utf-8
    import os
    
    def cpu():
        grains = {}
        command = "top -b -n 1 | grep Cpu | awk -F'[:,]' '{print $5}'"
        with os.popen(command) as file:
            grains["cpu_id"] = file.read()
        return grains
    

      master端执行

    salt '*' saltutil.sync_all
    salt '*' sys.reload_modules
    查看效果
    salt '*' grains.item cup_id

      静态数据:vim /srv/salt/_grains/test.py

    #!/usr/bin/python
    #coding:utf-8
    
    def test():
        grains = {}
        grains['test_key'] = 'test sign'
        return grains
    

      

      master端执行

salt '*' saltutil.sync_all
salt '*' sys.reload_modules
     查看效果
 salt '*' grains.item test_key

 二.pillar使用方法: vim /etc/salt/master

    

pillar_roots:
  base:
    - /srv/pillar

 vim /srv/pillar/top.sls

base:
    'minion102':
      - data
    'minion104':
      - data

 vim /srv/pillar/data.sls

name: lanxiaohui
age: 18
language:
    - php
    - js
    - python
cpu: {{ grains.cpu_id }}  #调用grains变量

 测试:

[root@99bill pillar]# salt '*' pillar.data language
minion105:
    ----------
minion106:
    ----------
minion102:
    ----------
    language:
        - php
        - js
        - python
minion103:
    ----------
minion104:
    ----------
    language:
        - php
        - js
        - python
[root@99bill pillar]# salt '*' pillar.data cpu
minion103:
    ----------
minion106:
    ----------
minion104:
    ----------
    cpu:
        93.9 id
minion105:
    ----------
minion102:
    ----------
    cpu:
        93.8 id
[root@99bill pillar]# 

 

 

 

        

posted @ 2018-08-30 09:59  Feiger.Lan  阅读(143)  评论(0编辑  收藏  举报