salt-grains-pillar

Grains数据系统

Grains(鼓励):静态数据,存储客户端的信息。

1
salt '*' grains.items ###列出服务器的详细参数

只查看OS的信息:

1
2
3
4
5
6
7
8
9
[root@centos6 ~]# salt '*' grains.item os
minion.saltstack.com:
----------
os:
CentOS
[root@centos6 ~]# salt '*' grains.get os
minion.saltstack.com:
CentOS

1
2
3
4
5
6
7
8
[root@linux-node1 salt]# salt '*' grains.get ip_interfaces:eth0
linux-node2:
- 10.0.0.8
- fe80::20c:29ff:fea4:91c7
linux-node1:
- 10.0.0.7
- fe80::20c:29ff:fe6a:d896
[root@linux-node1 salt]#

列出所有信息的名称:

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@centos6 ~]# salt '*' grains.ls
minion.saltstack.com:
- SSDs
- biosreleasedate
- biosversion
- cpu_flags
- cpu_model
- cpuarch
- domain
- fqdn
- fqdn_ip4
- fqdn_ip6
省略部分内容。。。。。。。。

在服务端测试:-G 表示匹配grains :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@centos6 ~]# salt -G 'env:prod' test.ping
minion.saltstack.com:
True

[root@linux-node1 salt]# salt -G 'os:CentOS' cmd.run 'w'
linux-node2:
03:35:48 up 9:50, 2 users, load average: 0.00, 0.00, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
niu pts/0 10.0.2.1 02:02 1:10m 0.00s 0.00s -bash
niu pts/1 10.0.2.1 02:14 1:21m 0.00s 0.00s -bash
linux-node1:
03:35:46 up 19:30, 2 users, load average: 0.00, 0.00, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
niu pts/0 10.0.2.1 02:02 1.00s 0.41s 0.00s sshd: niu [priv
niu pts/1 10.0.2.1 02:14 1:07m 0.02s 0.00s sshd: niu [priv
[root@linux-node1 salt]#

在客户端自定义grains:

1
2
3
4
5
6
7
[root@centos6 ~]# vim /etc/salt/minion
grains:
roles: nginx
env: prod

"/etc/salt/minion" 627L, 25173C written

使用命令行设置minion端的grains。

1
2
3
4
5
6
7
8
## 给logstash4服务器设置一个elastic_url的grains值
salt 'chuye.logstash4.service' grains.setval elastic_url 'http://10.10.196.24:9200'
## 查看elastic_url的值
salt 'chuye.logstash4.service' grains.item elastic_url
## 重新给elastic_url赋值
salt 'chuye.logstash5.service' grains.setval elastic_url '"http://10.10.160.129:9200"'
## 查看新赋值是否生效
salt 'chuye.logstash5.service' grains.item elastic_url

设置成功后,minion会在 /etc/minion/grains 增加一行配置文件

1
2
vim /etc/salt/grains
elastic_url: http://10.10.196.24:9200

也可以在客户端这样定义:
客户端编写: grains配置文件中,不能和minion的参数冲突。

1
2
3
4
5
6
[root@centos6 ~]# vim /etc/salt/grains
cloud: openstack
[root@centos6 ~]# /etc/init.d/salt-minion restart
Stopping salt-minion daemon: [ OK ]
Starting salt-minion daemon: [ OK ]
[root@centos6 ~]#

在top.sls中调用grains的方法:

1
2
3
4
5
6
[root@linux-node1 salt]# vim top.sls
base:
'web:nginx':
- match: grain
- apache

在服务器端测试:
[root@centos6 ~]# salt -G ‘cloud:openstack’ test.ping
minion.saltstack.com:
True

在服务端输入如下命令可以刷新:(客户端更改后不用重启)

1
2
[root@centos6 ~]# salt '*' saltutil.sync_grains

在配置文件中调用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
nginx:
pkg:
- installed
file.managed:
- source: salt://nginx/nginx.conf
- name: /etc/nginx/nginx.conf
- user: root
- group: root
- mode: 644
- template: jinja

service.running:
- enable: True
- reload: True
- watch:
- file: /etc/nginx/nginx.conf
- pkg: nginx

Pillar(柱子)数据系统:
启动pillar功能

1
2
3
vim /etc/salt/master
pillar_opts: True
##piller 默认参数的开启。

作用:

  • 处理敏感数据。
  • 处理差异性的文件。

第二步:在master文件中配置pillar文件存放的位置

1
2
3
4
vim /etc/salt/master
pillar_roots:
base:
- /etc/salt/pillar

第三步:开始编写pillar文件

1
2
3
4
5
6
7
8
# mkdir /etc/salt/pillar
# cd /etc/salt/pillar/
# vim top.sls
base:
'*':
- init.rsyslog
~
"top.sls" [New] 3L, 33C written
1
2
3
4
5
6
7
8
# mkdir init
# cd init/
# vim rsyslog.sls
{% if grains['osfinger'] == 'CentOS-6' %}
syslog: rsyslog
{% elif grains['osfinger'] == 'CentOS-5' %}
syslog: syslog
{% endif %}

刷新一下配置:

1
2
3
4
[root@centos6 init]# salt '*' saltutil.refresh_pillar
minion.saltstack.com:
True
[root@linux-node1 pillar]# salt '*' pillar.items

第四步使用pillar
pillar跟grains的使用方法一样
可以用索引(pillar[‘pkgs’][‘apache’])或get方法(pillar.get(‘users’, {}))。

索引的方式:

1
2
3
4
5
6
/srv/salt/apache/init.sls:

apache:
pkg.installed:
- name: {{ pillar['pkgs']['apache'] }}

get的方式

1
2
3
4
5
还可以在state file中设置默认值: srv/salt/apache/init.sls:

apache:
pkg.installed:
- name: {{ salt['pillar.get']('pkgs:apache', 'httpd') }}

实例二:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{% if grains['os'] == 'CentOS' %}
apache:httpd
{% elif grains['os'] == 'Debian' %}
apache:apche2
{% endif %}

[root@linux-node1 pillar]# cat /srv/pillar/top.sls
base:
'*':
- apache

[root@linux-node1 pillar]# ls
apache.sls top.sls
[root@linux-node1 pillar]# pwd
/srv/pillar
[root@linux-node1 pillar]#

grains和pillar对比:

使用salt进行nginx配置文件管理。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# For more information on configuration, see:
user nginx;
worker_processes {{ grains['num_cpus'] }};
{% if grains['num_cpus'] == 2 %}
worker_cpu_affinity 01 10;
{% elif grains['num_cpus'] == 4 %}
worker_cpu_affinity 1000 0100 0010 0001;
{% elif grains['num_cpus'] >= 8 %}
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
{% else %}
worker_cpu_affinity 1000 0100 0010 0001;
{% endif %}
worker_rlimit_nofile {{ grains['max_open_file'] }};

error_log /var/log/nginx/error.log;
#error_log /var/log/nginx/error.log notice;
#error_log /var/log/nginx/error.log info;

pid /var/run/nginx.pid;

events {
worker_connections {{ grains['max_open_file'] }};
}


http {
include /etc/nginx/mime.types;
default_type application/octet-stream;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log /var/log/nginx/access.log main;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

#gzip on;

# Load config files from the /etc/nginx/conf.d directory
# The default server is in conf.d/default.conf
#include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
server_name _;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {
root {{ pillar['nginx']['root'] }};
index index.html index.htm;
}

error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
}

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}

}

}