SaltStack 批量操作Windows

一、基础环境
[ ~]#  cat /etc/redhat-release 
CentOS Linux release 7.7.1908 (Core)
[ ~]# python --version
Python 2.7.5

二、SaltSatck 安装
完全按照官方文档来即可。
https://repo.saltstack.com/2018.3.html#rhel

三、Master 配置文件

默认配置即可正常工作。

[ ~]# cat /etc/salt/master  | grep -v ^# |grep -v ^$
  default_include: master.d/*.conf
  file_roots:
    base:
      - /srv/salt
    dev:
      - /srv/salt/dev/services
      - /srv/salt/dev/states
    prod:
      - /srv/salt/prod/services
      - /srv/salt/prod/states

salt-minion 安装略。

四、通过cmd模块操作windows

1.先看下cmd模块包含哪些方法

[ ~]# salt ‘BJ-WJ-T-DCS-01‘ sys.list_functions cmd 
BJ-WJ-T-DCS-01:
    - cmd.exec_code
    - cmd.exec_code_all
    - cmd.has_exec
    - cmd.powershell
    - cmd.powershell_all
    - cmd.retcode
    - cmd.run
    - cmd.run_all
    - cmd.run_bg
    - cmd.run_chroot
    - cmd.run_stderr
    - cmd.run_stdout
    - cmd.script
    - cmd.script_retcode
    - cmd.shell
    - cmd.shell_info
    - cmd.shells
    - cmd.tty
    - cmd.which
    - cmd.which_bin
    - cmd.win_runas

2.看看相关方法的用法

#如
salt ‘BJ-WJ-T-DCS-01‘ sys.doc cmd.run 

salt ‘BJ-WJ-T-DCS-01‘ sys.doc cmd.powershell

3.测试一下cmd.run和cmd.script

cmd.run

~]# salt ‘BJ-WJ-T-DCS-01‘ cmd.run ‘hostname‘  shell=‘powershell‘
BJ-WJ-T-DCS-01:
    BJ-WJ-T-DCS-01

[ scripts]# salt ‘BJ-WJ-T-DCS-01‘ cmd.run ‘get-hotfix -hfid KB4532931‘  shell=‘powershell‘
BJ-WJ-T-DCS-01:

    Source        Description      HotFixID      InstalledBy          InstalledOn  
    ------        -----------      --------      -----------          -----------  
    BJ-WJ-T-DC... Update           KB4532931     NT AUTHORITY\SYSTEM  2020/4/10 ...

cmd.script

#脚本内容,获取指定kb的安装时间
[ scripts]# cat get_hotfix.ps1 
 $a = Get-HotFix  | Where-Object {$_.HotFixid -eq "KB4532931"}

 $a.InstalledOn

 #用salt 执行
 [ scripts]# salt ‘BJ-WJ-T-DCS-01‘ cmd.script salt://scripts/get_hotfix.ps1 shell=‘powershell‘
BJ-WJ-T-DCS-01:
    ----------
    pid:
        3916
    retcode:
        0
    stderr:
    stdout:

        2020?4?10? 0:00:00

五、批量操作

最简单的可以定义组:

[ scripts]# cat /etc/salt/master.d/nodegroups.conf 
nodegroups:
  jump_win: ‘,BJ-WJ-Jump-02,BJ-DX-JumpLS-01‘
  demo: ‘,BJ-WJ-T-DCS-02‘

然后对组进行批量操作:

[ scripts]#  salt -N demo cmd.run ‘ipconfig ‘ shell=‘powershell‘
BJ-WJ-T-DCS-01:

    Windows IP Configuration

    Ethernet adapter 以太网:

       Connection-specific DNS Suffix  . : 
       IPv4 Address. . . . . . . . . . . : 172.16.7.54
       Subnet Mask . . . . . . . . . . . : 255.255.255.0
       Default Gateway . . . . . . . . . : 172.16.7.1

    Tunnel adapter isatap.{77A1A005-DE37-488B-A1E7-98CDB838E01E}:

       Media State . . . . . . . . . . . : Media disconnected
       Connection-specific DNS Suffix  . :
BJ-WJ-T-DCS-02:

    Windows IP Configuration

    Ethernet adapter 以太网:

       Connection-specific DNS Suffix  . : 
       IPv4 Address. . . . . . . . . . . : 172.16.7.55
       Subnet Mask . . . . . . . . . . . : 255.255.255.0
       Default Gateway . . . . . . . . . : 172.16.7.1

    Tunnel adapter isatap.{2526DE3F-E74C-4175-B209-B15F924284BB}:

       Media State . . . . . . . . . . . : Media disconnected
       Connection-specific DNS Suffix  . :

相关推荐