vlei 发表于 2018-7-31 10:58:13

saltstack (2) 常用模块

  1、crontab模块
  # salt 'redis02-jp' cron.raw_cron root      查看指定用户的计划任务
  redis02-jp:
  # salt 'redis02-jp' cron.set_job root '55' '14' '*' '*' '*' '/bin/echo "xxx" > /tmp/test.log'   创建任务
  redis02-jp:
  new
  # salt 'redis02-jp' cron.raw_cron root   再次查看任务
  redis02-jp:
  # Lines below here are managed by Salt, do not edit
  55 14 * * * /bin/echo "xxx" > /tmp/test.log
  # salt 'redis02-jp'cron.rm_job root '/bin/echo "xxx" > /tmp/test.log'      删除任务
  redis02-jp:
  removed
  # salt 'redis02-jp' cron.raw_cron root      再次查看任务
  redis02-jp:
  # Lines below here are managed by Salt, do not edit
  #
  2、远程运行命令cmd.run模块和名称解析dnsutil模块
  # salt 'redis02-jp' cmd.run 'cat /etc/hosts'
  redis02-jp:
  127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
  ::1         localhost6 localhost6.localdomain6
  172.31.24.139 redis01-jp
  172.31.24.140 redis02-jp
  172.31.24.141 redis03-jp
  # salt 'redis02-jp' dnsutil.hosts_append /etc/hosts 1.1.1.1 www.example_salt.com添加本地解析条目
  redis02-jp:
  The following line was added to /etc/hosts:
  1.1.1.1 www.example_salt.com
  # salt 'redis02-jp' cmd.run 'cat /etc/hosts'   查看/etc/hosts
  redis02-jp:
  127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
  ::1         localhost6 localhost6.localdomain6
  172.31.24.139 redis01-jp
  172.31.24.140 redis02-jp
  172.31.24.141 redis03-jp
  1.1.1.1 www.example_salt.com
  # salt 'redis02-jp' dnsutil.hosts_remove /etc/hosts www.example_salt.com删除本地解析条目
  redis02-jp:
  None
  # salt 'redis02-jp' cmd.run 'cat /etc/hosts'   再次查看/etc/hosts
  redis02-jp:
  127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
  ::1 localhost6 localhost6.localdomain6
  172.31.24.139 redis01-jp
  172.31.24.140 redis02-jp
  172.31.24.141 redis03-jp
  #
  3、网络模块network
  # salt 'redis02-jp' network.dig www.qq.com   名称解析
  redis02-jp:
  ; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.47.rc1.51.amzn1 <<>> www.qq.com
  ;; global options: +cmd
  ;; Got answer:

  ;; ->>HEADER<<- opcode: QUERY, status: NOERROR,>  ;; flags: qr rd ra; QUERY: 1, ANSWER: 4, AUTHORITY: 0, ADDITIONAL: 0
  ;; QUESTION SECTION:
  ;www.qq.com.INA
  ;; ANSWER SECTION:
  www.qq.com.60INCNAMEqq.com.edgesuite.net.
  qq.com.edgesuite.net.60INCNAMEa1574.b.akamai.net.
  a1574.b.akamai.net.11INA96.17.151.16
  a1574.b.akamai.net.11INA96.17.151.18
  ;; Query time: 5 msec
  ;; SERVER: 172.31.0.2#53(172.31.0.2)
  ;; WHEN: Mon Dec 19 07:24:33 2016

  ;; MSG>  # salt 'redis02-jp' network.ping www.baidu.com    网络连通性测试
  redis02-jp:
  PING www.a.shifen.com (103.235.46.39) 56(84) bytes of data.
  64 bytes from 103.235.46.39: icmp_seq=1 ttl=50 time=84.0 ms
  64 bytes from 103.235.46.39: icmp_seq=2 ttl=50 time=84.1 ms
  64 bytes from 103.235.46.39: icmp_seq=3 ttl=50 time=84.1 ms
  64 bytes from 103.235.46.39: icmp_seq=4 ttl=50 time=84.9 ms
  --- www.a.shifen.com ping statistics ---
  4 packets transmitted, 4 received, 0% packet loss, time 3003ms
  rtt min/avg/max/mdev = 84.068/84.321/84.935/0.459 ms
  # salt 'redis02-jp' network.traceroute www.qq.com      网络路径跟踪测试
  ......
  # salt 'redis02-jp' network.hwaddr eth0          查看MAC地址
  redis02-jp:
  06:cf:b1:b8:61:bf
  # salt 'redis02-jp' network.in_subnet 172.31.0.0/16   测试接口所在子网
  redis02-jp:
  True
  # salt 'redis02-jp' network.interfaces       查看指定机器的网络接口
  # salt 'redis02-jp' network.ip_addrs          查看指定机器的IP地址
  redis02-jp:
  - 172.31.24.141
  # salt 'redis02-jp' network.subnets            查看指定机器的子网网段
  redis02-jp:
  - 172.31.16.0/20
  #
  4、复制模块cp
  # salt 'redis02-jp' cp.get_file salt://files/file1 /tmp/file1    文件复制
  redis02-jp:
  /tmp/file1
  # salt 'redis02-jp' cp.get_file salt://files/file2 /tmp/file2
  redis02-jp:
  /tmp/file2
  # ls /srv/salt/files/
  file1file2file3
  #
  # salt 'redis02-jp' cp.get_dir salt://files/tmp/         #将主控端file_roots指定位置下的目录复制到被控主机/tmp/目录下
  redis02-jp:
  - /tmp//files/file1
  - /tmp//files/file2
  - /tmp//files/file3
  #
  # salt '*' cp.get_url http://www.slashdot.ort /tmp/index.html   #下载URL内容到被控主机指定位置(/tmp/index.html)
  # salt '*' cp.get_file salt://vimrc /etc/vimrc gzip=5    #传输时压缩文件
  Valid values are integers from 1 to 9, where 1 is the lightest compression and 9 the heaviest. In other words, 1 uses the least CPU on the master (and minion), while 9 uses the most.
  # salt '*' cp.get_file salt://vimrc /etc/vim/vimrc makedirs=True    #如果没有目标目录,则创建
  5、文件操作模块file
  参考:http://docs.saltstack.cn/ref/modules/all/salt.modules.file.html#module-salt.modules.file
  # salt 'redis02-jp' file.get_sum /etc/fstab md5   # 查看文件md5值
  redis02-jp:
  44c60b006413b0fc1b98c061d4b339ff
  # salt 'redis02-jp' file.check_hash /etc/fstab md5:'44c60b006413b0fc1b98c061d4b339ff'
  redis02-jp:
  True
  #
  # salt 'redis02-jp' file.chown /tmp/file1 push push#修改文件属主、属组
  redis02-jp:
  None
  # salt 'redis02-jp' cmd.run 'ls -l /tmp/file1'
  redis02-jp:
  -rw-r--r-- 1 push push 0 Dec 19 04:12 /tmp/file1
  #
  # salt 'redis02-jp' file.copy /etc/passwd /tmp/passwd    #远程主机复制文件
  redis02-jp:
  True
  # salt 'redis02-jp' cmd.run 'ls /tmp/passwd'
  redis02-jp:
  /tmp/passwd
  # salt 'redis02-jp' file.file_exists /tmp/passwd    #测试文件是否存在
  redis02-jp:
  True
  # salt 'redis02-jp' file.directory_exists /tmp/files   # 测试目录是否存在
  redis02-jp:
  True
  #
  # salt 'redis02-jp' file.stats /tmp/passwd   #查看文件meta信息
  redis02-jp:
  ----------
  atime:
  1482131501.34
  ctime:
  1482131501.34
  gid:
  0
  group:
  root
  inode:
  281103
  mode:
  0644
  mtime:
  1482131501.34

  >  1321
  target:
  /tmp/passwd
  type:
  file
  uid:
  0
  user:
  root
  # salt 'redis02-jp' file.access /etc/passwd r      #测试文件是否有r权限
  redis02-jp:
  True
  # salt 'redis02-jp' file.access /etc/passwd x      #测试文件是否有x权限
  redis02-jp:
  False
  #
  # salt 'redis02-jp' file.get_mode /tmp/passwd      #查看文件权限
  redis02-jp:
  0644
  # salt 'redis02-jp' file.set_mode /tmp/passwd 755   #设置文件权限
  redis02-jp:
  0755
  # salt 'redis02-jp' file.mkdir /tmp/dir1   #创建目录
  redis02-jp:
  True
  # salt 'redis02-jp' file.sed /tmp/passwd 'push' 'work'   #替换文件内容
  redis02-jp:
  ----------
  pid:
  4621
  retcode:
  0
  stderr:
  stdout:
  # salt 'redis02-jp' cmd.run 'tail -1 /tmp/passwd'
  redis02-jp:
  work:x:501:501::/home/work:/bin/bash
  # salt 'redis02-jp' file.append /tmp/passwd 'saltstack file append test'#追回文件内容
  redis02-jp:
  Wrote 1 lines to "/tmp/passwd"
  # salt 'redis02-jp' cmd.run 'tail -2 /tmp/passwd'
  redis02-jp:
  work:x:501:501::/home/work:/bin/bash
  saltstack file append test
  # salt 'redis02-jp' file.remove /tmp/file1   # 可以删除指定文件
  redis02-jp:
  True
  #
  # salt 'redis02-jp' file.remove /tmp/dir1    #可以删除空目录
  redis02-jp:
  True
  # salt 'redis02-jp' file.remove /tmp/files    # 可以删除非空目录
  redis02-jp:
  True
  # salt 'redis02-jp' file.contains /etc/passwd 'push'    # 测试文件中是否包含push字符串
  redis02-jp:
  True
  # salt 'redis02-jp' file.contains /etc/passwd 'work'
  redis02-jp:
  False
  #
  # cp /etc/passwd /srv/salt/files/
  # salt 'redis02-jp' file.get_diff /etc/passwd salt://files/passwd   #比较文件内容差异
  redis02-jp:
  --- /etc/passwd
  +++ salt://files/passwd
  @@ -23,6 +23,3 @@
  sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
  dbus:x:81:81:System message bus:/:/sbin/nologin
  ec2-user:x:500:500:EC2 Default User:/home/ec2-user:/bin/bash
  -apache:x:48:48:Apache:/var/www:/sbin/nologin
  -nginx:x:498:497:Nginx web server:/var/lib/nginx:/sbin/nologin
  -push:x:501:501::/home/push:/bin/bash
  #
  # salt 'redis02-jp' file.grep /etc/passwd push    #过滤内容
  redis02-jp:
  ----------
  pid:
  5124
  retcode:
  0
  stderr:
  stdout:
  push:x:501:501::/home/push:/bin/bash
  #
  6、包管理模块pkg与服务管理模块service
  # salt 'redis02-jp' pkg.install php      #安装指定的软件包
  redis02-jp:
  ----------
  compat-gmp4:
  ----------
  new:
  4.3.2-1.14.amzn1
  old:
  httpd:
  ----------
  new:
  2.2.31-1.8.amzn1
  old:
  php:
  ----------
  new:
  5.3.29-1.8.amzn1
  old:
  php-cli:
  ----------
  new:
  5.3.29-1.8.amzn1
  old:
  php-common:
  ----------
  new:
  5.3.29-1.8.amzn1
  old:
  #
  # salt 'redis02-jp' pkg.remove nginx   #卸载包
  redis02-jp:
  ----------
  # salt 'redis02-jp' pkg.upgrade   #系统更新
  # salt 'redis02-jp' service.enable httpd    #设置服务开机自启动
  redis02-jp:
  True
  # salt 'redis02-jp' service.disable httpd   #禁止服务开机自启动
  redis02-jp:
  True
  # salt 'redis02-jp' service.start httpd   #启动服务
  redis02-jp:
  True
  # salt 'redis02-jp' service.reload httpd
  redis02-jp:
  True
  # salt 'redis02-jp' service.restart httpd
  redis02-jp:
  True
  # salt 'redis02-jp' service.status httpd
  redis02-jp:
  True
  # salt 'redis02-jp' service.stop httpd
  redis02-jp:
  True
  #
页: [1]
查看完整版本: saltstack (2) 常用模块