SSCCGo 2020-03-28
安装,官方文档
firewall
firewall-cmd --permanent --zone=public --add-port=4505-4506/tcp firewall-cmd --reload
or iptables
-A INPUT -m state --state new -m tcp -p tcp --dport 4505:4506 -j ACCEPT
systemctl start salt-master systemctl start salt-minion systemctl enable salt-master systemctl enable salt-minion
配置,官方文档
mastermaster配置详解
配置监听端口
/etc/salt/master --> interface
配置accept public keys from minion
/etc/salt/master --> auto_accept
或者手动允许minion key
salt-key # 查看keys 也可以使用salt-key -L salt-key -A # 通过所有key
minionminion配置详解
指定salt-master服务器
/etc/salt/minion --> master
可以指定ip或者hostname
grains操作
可以手工编写/etc/grains 文件yaml格式,然后使用命令同步grains
salt ‘*‘ saltutil.sync_grains
查看grains方法
salt ‘*‘ grains.
查看grains
ls
查看所有grains的key
salt ‘*‘ grains.ls
items
查看所有grains的key和value
salt ‘*‘ grains.items
item
查看一个或者多个key
salt ‘*‘ grains.item os salt ‘*‘ grains.item os osrelease oscodename
get
salt ‘*‘ grains.get pkg:apache salt ‘*‘ grains.get abc::def|ghi delimiter=‘|‘‘
新增grains
以下操作均会同步grains config file
append
salt ‘*‘ grains.append key val
setval
salt ‘*‘ grains.setval key val salt ‘*‘ grains.setval key "{‘sub-key‘: ‘val‘, ‘sub-key2‘: ‘val2‘}"
setvals 可以传递python字典结构
salt ‘*‘ grains.setvals "{‘key1‘: ‘val1‘, ‘key2‘: ‘val2‘}" salt ‘*‘ grains.setvals "{‘A‘:[{‘B1‘: {‘C‘:‘C_value‘}},‘B2‘]}" # A: #- B1: # C: C_value #- B2
set 可以通过:分隔层级选中key来设置value
salt ‘*‘ grains.set ‘apps:myApp:port‘ 2209 salt ‘*‘ grains.set ‘apps:myApp‘ ‘{port: 2209}‘
删除grains
remove
salt ‘*‘ grains.remove key val
delval
salt ‘*‘ grains.delval key
delkey
salt ‘*‘ grains.delkey key
其他方法
equals
确认minion的grains为给定的值
注意当value为列表时需要给定列表的所有值
salt ‘*‘ grains.equals roles [‘salt-master‘,‘k8s-master‘]
fetch
get方法的别名 在salt源代码中 fetch的实现为
# Provide a jinja function call compatible get aliased as fetch fetch = get
filter_by
根据给定的字典返回指定的key
可以传递位置参数也可以键值对传餐
""" :param lookup_dict: 给定dict :param grain: 指定需要匹配的grains key,默认为‘os_family‘ :param merge: 默认为None 联合lookup_dict :param default: 默认为‘default‘ 如果在给定的dict中没有匹配到 那就使用defult指定的key :param base: 默认为None 配合merge使用 """例子中的os_family如果是RedHat会返回给定的列表中的os_family is RedHat
salt ‘*‘ grains.filter_by ‘{Debian: os_family is Debain,RedHat: os_family is RedHat}‘ os_family salt ‘*‘ grains.filter_by ‘{default: {A: {B: C}, D: E}, F: {A: {B: G}}, H: {D: I}}‘ ‘xxx‘ ‘{D: J}‘ ‘F‘ ‘default‘
更多的应用在jinja中
{% set apache = salt[‘grains.filter_by‘]({ ‘Debian‘: {‘pkg‘: ‘apache2‘, ‘srv‘: ‘apache2‘}, ‘RedHat‘: {‘pkg‘: ‘httpd‘, ‘srv‘: ‘httpd‘}, }, default=‘Debian‘) %} myapache: pkg.installed: - name: {{ apache.pkg }} service.running: - name: {{ apache.srv }}
get_or_set_hash
获取或者生成hash值 例子中的50代表hash的长度
salt ‘*‘ grains.get_or_set_hash ‘django:SECRET_KEY‘ 50
在sls中使用
some_mysql_user: mysql_user: - present - host: localhost - password: {{ salt[‘grains.get_or_set_hash‘](‘mysql:some_mysql_user‘) }}
has_value
判断给定的key有没有值
salt ‘*‘ grains.has_value systemd:version
列出所有模块
salt ‘*‘ sys.list_modules
列出所有函数
salt ‘*‘ sys.list_functions