saltstack (3) grains
1、使用自定义grains
grans的一些基本用法:
salt object grains.ls 查看某台设备当前可用grains的key
salt object grains.items 查看某台设备当前可用 grains 的值
salt -G 'os:CentOS' test.ping && salt -G 'os:Amazon' test.ping
salt -G 'cpuarch:x86_64' grains.item num_cpus
salt -G 'mem_total:32232' test.ping
salt 'linux-node1*' grains.get fqdn 查询某个的信息
salt -G os:CentOS cmd.run 'uptime' 使用granis来匹配主机 -G 参数
[root@redis01-jp base]# pwd
/srv/salt/base
[root@redis01-jp base]# ls
apache dns.sls files nginx.sls top.sls
[root@redis01-jp base]# cat dns.sls
local_resolv:
file.managed:
- source: salt://files/resolv.conf
- name: /etc/resolv.conf
- user: root
- group: root
- mode: 644
- template: jinja
- defaults:
DNS_SERVER: 8.8.8.8 # 为变量传值
[root@redis01-jp base]# cat files/resolv.conf
options timeout:2 attempts:5
search ap-northeast-1.compute.internal
nameserver 172.31.0.2
nameserver {{ DNS_SERVER }} # 调用变量
[root@redis01-jp base]# salt 'redis02-jp' state.sls dns #手工调用dns模块(首先加载dns.sls文件,如果没有此文件加载base/dns/init.sls 文件)
redis02-jp:
----------
ID: local_resolv
Function: file.managed
Name: /etc/resolv.conf
Result: True
Comment: File /etc/resolv.conf updated
Started: 06:08:55.301324
Duration: 59.928 ms
Changes:
----------
diff:
---
+++
@@ -1,4 +1,4 @@
options timeout:2 attempts:5
-; generated by /sbin/dhclient-script
search ap-northeast-1.compute.internal
nameserver 172.31.0.2
+nameserver 8.8.8.8
Summary for redis02-jp
------------
Succeeded: 1 (changed=1)
Failed: 0
------------
Total states run: 1
Total run time: 59.928 ms
[root@redis01-jp base]# salt -G 'roles:cacheserver' cmd.run 'w'
redis02-jp:
06:43:19 up 3:24, 1 user, load average: 0.00, 0.00, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
ec2-user pts/0 static-ip-218-20 03:25 1:42m 0.01s 0.00s sshd: ec2-user
[root@redis01-jp base]#
[root@redis01-jp base]# vim /etc/salt/master
[root@redis01-jp base]# sed -n '680,682p' /etc/salt/master
pillar_roots:
base:
- /srv/pillar
[root@redis01-jp base]#
2、使用pillar 调用变量
[root@redis01-jp ~]# vim /etc/salt/master
[root@redis01-jp ~]# sed -n '527,529p' /etc/salt/master
pillar_roots:
base:
- /srv/pillar
[root@redis01-jp ~]# mkdir /srv/pillar
[root@redis01-jp ~]# service salt-master restart
Stopping salt-master daemon: [ OK ]
Starting salt-master daemon: [ OK ]
[root@redis01-jp pillar]# tree /srv/pillar/
/srv/pillar/
|-- pkg
| `-- init.sls
`-- top.sls
1 directory, 2 files
[root@redis01-jp pillar]# cat /srv/pillar/pkg/init.sls
pkgs:
{% if grains['os_family'] == 'RedHat' %}
apache: httpd
vim: vim-enhanced
{% elif grains['os_family'] == 'Debian' %}
apache: apache2
vim: vim
{% elif grains['os'] == 'Arch' %}
apache: apache
vim: vim
{% endif %}
[root@redis01-jp pillar]# cat /srv/pillar/top.sls
base:
'*':
- pkg #调用pkg目录(模块),系统会自动到该目录(模块)下加载init.sls
[root@redis01-jp pillar]# salt '*' pillar.items #查看各minion获得到的 pillar变量值
redis02-jp:
----------
pkgs:
----------
apache:
httpd
vim:
vim-enhanced
[root@redis01-jp base]# pwd
/srv/salt/base
[root@redis01-jp base]# tree apache/
apache/
`-- init.sls
0 directories, 1 file
[root@redis01-jp base]# cat apache/init.sls
apache:
pkg.installed:
- name: {{ pillar['pkgs']['apache'] }} #调用pillar变量
[root@redis01-jp base]# cat top.sls
base:
'*':
- apache #调用apache模块,自动加载apache/init.sls
[root@redis01-jp ~]# salt 'redis02-jp' state.highstate
[root@redis01-jp ~]# salt '*' saltutil.refresh_pillar 刷新pillar,grains是需要重启minion端,pillar是需要刷新
redis02-jp:
True
[root@redis01-jp ~]# salt -I 'apache:httpd' test.ping
redis02-jp:
True
[root@redis01-jp ~]#
3、Grains与pillar 的区别总结:
Grains 静态 minion启动时收集 目标选择、配置管理、数据查询 minion
pillar 动态 master定义 目标选择、配置管理、敏感数据 maste
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341