设为首页 收藏本站
查看: 1287|回复: 0

[经验分享] saltstack常用功能

[复制链接]

尚未签到

发表于 2018-7-31 06:54:21 | 显示全部楼层 |阅读模式
1.查看版本
  # salt --version

  salt 2015.5.8 (Lithium)
  [root@localhost salt]# salt --version-report
  Usage: salt [options] '<target>' <function> [arguments]
  salt: error: no such option: --version-report

  # salt --versions-report

  

      Salt: 2015.5.8  

  Python: 2.6.6 (r266:84292, Jul 23 2015, 15:22:56)
  Jinja2: unknown
  M2Crypto: 0.20.2
  msgpack-python: 0.4.6
  msgpack-pure: Not Installed
  pycrypto: 2.0.1
  libnacl: Not Installed
  PyYAML: 3.10
  ioflo: Not Installed
  PyZMQ: 14.3.1
  RAET: Not Installed
  ZMQ: 3.2.5
  Mako: 0.3.4
  Tornado: Not Installed
  timelib: Not Installed
  dateutil: Not Installed


2. salt-key

查看所的有minion
  # salt-key –L

  Accepted Keys:
  192.168.10.110
  192.168.43.218
  Denied Keys:
  Unaccepted Keys:
  192.168.10.110
  Rejected Keys:


接受指定的minion

  # salt-key –a>  # salt-key -a 192.168.10.110

  The following keys are going to be accepted:
  Unaccepted Keys:
  192.168.10.110
  Proceed? [n/Y] Y


接受所有的minion
  # salt-key -A

删除指定的minion

  # salt-key –d>  # salt-key -d 192.168.10.110

  The following keys are going to be deleted:
  Accepted Keys:
  192.168.10.110
  Proceed? [N/y] y
  Key for minion 192.168.10.110 deleted.


删除所有的minion
  # salt-key -D

  The following keys are going to be deleted:
  Accepted Keys:
  192.168.10.110
  192.168.43.218
  Proceed? [N/y] y
  Key for minion 192.168.10.110 deleted.
  Key for minion 192.168.43.218 deleted.


拒绝指定的minion

  # salt-key –r>
拒绝所有的minion
  # salt-key -R

  The following keys are going to be rejected:
  Unaccepted Keys:
  192.168.10.110
  Proceed? [n/Y] Y
  Key for minion 192.168.10.110 rejected.


3. minion状态管理

查看存活的minion
  # salt-run manage.up



  • 192.168.10.249

查看死掉的minion
  #salt-run manage.down

查看down掉的minion,并将其删除
  # salt-run manage.down removekeys=True

查看minion的相关状态
  # salt-run manage.status
  # salt-run manage.status

  down:
  up:


  • 192.168.10.249

查看slat的所有master和minion的版本信息
  # salt-run manage.versions
  # salt-run manage.versions

  Master:
  2015.5.8
  2016.Up to date:
  ----------
  192.168.10.249:
  2015.5.8


4. 将minion分组
  分组规则:

  G --针对Grains做单个匹配,例如:G@os:Ubuntu
  E --针对minion针对正则表达式匹婚配,例如:E@webd+.(dev|qa|prod).loc
  P --针对Grains做正则表达式匹配,例如:P@os:(RedHat|Fedora|CentOS)
  L --针对minion做列表匹配,例如:L@minion1.test.com,minion3.domain.com or bl*.domain.com
  I --针对 Pillar 做单个匹配,例如:I@pdata:foobar
  S --针对子网或是IP做匹配,例如:S@192.168.1.0/24 or S@192.168.1.100
  R --针对客户端范围做匹配,例如:R@%foo.bar

  编辑master配置文件,注意组名前有两个空格,后有一个空格
  # vi /etc/salt/master

  nodegroups:
  web: 'L@192.168.10.110,192.168.10.111'

  # salt -N ‘web’ test.ping

  192.168.10.110:
  True

  首先要保证组员在master的列表中,否则对分组进行操作时会不成功:
  # salt -N group1 test.ping

  No minions matched the target. No command was sent, no jid was assigned.
  ERROR: No return received


5. salt文件管理
  编辑master主配置文件,做如下配置:

  file_roots:
  base:
  - /srv/salt
  dev:
  - /srv/salt/dev
  prod:
  - /srv/salt/prod

  

可以将文件及shell放在/srv/salt等文件夹下,执行其他命令调用方法:salt://dev/install.sh  

  如下执行脚本:
  # salt '192.168.10.110' cmd.script salt://test.sh

6. 压缩和解压缩:archive.tar
  通过tar压缩或解压文件。

  options:Options to pass to the tar command
  tarfile:The filename of the tar archive to pack/unpack
  sources:Comma delimited list of files to pack into the tarfile. Can also be passed as a Python list.
  dest:The destination directory into which to unpack the tarfile
  cwd : None,The directory in which the tar command should be executed. If not specified, will default to the home directory of the user under which the salt minion process is running.
  template : None
  Can be set to 'jinja' or another supported template engine to render the command arguments before execution:
  salt '' archive.tar cjvf /tmp/salt.tar.bz2 {{grains.saltpath}} template=jinja
  CLI Examples:
  # Create a tarfile
  salt '' archive.tar cjvf /tmp/tarfile.tar.bz2 /tmp/file_1,/tmp/file_2
  # Unpack a tarfile
  salt '*' archive.tar xf foo.tar dest=/target/directory



  • 执行压缩命令:  # salt '*' archive.tar zcvf /root/test.tar.gz /root/python,/root/testa

      192.168.10.249:
      - tar: Removing leading `/' from member names
      - /root/python/
      - /root/python/p2
      - /root/python/p1
      - /root/testa


  通过该方法压缩的文件,解压后带有全路径,可通过cwd指定执行的目录.
  # salt '*' archive.tar zcvf /root/test/test.tar.gz python,testa cwd=/root

  192.168.10.249:


  • python/
  • python/p2
  • python/p1
  • testa


  • 执行解压缩命令:  # salt '*' archive.tar zxvf /root/test/test.tar.gz dest=/root/test

      192.168.10.249:
      - python/
      - python/p2
      - python/p1
      - testa


7. 检测:test.ping
  测试主机的存活状态
  # salt ‘*’ test.ping
  # salt '192.168.10.110' test.ping

  192.168.10.110:
  True


8. 执行shell:cmd.run
  在minion上执行shell命令。
  # salt '192.168.10.110' cmd.run 'whoami'

  192.168.10.110:
  root


9. 执行脚本文件:cmd. script
  在文件管理中放入相应的脚本文件
  # cat /srv/salt/test.sh

  #!/bin/bash
  echo 'This is a test.'

  在minion上执行该脚本:
  # salt '192.168.10.110' cmd.script salt://test.sh

  192.168.10.110:
  ----------
  pid:
  23866
  retcode:
  0
  stderr:
  stdout:
  This is a test.


10. 查看磁盘利用率:disk.usage
  Return usage information for volumes mounted on this minion
  CLI Example:
  salt '*' disk.usage

11. 更改文件的所属权
  更改文件的所属权.
  file.chown(path, user, group)
  Chown a file, pass the file the desired user and group
  path:path to the file or directory
  user:user owner
  group:group owner
  CLI Example:
  salt '*' file.chown /etc/passwd root root

12. 复制文件
  file.copy(src, dst, recurse=False, remove_existing=False)
  将一个目录或文件夹复制到另一个地方
  如果复制一个文件夹必须加上recurse=True参数。默认为复制目的文件夹。
  remove_existing=True,首先将目的文件夹里的内容全部删除,再进行复制。该参数可以保证源和目的完全一样。
  CLI Example:

  salt '*' file.copy /path/to/src /path/to/dst
  salt '*' file.copy /path/to/src_dir /path/to/dst_dir recurse=True
  salt '*' file.copy /path/to/src_dir /path/to/dst_dir recurse=True remove_existing=True


13. 判断文件是否存在
  判断某个文件是否存在,返回true/false。
  CLI Example:

  salt '*' file.file_exists /etc/passwd


14. 判断文件夹是否存在
  判断一个文件夹是否存在,返回true/false。
  # salt '*' file.directory_exists /root/a

  192.168.10.249:
  True
  # salt '*' file.directory_exists /root/ab
  192.168.10.249:
  False


15. 查看目录大小
  返回目录大小(字节)。
  CLI Example:
  # salt '*' file.diskusage /root

  192.168.10.249:
  252580128


16. 查找文件

  salt '*' file.find / type=f name=*.bak>
  salt '*' file.find /var mtime=+30d>
  salt '*' file.find /var/log name=*.[0-9] mtime=+30d>

  •   1.查看大小大于10k,后缀为.bak的文件

      # salt '*' file.find /root type=f name=*.bak>
      192.168.10.249:
      - /root/sabawar/assets/tinymce/jscripts/tiny_mce/plugins/media/js/media.js.bak



    • 查看大小大于10k,后缀为.bak的文件,返回path,size,mtime
        # salt '*' file.find /root type=f name=*.bak>
        192.168.10.249:
        - /root/sabawar/assets/tinymce/jscripts/tiny_mce/plugins/media/js/media.js.bak
        - 16485
        - 1453970790




    • 查看大小大于10k,后缀为.bak的文件,并删除
        # salt '*' file.find /root type=f name=*.bak>
        192.168.10.249:
        - /root/sabawar/assets/tinymce/jscripts/tiny_mce/plugins/media/js/media.js.bak

        # salt '' file.find /root type=f name=\.bak>  192.168.10.249:



17. 过滤文件内容
  过滤某个文件的内容。同linux的grep。
  file.grep(path, pattern, *args)
  Note:This function's return value is slated for refinement in future versions of Salt
  path:A file path
  pattern:A string. For example: test a[0-5]
  args:grep options. For example: &quot; -v&quot; &quot; -i -B2&quot;
  CLI Example:

  salt '*' file.grep /etc/passwd nobody
  salt '*' file.grep /etc/sysconfig/network-scripts/ifcfg-eth0 ipaddr &quot; -i&quot;
  salt '*' file.grep /etc/sysconfig/network-scripts/ifcfg-eth0 ipaddr &quot; -i -B2&quot;
  salt '*' file.grep &quot;/etc/sysconfig/network-scripts/*&quot; ipaddr &quot; -i -l&quot;


18. 替换文件内容
  # cat a

  a
  b
  c

  # salt '*' file.replace /root/test/a pattern='a' repl='aa'

  192.168.10.249:
  ---
  +++
  @@ -1,3 +1,3 @@
  -a
  +aa
  b
  c

  # cat a

  aa
  b
  c


19. 更改文件的权限
  设置文件(夹)的权限
  salt '*' file.set_mode /etc/passwd 0644

20. 添加hosts文件
  添加一个host对,如果存在将对应的值添加进去,不存在则创建。
  # salt '*' hosts.add_host 192.168.10.249 test.com

  192.168.10.249:
  True


21. 检测端口
  在minion检测某个域名或主机的端口是否打开.
  CLI Example:

  salt '*' network.connect archlinux.org 80
  salt '*' network.connect archlinux.org 80 timeout=3
  salt '*' network.connect archlinux.org 80 timeout=3 family=ipv4
  salt '*' network.connect google-public-dns-a.google.com port=53 proto=udp timeout=3

  # salt '*' network.connect 192.168.10.249 22

  192.168.10.249:
  ----------
  comment:
  Successfully connected to 192.168.10.249 (192.168.10.249) on tcp port 22
  result:
  True

22. 获取hostname
  Get hostname
  CLI Example:

  # salt '*' network.get_hostname

  192.168.10.249:
  localhost.localdomain

23. 同步文件
  CLI Example:
  salt '' rsync.rsync {src} {dst} {delete=True} {update=True} {passwordfile=/etc/pass.crt} {exclude=xx}
  salt '' rsync.rsync {src} {dst} {delete=True} {excludefrom=/xx.ini}

  # salt '*' rsync.rsync /root/test /test

  192.168.10.249:
  ----------
  pid:
  5572
  retcode:
  0
  stderr:
  stdout:
  sending incremental file list
  test/
  test/a
  test/a.bak
  test/telnet.rpm
  test/soft/
  test/soft/telnet.rpm
  sent 108711 bytes  received 96 bytes  72538.00 bytes/sec

  total>

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-543778-1-1.html 上篇帖子: saltstack的安装及配置 下篇帖子: saltstack安装、认证
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表