Saltstack 数据系统

Saltstack 数据系统
Grains (谷粒)
Pillar (柱子)

Grains:静态数据 当Minion 启动的时候手机的minion本地的相关信息,操作系统版本内核版本,CPU,内存,硬盘,设备型号,序列号
1.资产管理,信息查询
2.用于目标选择
3.配置管理
salt ‘zf-200’ grains.item
salt ‘’ grains.item os #查看所有机器的操作系统
salt '
’ grains.item ipv4 #查看所有机器的IP
salt -G ‘os:CentOS’ test.ping #在centos上测试连接
定义grains 配置
vim /etc/salt/grains

grains:
roles: apache
systemctl restart salt-minion.service #重启minion
salt -G ‘roles:apache’ cmd.run ‘systemctl restart httpd’ #查找装有apache的机器并重启服务

top file使用案例

base:
  'zf-202':
    - web.apache
  'zf-200':
    - web.apache
  'roles:apache':
    -  match: grain
    -  web.apache

开发一个Grains:
python 写一个python脚本 返回一个字典
#!/usr/bin/env python
def my_grains():
#初始化grains字典
grains = {}
#设置字典中的key-vlaue
grains[‘iaas’] = ‘openstack’
grains[‘edu’] = ‘oldboyedu’
#返回这个字典
return grains
#刷新grains
salt ‘*’ saltutil.sync_grains
Grians优先级
1.系统自带
2.granin

Pillar: Pillar动态,给特定的minion指定特定的数据。只有指定的minion自己能看到自己的数据
vim /etc/salt/master
pillar_opts: True
salt ‘*’ pillar.items #查看pillar

深入学习saltstack远程执行:
salt '’ cmd.run ‘w’
命令:salt
目标:

模块 cmd.run 自带150+模块。 自己写模块
返回: 执行后结果返回,Returnners
目标: Targeting
一种和Minion ID 有关
一种和Minion ID无关
1.Minion ID有关的方法
通配符
[root@zf-200 ~]# salt ‘zf-20*’ test.ping
zf-200:
True
zf-201:
True
zf-202:
True
[root@zf-200 ~]# salt ‘zf-20?’ test.ping
zf-201:
True
zf-202:
True
zf-200:
True
列表
[root@zf-200 ~]# salt -L ‘zf-200,zf-202’ test.ping
zf-202:
True
zf-200:
True
正则表达式
所有匹配目标的方式,都可以用到top file 里面来指定目标。
主机名设置方案
1.IP地址
2.根据业务来进行设置
salt-cp ‘*’ /etc/hosts /opt/hehe

salt-cp  '*'   /etc/hosts    /opt/hehe               **#远程执行拷贝命令**
[root@zf-200 ~]#  salt  '*'  cmd.run  'ls -l /opt/hehe'
zf-200:
    -rw-r--r-- 1 root root 158 Jun  9 13:47 /opt/hehe
zf-202:
    -rw-r--r-- 1 root root 158 Jun  9 13:47 /opt/hehe
zf-201:
    -rw-r--r-- 1 root root 158 Jun  9 13:47 /opt/hehe
salt '*'   state.single  pkg.installed name=MySQL-python              

salt ‘*’ cmd.run ‘yum -y install MySQL-python’

#https://www.unixhot.com/docs/saltstack/ref/returners/all/salt.returners.mysql.html#module-salt.returners.mysql
写入数据库
#salt ‘*’ cmd.run ‘df -h’ --return mysql

猜你喜欢

转载自blog.csdn.net/weixin_43546282/article/details/91351179