标签:saltstack
1、简介
grains负责采集客户端一些基本信息,这个也完全可以自定义
pillar跟grains比较的话,他的灵活性强点,想这么定义就这么定义,然后取值就行
2、一个例子(系统是oracleLinux6的就线上磁盘使用情况)
salt -G ‘osfinger:Oracle Linux Server-6‘ cmd.run ‘df -h‘
3、修改客户端配置文件,自定义grains的键值
[root@localhost minion.d]# pwd /etc/salt/minion.d [root@localhost minion.d]# egrep -v "^$|^#" /etc/salt/minion default_include: minion.d/*.conf master: 192.168.24.66 id: 192.168.24.67
在/etc/salt/minion.d路径下新建一个配置文件grains.conf
[root@localhost minion.d]# cat grains.conf grains: myid: 192.168.24.67 class: nginx
[root@localhost salt]# salt ‘*‘ saltutil.sync_grains //在master上刷新grains配置 192.168.24.67: 192.168.24.68: [root@localhost salt]# salt -G ‘class:nginx‘ test.ping 192.168.24.67: True
4、启用pillar
[root@localhost srv]# egrep -v ‘^$|^#‘ /etc/salt/master default_include: master.d/*.conf interface: 192.168.24.66 auto_accept: True file_roots: base: - /srv/salt pillar_roots: base: - /srv/pillar //指定工作目录
首先创建一个状态文件
[root@localhost pillar]# pwd /srv/pillar [root@localhost pillar]# cat top.sls base: ‘*‘: - packages //列举一个web的安装例子
[root@localhost pillar]# cat packages.sls //使用jinja模板 {% if grains[‘os‘] == ‘OEL‘ %} //此处使用grains查询数据,付给pillar apache: httpd git: git {% elif grains[‘os‘] == ‘Centos‘ %} apache: httpd git: git {% elif grains[‘os‘] == ‘Debian‘ %} apache: apache2 git: git-core {% endif %}
取值方法如下
[root@localhost pillar]# salt ‘*‘ grains.item os 192.168.24.67: ---------- os: OEL 192.168.24.68: ---------- os: OEL
5、接下来其他地方(例如state文件)可以引用pillar的动态数据
[root@localhost salt]# pwd /srv/salt [root@localhost salt]# cat web.sls apache_install: pkg.installed: - name: {{ pillar[‘apache‘] }} //此处可理解为一个字典、pillar中一个键叫apache
6、运行安装
[root@localhost salt]# salt ‘192.168.24.67‘ state.sls web 192.168.24.67: ---------- ID: apache_install Function: pkg.installed Name: httpd Result: True Comment: The following packages were installed/updated: httpd Started: 17:51:16.750592 Duration: 39468.832 ms Changes: ----------
本文出自 “不在灯火阑珊处” 博客,请务必保留此出处http://tianruyun.blog.51cto.com/4549660/1796053
标签:saltstack
原文地址:http://tianruyun.blog.51cto.com/4549660/1796053