一.Grains组件
grains是Saltstack最重要的组件之一,grains的作用是收集被控主机的基本信息,这些信息通常都是一些静态类的数据,包括CPU、内核、
操作系统、虚拟化等,在服务器端可以根据这些信息进行灵活定制,管理员可以利用这些信息对不同业务进行个性化定制。
1、用 grains 不同的方法来更改、推送、刷新
1).在minion配置文件里找到grains节点进行添加或编辑
[root@server7 ~]# vim /etc/salt/minion
[root@server7 salt]# /etc/init.d/salt-minion restart
master中查看
[root@server6 salt]# salt '*' grains.item roles
server6:
----------
roles:
server8:
----------
roles:
server7:
----------
roles:
- apache
更改配置文件
[root@server8 ~]# vim /etc/salt/grains
roles:
nginx
master中查看:
[root@server6 salt]# salt server8 saltutil.sync_grains //推送刷新
server8:
[root@server6 salt]# salt '*' grains.item roles
server8:
----------
roles:
nginx
server7:
----------
roles:
- apache
server6:
----------
roles:
2.在master的base目录下建python文件来从minion上取得环境参数并且grains可以和一键推送结合,可以配合grains的指定值来推送
[root@server6 salt]# vim top.sls
base:
'server6':
- haproxy.service
'roles:apache': //用角色名代替主机名
- match: grain
- httpd.service
'roles:nginx':
- match: grain
- nginx.service
推送
[root@server6 salt]# salt '*' state.highstate
Summary for server7
------------
Succeeded: 4
Failed: 0
------------
Total states run: 4
Total run time: 439.369 ms
Summary for server6
-------------
Succeeded: 10
Failed: 0
-------------
Total states run: 10
Total run time: 750.266 ms
Summary for server8
------------
Succeeded: 8
Failed: 0
------------
Total states run: 8
Total run time: 731.395 ms
3.在/srv/salt建立编写grains文件,使用自定义python脚本获取grains信息,默认自定义脚本需要存放在master的/srv/salt/_grains目录下
[root@server6 salt]# pwd
/srv/salt
[root@server6 salt]# mkdir _grains
[root@server6 salt]# cd _grains/
[root@server6 _grains]# vim my_grains.py
#!/usr/bin/env python
def my_grains():
grains = {}
grains['hello'] = 'world'
grains['salt'] = 'stack'
return grains
//使用sync_grains命令同步脚本到minion主机上去,并通过grains.item命令获取信息
[root@server6 _grains]# salt server8 saltutil.sync_grains
server8:
- grains.my_grains
[root@server8 ~]# tree /var/cache/salt/minion/
/var/cache/salt/minion/
├── accumulator
├── extmods
│ └── grains
│ ├── my_grains.py
│ └── my_grains.pyc
├── files
│ └── base
│ ├── _grains
│ │ └── my_grains.py //已经接受
│ ├── nginx
│ │ ├── files
│ │ │ ├── nginx
│ │ │ ├── nginx-1.14.0.tar.gz
│ │ │ └── nginx.conf
│ │ ├── install.sls
│ │ └── service.sls
│ ├── top.sls
│ └── users
│ └── user.sls
├── highstate.cache.p
├── module_refresh
├── pkg_refresh
├── proc
└── sls.p
[root@server6 _grains]# salt '*' grains.item salt
server8:
----------
salt:
stack
server7:
----------
salt:
server6:
----------
salt:
[root@server6 _grains]# salt '*' grains.item hello
server7:
----------
hello:
server8:
----------
hello:
world
server6:
----------
hello:
grains 配置优先级顺序为/etc/salt/minion.d/grains.conf (或/etc/salt/minion) > master端自定义grains脚本(minion端/var/cache/salt/minion/extmods/grains目录下) > Grains模块定义Grains (/etc/salt/grains)
二. Pillar组件
pillar数据是存储在master端的,而在客户端有缓存,通常pillar数据是一些配置信息,pillar一般用来存储动态的、敏感的数据;通过
minion端和master端都可以配置设置或获取grains信息,而pillar信息只能在master端配置,再到 minion端执行。pillar默认使用
sls文件进行数据存储(YAML格式),除此之外,pillar也同时支持多种后端的数据存储方式。例 如:mysql, mongodb, ldap,json,
cobbler甚至是puppet。
首先需要在master配置文件中修改pillar根目录
[root@server6 salt]# vim /etc/salt/master
[root@server6 salt]# mkdir /srv/pillar
[root@server6 salt]# cd /srv/pillar/
[root@server6 pillar]# cd web/
[root@server6 web]# vim install.sls
{% if grains['fqdn'] == 'server7' %}
webserver: httpd
{% elif grains['fqdn'] == 'server8' %}
webserver: nginx
{% elif grains['fqdn'] == 'server6' %}
webserver: haproxy
{% endif %}
[root@server6 web]# cd ..
[root@server6 pillar]# vim top.sls
base:
'*':
- web.install
[root@server6 pillar]# /etc/init.d/salt-master restart
可以通过命令salt … refresh_pillar来刷新minion的pillar数据
[root@server6 pillar]# salt '*' saltutil.refresh_pillar
server7:
True
server8:
True
server6:
True
[root@server6 pillar]# salt '*' pillar.items
server6:
----------
webserver:
haproxy
server7:
----------
webserver:
httpd
server8:
----------
webserver:
nginx
也可单个查看
[root@server6 pillar]# salt -G 'roles:nginx' cmd.run df
server8:
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/VolGroup-lv_root 19134332 1096580 17065772 7% /
tmpfs 510200 16 510184 1% /dev/shm
/dev/vda1 495844 33475 436769 8% /boot
[root@server6 pillar]# salt -S '172.25.0.123' cmd.run hostname
server7:
server7
[root@server6 httpd]# salt -I 'webserver:httpd' test.ping
server7:
True
三. JINJA模块
第一种:
[root@server6 httpd]# vim service.sls
include:
- httpd.install
apache-service:
service.running:
- name: httpd
- enable: True
- reload: True
- watch:
- /etc/httpd/conf/httpd.conf
/etc/httpd/conf/httpd.conf:
file.managed:
- source: salt://httpd/files/httpd.conf
- mode: 644
- user: root
- template: jinja
- context:
port: 8080
bind: 172.25.0.123
[root@server6 httpd]# vim files/httpd.conf
Listen {{ port }}
推送:
[root@server6 httpd]# salt server7 state.sls httpd.service
diff:
---
+++
@@ -133,7 +133,7 @@
# prevent Apache from glomming onto all bound IP addresses (0.0.0.0)
#
#Listen 12.34.56.78:80
-Listen 80
+Listen 8080 //更改为8080端口
#
# Dynamic Shared Object (DSO) Support
----------
Summary for server7
------------
Succeeded: 4 (changed=2)
Failed: 0
------------
Total states run: 4
Total run time: 493.257 ms
第二种:
[root@server6 httpd]# vim files/httpd.conf
Listen {{ bind }}:{{ port }}
[root@server6 httpd]# salt server7 state.sls httpd.service
Changes:
----------
diff:
---
+++
@@ -133,7 +133,7 @@
# prevent Apache from glomming onto all bound IP addresses (0.0.0.0)
#
#Listen 12.34.56.78:80
-Listen 8080
+Listen 172.25.0.123:8080
#
# Dynamic Shared Object (DSO) Support
在server7中查看:
[root@server7 ~]# vim /etc/httpd/conf/httpd.conf
第三种:
[root@server6 httpd]# vim files/httpd.conf
{% from 'httpd/lib.sls' import port with context %} //第一行写入
[root@server6 httpd]# vim lib.sls
{% set port = 80 %}
推送
[root@server6 httpd]# salt server7 state.sls httpd.service
Changes:
----------
diff:
---
+++
@@ -1,3 +1,4 @@
+
#
# This is the main Apache server configuration file. It contains the
# configuration directives that give the server its instructions.
@@ -133,7 +134,7 @@
# prevent Apache from glomming onto all bound IP addresses (0.0.0.0)
#
#Listen 12.34.56.78:80
-Listen 172.25.0.123:8080
+Listen 172.25.0.123:80
server7查看文件
第四种
[root@server6 httpd]# cd /srv/pillar/
[root@server6 pillar]# cd web/
[root@server6 web]# vim install.sls
{% if grains['fqdn'] == 'server7' %}
webserver: httpd
bind: 172.25.0.123
port: 8080
{% elif grains['fqdn'] == 'server8' %}
webserver: nginx
{% elif grains['fqdn'] == 'server6' %}
webserver: haproxy
{% endif %}
[root@server6 web]# vim /srv/salt/httpd/files/httpd.conf
Listen {{ pillar[ 'bind' ] }}:{{ pillar['port'] }}
推送
[root@server6 web]# salt server7 state.sls httpd.service
Changes:
----------
diff:
---
+++
@@ -134,7 +134,7 @@
# prevent Apache from glomming onto all bound IP addresses (0.0.0.0)
#
#Listen 12.34.56.78:80
-Listen 172.25.0.123:80
+Listen 172.25.0.123:8080
#
# Dynamic Shared Object (DSO) Support
----------