salt相关

 
salt安装  https://docs.saltstack.com/en/latest/topics/installation/index.html#quick-install
 
salt远程执行命令
 salt  '<target>' <function> [arguments]
 # salt    '*'     service.status     mysqld
 
terget:
全局配置 * ?  []    
      salt  'tencen[a-z]'  test.ping
 -E  正则
     salt -E '[a-z]{0,8}' test.ping
     salt -E  '^ten.*t$'  test.ping
-L   列表
    salt -L 'minion01,minion02' test.ping
-G grains
     salt -G 'os:CentOS'  cmd.run 'w'
-C  复合
    salt -C 'G@os:Ubuntu and webser* or E@database.*'  cmd.run 'w'
  
查看function
salt '*' sys.list_modules
salt '*' sys.list_functions
salt '*' sys.list_functions test
salt '*' sys.doc test.ping 
 
salt-key 密钥管理,通常在master端执行
salt-key [options]
salt-key -L #查看所有minion-key
 -a minionid | -A  #接受某个minion|接受所有
-d minionid  |-D    #删除某个minion|删除所有
 
salt-run
salt-run manage.status   #查看所有minion状态
salt-run manage.up|down     #查看所有在线|不在线minion
 
salt-call    minion端自己执行模块
salt-call cmd.run 'ifconfig'    #自己执行cmd.run
salt-call -l debug state.sls zabbix   # 自己执行master上的zabbix.sls文件
 
salt-cp
salt-cp  下发文件 不用写目标名
salt-cp 'minion01'  s2n.py  /root   #  将本地s2n.py发送到远程minion的root目录下

常用模块

cp模块
salt '*' cp.get_file salt://test.txt /root/test.txt    # 下发文件  从master到minion   要写目标文件名
salt "*" cp.get_dir salt://testdir/ /root    #下发目录   从master到minion的/root下
 
cp.push   从minion发文件到master
需要先在master设置 file_recv: True

salt 'minion01' cp.push /root/test.log 

把minion01的文件发到了master /var/cache/salt/master/minions/minion01/files/root/test.log 下面

 
文件管理模块 file

# salt 'minion01'  file.copy /root/test.py /root/test.py.bak   # 将minion端的一个文件copy成另一个文件

#salt '*' file.chown /etc/passwd root root 修  改文件属组
 
# salt 'test01'   file.append /opt/test.txt 'salt test file001'   追加内容

远程执行命令 cmd
#salt -v --out=json "*" cmd.run "ps aux |wc -l"

master向minion推送脚本执行
salt '*' cmd.script salt://scripts/runme.sh 

安装包 pkg
#salt "*" pkg.install "httpd" 安装
pkg.version 

pkg.remove

管理服务模块 service
salt "*" service.status "httpd"

service.start|stop|restart


用户管理模块 user
salt  '*'  user.add  'test' shell=/sbin/nologin

user.delete  
user.info 

计划任务cron
查看:
# salt 'me' cron.ls root
添加:
# salt 'test*' cron.set_job root '0' '0' '*' '*' '*' '/bin/bash /root/scripts/nginxlogcron.sh' comment='Nginx 日志切分'
删除:
# salt 'test01' cron.rm_job root '/bin/bash /root/scripts/nginxlogcron.sh'
注意 :删除任务只能删除由salt 添加的计划任务,要想删除以前手动添加的, 需要在原本的计划任务上面添加一行:
# Lines below here are managed by Salt, do not edit
 
 
 
state.sls文件
https://docs.saltstack.com/en/latest/ref/states/all/
# salt '*' sys.list_state_modules          //查看state所有模块
# salt '*' sys.list_state_functions  pkg     // 查看state指定模块的所有方法
# salt '*' sys.state_doc pkg            // 查看模块方法的详细用法
# salt '*' sys.state_doc pkg.upgrade    // 查看某个方法的详细用法
 
yaml 格式
  • 缩进   两个空格,不能tab键。表示层级关系
  • 冒号   冒号后面一个空格,除了以冒号结尾和路径中的冒号
  • 短横线   后面跟一个空格。表示列表项。

state.sls的写法:

# cat  test.sls 
lftp:        # name
  pkg.installed   #状态模块

installed-ssh:    # ID 必须唯一。一个id声明下,状态模块不能重复使用。
  pkg.installed:
    - names:        # names复数,多个值,一行一个,列表项
      - openssh-server
      - openssh-clients

 执行:  salt '*' state.sls test  

 

jinja模板语法:变量用{{...}}, 表达式用{%...%}

1. 设置jinja变量

{% set var='hello world!' %}
test_var:
  cmd.run:
      - name: echo "var is {{var}}"

 设置:

字符串: {% set var='hello world!' %}    

       引用: {{var}}

列表:    {% set list = ['one','two','three'] %}        

              {{ list[1] }}

字典:    {% set dic = {'a':'11','b':'22'} %}      

              {{ dic['a'] }}

2,流程控制语句

For循环

{% for user in users %}
  {{user}}
{% endfor %}
#字典
{% for key,value in my_dict.iteritems() %} {{ key }} {{ value }} {% endfor %}

 举例:

举例:
dir-tomcat:
  file.directory:
    {% for Dir in ['bin','conf','lib','logs','temp','webapps','work'] %}
    - names: 
      - /usr/local/tomcat/{{Dir}}
    {% endfor %}
    - makedirs: True
    - user: root
    - group: root
    - dir_mode: 755
    - file_mode: 644
例2:

 

grains   描述minion本身固有属性的静态数据。例如,操作系统版本,内存,网卡的mac地址等
# salt '*' grains.get fqdn

# salt "minion-2" grains.items
# salt "*" grains.ls

检索某一grains数据用如下命令:
#salt 'minion-2' grains.item os

了解操作系统信息后 ,可以使用grains定位主机,用-G或者--grains对只是centos的机器定位
#salt -G "os:CentOS" test.ping
#salt -G "hwaddr_interfaces:eth0:00:16:3e:06:39:af" test.ping

 
 
pillar
#配置pillar_roots
# vim /etc/salt/master
pillar_roots:
  base:
    - /srv/pillar     

#  salt  '*' saltutil.refresh_pillar   pillar刷新

 

使用 jinja 模板
1.File状态使用template参数  - template: jiaja
2.File状态模块要指定变量列表:
 - defaults:
    Server: 192.168.1.10
3.模板文件里面调用  {{Server}}
 
file模块   
notice:只有file模块里才可以使用template函数
{% set confdir="/etc/zabbix" %}
zabbix-conf:
  file.managed:
    - name: {{confdir}}/zabbix_agentd.conf
    - source: salt://file/zabbix_agentd.conf
    - user: root
    - group: root
    - mode: 755
    - require:
      - pkg: zabbix-pkg
    - template: jinja
    - defaults:
      SERVER: {{ pillar['zabbix-agent'] }}  #SERVER变量

# 定义pillar值 # cat /srv/pillar/zabbix-agent.sls zabbix-agent: 192.168.1.10

 # cat /srv/pillar/top.sls
 base:
   '*':
     - zabbix-agent

# vim file/zabbix_agentd.conf      #文件里面使用变量
Server={{ SERVER }}            
Hostname={{ grains['fqdn'] }}    
 
pkgrepo模块  用来管理软件包存储库
zabbix-yum:    # ID号
pkgrepo.managed:     # 方法名
- name: zabbix           #参数   zabbix是文件名 发到客户端为/etc/yum.repos.d/zabbix.repo  文件
- humanname: Zabbix Official Repository - $basearch    
- baseurl: http://repo.zabbix.com/zabbix/3.4/rhel/7/$basearch/
- enabled: 1
- gpgcheck: 0
- gpgkey: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591
 
 
 
 

转载于:https://www.cnblogs.com/xiaobaozi-95/p/9389842.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值