第1章 SaltStack数据系统Grains
1.1 Grains介绍
Minion启动时收集的静态数据(只有minion启动时收集到的数据,启动后变化的数据无法更新)存放在Grains中。
1.2 Grains应用场景
- Grains可以在state系统中使用,用于配置管理模块
- Grains可以target中使用,在用来匹配Minion,比如匹配操作系统,使用-G选项
- Grains可以用于数据采集,Grains保存着收集到的客户端的详细信息
1.3 查看Grains收集到的信息
[root@linux-node01 ~]# salt 'linux-node02' grains.ls linux-node02: - SSDs - biosreleasedate …… [root@linux-node01 ~]# salt 'linux-node02' grains.items linux-node02: ---------- SSDs: biosreleasedate: 07/29/2019 biosversion: 6.00 cpu_flags: - fpu - vme ……
1.4 使用模板文件编写sls文件
1.4.1 使用Jinja模板步骤
- 修改模板文件里面变量使用{{ 名称 }},如{{ PORT }}
- 修改sls文件中的file状态使用template参数:- template: jinja
- 在template参数下添加变量列表,如 – defaults: PORT: 8080
1.4.2 模板文件里面变量书写方式
1.4.2.1 使用Grains方式:
{{ grains['fqdn_ip4'][0] }}
1.4.2.2 使用执行模块方式
{{ salt[‘network.hw_addr’](‘eth0’) }}
1.4.2.3 使用Pillar方式
{{ pillar[‘apache’][‘PORT’] }}
1.5 Grains案例:Apache监听本地IP地址
1.5.1 使用Grains获取minion本地IP地址
[root@linux-node01 ~]# salt '*' grains.item fqdn_ip4 linux-node02: ---------- fqdn_ip4: - 10.10.10.102 linux-node01: ---------- fqdn_ip4: - 10.10.10.101
1.5.2 使用Jinja模板编写sls文件
[root@linux-node01 ~]# vim /srv/salt/base/web/apache.sls apache-install: pkg.installed: - name: httpd apache-config: file.managed: - name: /etc/httpd/conf/httpd.conf - source: salt://web/files/httpd.conf - user: root - group: root - mode: 644 - template: jinja - defaults: PORT: 8080 IPADDR: {{ grains['fqdn_ip4'][0] }} - require: - pkg: apache-install apache-service: service.running: - name: httpd - enable: True - reload: True - watch: - file: apache-config
1.5.3 修改模板配置文件
[root@linux-node01 ~]# vim /srv/salt/base/web/files/httpd.conf 42 Listen {{ IPADDR }}:{{ PORT }}
1.5.4 运行批量管理
[root@linux-node01 ~]# salt '*' state.highstate linux-node01: ---------- ID: apache-install Function: pkg.installed Name: httpd Result: True Comment: All specified packages are already installed Started: 15:51:07.829947 Duration: 1498.821 ms Changes: ---------- ID: apache-config Function: file.managed Name: /etc/httpd/conf/httpd.conf Result: True Comment: File /etc/httpd/conf/httpd.conf updated Started: 15:51:09.333043 Duration: 43.146 ms Changes: ---------- diff: --- +++ @@ -39,7 +39,7 @@ # prevent Apache from glomming onto all bound IP addresses. # #Listen 12.34.56.78:80 -Listen 80 +Listen 10.10.10.101:8080 # # Dynamic Shared Object (DSO) Support ---------- ID: apache-service Function: service.running Name: httpd Result: True Comment: Service reloaded Started: 15:51:09.467051 Duration: 5225.763 ms Changes: ---------- httpd: True Summary for linux-node01 ------------ Succeeded: 3 (changed=2) Failed: 0 ------------ Total states run: 3 Total run time: 6.768 s linux-node02: ---------- ID: apache-install Function: pkg.installed Name: httpd Result: True Comment: All specified packages are already installed Started: 15:51:08.182471 Duration: 1467.794 ms Changes: ---------- ID: apache-config Function: file.managed Name: /etc/httpd/conf/httpd.conf Result: True Comment: File /etc/httpd/conf/httpd.conf updated Started: 15:51:09.654130 Duration: 56.25 ms Changes: ---------- diff: --- +++ @@ -39,7 +39,7 @@ # prevent Apache from glomming onto all bound IP addresses. # #Listen 12.34.56.78:80 -Listen 80 +Listen 10.10.10.102:8080 # # Dynamic Shared Object (DSO) Support ---------- ID: apache-service Function: service.running Name: httpd Result: True Comment: Service reloaded Started: 15:51:09.792219 Duration: 5267.125 ms Changes: ---------- httpd: True Summary for linux-node02 ------------ Succeeded: 3 (changed=2) Failed: 0 ------------ Total states run: 3 Total run time: 6.791 s
第2章 SaltStack数据系统Pillar
备注:官方文档:https://docs.saltstack.com/en/latest/ref/pillar/index.html
2.1 Pillar和Grains区别
名称 | 存储位置 | 数据类型 | 数据采集更新方式 | 应用 |
Grains | Minion端 | 静态数据 | Minion启动时采集,也可以使用salt '*' saltutil.sync_grains进行刷新 | 存在Minion基本数据,比如用于匹配Minion,自身数据可以用来做资产管理等 |
Pillar | Master端 | 动态数据 | 在Master端定义,指定给对应的Minion,可以使用salt '*' saltutil.refresh_pillar进行刷新 | 存储Master指定的数据,只有指定的Minion可以看到,用于敏感数据保存 |
2.2 配置pillar
2.2.1 编辑master配置文件
[root@linux-node01 ~]# vim /etc/salt/master 841 pillar_roots: 842 base: 843 - /srv/pillar/base 844 dev: 845 - /srv/pillar/dev 846 test: 847 - /srv/pillar/test 848 prod: 849 - /srv/pillar/prod
2.2.2 根据配置文件创建目录
[root@linux-node01 ~]# mkdir -p /srv/pillar/{base,dev,test,prod}
2.2.3 重启master服务
[root@linux-node01 ~]# systemctl restart salt-master
2.3 使用pillar
2.3.1 编写sls文件
[root@linux-node01 ~]# vim /srv/pillar/base/top.sls base: '*': - web [root@linux-node01 ~]# vim /srv/pillar/base/web.sls {% if grains['fqdn'] == 'linux-node01' %} package: nginx {% elif grains['fqdn'] == 'linux-node02' %} package: httpd {% endif %}
2.3.2 刷新pillar
[root@linux-node01 ~]# salt '*' saltutil.refresh_pillar linux-node01: True linux-node02: True
2.3.3 查询pillar数据
[root@linux-node01 ~]# salt '*' pillar.items linux-node01: ---------- package: nginx linux-node02: ---------- package: httpd [root@linux-node1 ~]# salt -I package:httpd cmd.run hostname linux-node02: linux-node02
2.3.4 使用pillar数据
[root@linux-node01 ~]# vim /srv/salt/base/web/web.sls web-install: pkg.installed: - pkgs: - {{ pillar['package'] }} [root@linux-node01 ~]# salt '*' state.sls web.web

我的微信
如果有技术上的问题可以扫一扫我的微信