SaltStack常用模块

目录

目录

环境

SaltStack模块介绍

SaltStack常用模块之network

network.active_tcp

network.calc_net

network.connect

network.default_route

network.get_fqdn

network.get_hostname

network.get_route

network.hw_addr

network.ifacestartswith

 network.in_subnet

network.interface

network.interface_ip

network.interfaces

network.ip_addrs

network.netstat

network.ping

SaltStack常用模块之service

service.available

service.get_all

 service.disabled

service.enabled

service.reload

service.stop

service.start

service.restart

service.status

SaltStack常用模块之pkg

 pkg.file_list

pkg.group_info

pkg.group_list

pkg.list_downloaded

 pkg.install

 pkg.list_pkgs

pkg.owner

 pkg.remove

pkg.upgrade

SaltStack常用模块之state

 state.show_highstate

 state.show_state_usage

state.show_top

state.top

state.show_sls

SaltStack常用模块之user

user.add

user.chuid

user.chgid

user.chgroups

user.delete

file模块

file.access

file.append

file.check_hash

file.chattr

file.copy

file.ditectory_exists

file.get_gid

file.get_group

file.get_hash

file.get_mode

file.get_selinux_context

file.get_sum

file.get_uid与file.get_user

 file.gid_to_group

file.group_to_gid

file.grep

file.is_blkdev

 file.lsattr

file.mkdir

file.move

file.prepend

 file.sed

 file.read

file.readdir

file.remove

file.rename

 file.set_mode

file.write


环境

master192.168.75.128
node1192.168.75.150
node2192.168.75.142
node3192.168.75.143

主要使用master和node1两台主机

SaltStack模块介绍

Module是日常使用SaltStack接触最多的一个组件,其用于管理对象操作,这也是SaltStack通过Push的方式进行管理的入口,比如我们日常简单的执行命令、查看包安装情况、查看服务运行情况等工作都是通过SaltStack Module来实现的。

当安装好Master和Minion包后,系统上会安装很多Module,大家可以通过以下命令查看支持的所有Module列表

[root@master ~]# salt '*' test.ping
node2:
    True
node1:
    True
node3:
    True
master:
    True
[root@master ~]# 

//查看node1主机所有module列表
node1:
    - acl
    - aliases
    - alternatives
    - apache

    - archive
    - artifactory
    - at
    ......
    - vbox_guest
    - virtualenv
    - vsphere
    - webutil
    - xfs
    - zenoss

//查看指定module的所有function
[root@master ~]# salt 'node1' sys.list_functions cmd
node1:
    - 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

//查看指定module的用法
[root@master ~]# salt 'node1' sys.doc user
user.add:

    Add a user to the minion

    name
        Username LOGIN to add

    uid
        User ID of the new account

    gid
        Name or ID of the primary group of the new account

    groups
        List of supplementary groups of the new account

    home
        Home directory of the new account

    shell
        Login shell of the new account

    unique
        If not True, the user account can have a non-unique UID

    system
        Create a system account

    fullname
        GECOS field for the full name

    roomnumber
        GECOS field for the room number

    workphone
        GECOS field for the work phone

    homephone
        GECOS field for the home phone

    other
        GECOS field for other information

    createhome
        Create the user's home directory

    loginclass
        Login class for the new account (OpenBSD)

    nologinit
        Do not add the user to the lastlog and faillog databases

    root
        Directory to chroot into

    usergroup
        Create and add the user to a new primary group of the same name

    CLI Example:

        salt '*' user.add name <uid> <gid> <groups> <home> <shell>
    

user.chfullname:

    Change the user's Full Name

    name
        User to modify

    fullname
        GECOS field for the full name

    root
        Directory to chroot into

    CLI Example:

        salt '*' user.chfullname foo "Foo Bar"
    

user.chgid:

    Change the default group of the user

    name
        User to modify

    gid
        Force use GID as new primary group

    root
        Directory to chroot into

    CLI Example:

        salt '*' user.chgid foo 4376
    

user.chgroups:

    Change the groups to which this user belongs

    name
        User to modify

    groups
        Groups to set for the user

    append : False
        If ``True``, append the specified group(s). Otherwise, this function
        will replace the user's groups with the specified group(s).

    root
        Directory to chroot into

    CLI Examples:

        salt '*' user.chgroups foo wheel,root
        salt '*' user.chgroups foo wheel,root append=True
    

user.chhome:

    Change the home directory of the user, pass True for persist to move files
    to the new home directory if the old home directory exist.

    name
        User to modify

    home
        New home directory for the user account

    persist
        Move contents of the home directory to the new location

    root
        Directory to chroot into

    CLI Example:

        salt '*' user.chhome foo /home/users/foo True
    

user.chhomephone:

    Change the user's Home Phone

    name
        User to modify

    homephone
        GECOS field for the home phone

    root
        Directory to chroot into

    CLI Example:

        salt '*' user.chhomephone foo 7735551234
    

user.chloginclass:

    Change the default login class of the user

    name
        User to modify

    loginclass
        Login class for the new account

    root
        Directory to chroot into

    Note:
        This function only applies to OpenBSD systems.

    CLI Example:

        salt '*' user.chloginclass foo staff
    

user.chother:

    Change the user's other GECOS attribute

    name
        User to modify

    other
        GECOS field for other information

    root
        Directory to chroot into

    CLI Example:

        salt '*' user.chother foobar
    

user.chroomnumber:

    Change the user's Room Number

    CLI Example:

        salt '*' user.chroomnumber foo 123
    

user.chshell:

    Change the default shell of the user

    name
        User to modify

    shell
        New login shell for the user account

    root
        Directory to chroot into

    CLI Example:

        salt '*' user.chshell foo /bin/zsh
    

user.chuid:

    Change the uid for a named user

    name
        User to modify

    uid
        New UID for the user account

    root
        Directory to chroot into

    CLI Example:

        salt '*' user.chuid foo 4376
    

user.chworkphone:

    Change the user's Work Phone

    name
        User to modify

    workphone
        GECOS field for the work phone

    root
        Directory to chroot into

    CLI Example:

        salt '*' user.chworkphone foo 7735550123
    

user.delete:

    Remove a user from the minion

    name
        Username to delete

    remove
        Remove home directory and mail spool

    force
        Force some actions that would fail otherwise

    root
        Directory to chroot into

    CLI Example:

        salt '*' user.delete name remove=True force=True
    

user.get_loginclass:

    Get the login class of the user

    name
        User to get the information

    Note:
        This function only applies to OpenBSD systems.

    CLI Example:

        salt '*' user.get_loginclass foo
    

user.getent:

    Return the list of all info for all users

    refresh
        Force a refresh of user information

    root
        Directory to chroot into

    CLI Example:

        salt '*' user.getent
    

user.info:

    Return user information

    name
        User to get the information

    root
        Directory to chroot into

    CLI Example:

        salt '*' user.info root
    

user.list_groups:

    Return a list of groups the named user belongs to

    name
        User to get the information

    CLI Example:

        salt '*' user.list_groups foo
    

user.list_users:

    Return a list of all users

    root
        Directory to chroot into

    CLI Example:

        salt '*' user.list_users
    

user.primary_group:

    Return the primary group of the named user

    New in version 2016.3.0

    name
        User to get the information

    CLI Example:

        salt '*' user.primary_group saltadmin
    

user.rename:

    Change the username for a named user

    name
        User to modify

    new_name
        New value of the login name

    root
        Directory to chroot into

    CLI Example:

        salt '*' user.rename name new_name
    

[root@master ~]# 

//SaltStack默认也支持一次执行多个Module,Module之间通过逗号隔开,默认传参之间也是用逗号分隔,也支持指定传参分隔符号--args-separator=@即可
[root@master ~]# salt 'node1' test.echo,cmd.run,service.status hello,hostname,salt-minion
node1:
    ----------
    cmd.run:
        node1
    service.status:
        True
    test.echo:
        hello

SaltStack常用模块之network

network.active_tcp

返回所有活动的tcp连接

[root@master ~]# salt 'node1' network.active_tcp
node1:
    ----------
    0:
        ----------
        local_addr:
            192.168.75.150
        local_port:
            34226
        remote_addr:
            192.168.75.128
        remote_port:
            4505

network.calc_net

通过IP和子网掩码计算出网段

[root@master ~]# salt 'node1' network.calc_net 192.168.75.255 255.255.255.0
node1:
    192.168.75.0/24
[root@master ~]# 

network.connect

测试minion至某一台服务器的网络是否连通

//ping www.biying.cn网站
[root@master ~]# salt 'node1' network.connect biying.cn 80
node1:
    ----------
    comment:
        Successfully connected to biying.cn (202.89.233.101) on tcp port 80
    result:
        True
[root@master ~]# 

network.default_route

查看默认路由

[root@master ~]# salt 'node1' network.default_route
node1:
    |_
      ----------
      addr_family:
          inet
      destination:
          0.0.0.0
      flags:
          UG
      gateway:
          192.168.75.2
      interface:
          ens33
      netmask:
          0.0.0.0
[root@master ~]# 

network.get_fqdn

查看主机的fqdn(完全限定域名)

[root@master ~]# salt '*' network.get_fqdn
master:
    master
node2:
    node2
node1:
    node1
node3:
    node3

network.get_hostname

获取主机名

[root@master ~]# salt '*' network.get_hostname
node1:
    node1
node3:
    node3
node2:
    node2
master:
    master

network.get_route

查询到一个目标网络的路由信息

[root@master ~]# salt 'node1' network.get_route 192.168.75.150
node1:
    ----------
    destination:
        192.168.75.150
    gateway:
        None
    interface:
        lo
    source:
        192.168.75.150

network.hw_addr

返回指定网卡的MAC地址

[root@master ~]# salt '*' network.hw_addr ens33
node1:
    00:0c:29:bf:1c:fe
node2:
    00:0c:29:37:f5:bc
node3:
    Interface "ens33" not in available interfaces: "lo", "ens160", "virbr0", "virbr0-nic"
master:
    Interface "ens33" not in available interfaces: "lo", "ens160", "virbr0", "virbr0-nic"
[root@master ~]# 

network.ifacestartswith

从特定CIDR检索接口名称

[root@master ~]# salt '*' network.ifacestartswith 192.168
node2:
    - ens33
node1:
    - ens33
node3:
    - ens160
    - virbr0
master:
    - ens160
    - virbr0

 network.in_subnet

判断当前主机是否在某一个网段内

[root@master ~]# salt 'node1' network.in_subnet 192.168.66.0/24
node1:
    False
[root@master ~]# salt 'node1' network.in_subnet 192.168.75.0/24
node1:
    True
[root@master ~]# 

network.interface

返回指定网卡的信息

[root@master ~]# salt '*' network.interface ens33
node2:
    |_
      ----------
      address:
          192.168.75.142
      broadcast:
          192.168.75.255
      label:
          ens33
      netmask:
          255.255.255.0
node1:
    |_
      ----------
      address:
          192.168.75.150
      broadcast:
          192.168.75.255
      label:
          ens33
      netmask:
          255.255.255.0
node3:
    Interface "ens33" not in available interfaces: "lo", "ens160", "virbr0", "virbr0-nic"
master:
    Interface "ens33" not in available interfaces: "lo", "ens160", "virbr0", "virbr0-nic"
[root@master ~]# 

network.interface_ip

返回指定网卡的IP地址

[root@master ~]# salt '*' network.interface_ip ens33
node1:
    192.168.75.150
node2:
    192.168.75.142
node3:
    Interface "ens33" not in available interfaces: "lo", "ens160", "virbr0", "virbr0-nic"
master:
    Interface "ens33" not in available interfaces: "lo", "ens160", "virbr0", "virbr0-nic"
[root@master ~]# 

network.interfaces

返回当前系统中所有的网卡信息

[root@master ~]# salt 'node1' network.interfaces
node1:
    ----------
    ens33:
        ----------
        hwaddr:
            00:0c:29:bf:1c:fe
        inet:
            |_
              ----------
              address:
                  192.168.75.150
              broadcast:
                  192.168.75.255
              label:
                  ens33
              netmask:
                  255.255.255.0
        inet6:
            |_
              ----------
              address:
                  fe80::5e93:d112:1456:d433
              prefixlen:
                  64
              scope:
                  link
        up:
            True
    lo:
        ----------
        hwaddr:
            00:00:00:00:00:00
        inet:
            |_
              ----------
              address:
                  127.0.0.1
              broadcast:
                  None
              label:
                  lo
              netmask:
                  255.0.0.0
        inet6:
            |_
              ----------
              address:
                  ::1
              prefixlen:
                  128
              scope:
                  host
        up:
            True
[root@master ~]# 

network.ip_addrs

返回一个IPv4的地址列表
该函数将会忽略掉127.0.0.1的地址

[root@master ~]# salt '*' network.ip_addrs
node2:
    - 192.168.75.142
node1:
    - 192.168.75.150
node3:
    - 192.168.75.143
    - 192.168.122.1
master:
    - 192.168.75.128
    - 192.168.122.1

network.netstat

返回所有打开的端口和状态

[root@master ~]# salt 'node1' network.netstat
node1:
    |_
      ----------
      inode:
          17757
      local-address:
          127.0.0.1:323
      program:
          chronyd
      proto:
          udp
      recv-q:
          0
      remote-address:
          *:*
      send-q:
          0
      user:
          0
    |_
      ----------
      inode:
          17926
      local-address:
          *:68
      program:
          dhclient
      proto:
          udp
      recv-q:
          0
      remote-address:
          *:*
      send-q:
          0
      user:
          0
    |_
      ----------
      inode:
          17758
      local-address:
          ::1:323
      program:
          chronyd
      proto:
          udp
      recv-q:
          0
      remote-address:
          :::*
      send-q:
          0
      user:
          0
    |_
      ----------
      inode:
          21627
      local-address:
          *:22
      program:
          sshd
      proto:
          tcp
      recv-q:
          0
      remote-address:
          *:*
      send-q:
          128
      state:
          LISTEN
      user:
          0
    |_
      ----------
      inode:
          22571
      local-address:
          127.0.0.1:25
      program:
          master
      proto:
          tcp
      recv-q:
          0
      remote-address:
          *:*
      send-q:
          100
      state:
          LISTEN
      user:
          0
    |_
      ----------
      inode:
          21322
      local-address:
          192.168.75.150:34226
      program:
          salt-minion
      proto:
          tcp
      recv-q:
          0
      remote-address:
          192.168.75.128:4505
      send-q:
          0
      state:
          ESTABLISHED
      user:
          0
    |_
      ----------
      inode:
          0
      local-address:
          192.168.75.150:54972
      program:
      proto:
          tcp
      recv-q:
          0
      remote-address:
          192.168.75.128:4506
      send-q:
          0
      state:
          TIME-WAIT
      user:
          0
    |_
      ----------
      inode:
          0
      local-address:
          192.168.75.150:54974
      program:
      proto:
          tcp
      recv-q:
          0
      remote-address:
          192.168.75.128:4506
      send-q:
          0
      state:
          TIME-WAIT
      user:
          0
    |_
      ----------
      inode:
          21629
      local-address:
          :::22
      program:
          sshd
      proto:
          tcp
      recv-q:
          0
      remote-address:
          :::*
      send-q:
          128
      state:
          LISTEN
      user:
          0
    |_
      ----------
      inode:
          22572
      local-address:
          ::1:25
      program:
          master
      proto:
          tcp
      recv-q:
          0
      remote-address:
          :::*
      send-q:
          100
      state:
          LISTEN
      user:
          0
[root@master ~]#

network.ping

使用ping命令测试到某主机的连通性

[root@master ~]# salt 'node1' network.ping biying.cn
node1:
    PING biying.cn (202.89.233.101) 56(84) bytes of data.
    64 bytes from 202.89.233.101 (202.89.233.101): icmp_seq=1 ttl=128 time=27.8 ms
    64 bytes from 202.89.233.101 (202.89.233.101): icmp_seq=2 ttl=128 time=160 ms
    64 bytes from 202.89.233.101 (202.89.233.101): icmp_seq=3 ttl=128 time=38.2 ms
    64 bytes from 202.89.233.101 (202.89.233.101): icmp_seq=4 ttl=128 time=50.2 ms
    
    --- biying.cn ping statistics ---
    4 packets transmitted, 4 received, 0% packet loss, time 3004ms
    rtt min/avg/max/mdev = 27.859/69.175/160.375/53.247 ms

SaltStack常用模块之service

service.available

判断指定的服务是否可用

[root@master ~]# salt '*' service.available sshd
node2:
    True
node3:
    True
node1:
    True
master:
    True
[root@master ~]# salt '*' service.available nginx
node3:
    False
node2:
    True
node1:
    False
master:
    False

service.get_all

获取所有正在运行的服务

[root@master ~]# salt 'node1' service.get_all
node1:
    - NetworkManager
    - NetworkManager-dispatcher
    - NetworkManager-wait-online
    - auditd
......

 service.disabled

检查指定服务是否开机不自动启动

[root@master ~]# salt 'node2' service.disabled nginx
node2:
    True
[root@master ~]# 

service.enabled

检查指定服务是否开机自动启动

[root@master ~]# salt 'node1' service.enable httpd
node1:
    True

service.reload

重新加载指定服务

[root@master ~]# salt 'node2' service.reload nginx
node2:
    True

service.stop

停止指定服务

[root@master ~]# salt 'node2' service.stop nginx
node2:
    True

service.start

启动指定服务

[root@master ~]# salt 'node2' service.start nginx
node2:
    True

service.restart

重启指定服务

[root@master ~]# salt 'node2' service.restart nginx
node2:
    True

service.status

查看指定服务的状态

[root@master ~]# salt 'node2' service.status nginx
node2:
    True

SaltStack常用模块之pkg

 pkg.download

只下载软件包但不安装
此功能将会下载指定的软件包,但是需要在minion端安装yum-utils,可以使用 cmd.run 进行远程安装

[root@master ~]# salt '*' pkg.download wget
master:
    ----------
    wget:
        /var/cache/yum/packages/wget-1.19.5-10.el8.x86_64.rpm
node2:
    ----------
    wget:
        /var/cache/yum/packages/wget-1.19.5-10.el8.x86_64.rpm
node3:
    ----------
    wget:
        /var/cache/yum/packages/wget-1.19.5-10.el8.x86_64.rpm
node1:
    ----------
    wget:
        /var/cache/yum/packages/wget-1.14-18.el7_6.1.x86_64.rpm

 pkg.file_list

列出指定包或系统中已安装的所有包的文件

[root@master ~]# salt 'node1' pkg.file_list httpd
node1:
    ----------
    errors:
    files:
        - /etc/httpd
        - /etc/httpd/conf
        - /etc/httpd/conf.d
        - /etc/httpd/conf.d/README
        - /etc/httpd/conf.d/autoindex.conf
        - /etc/httpd/conf.d/userdir.conf
        - /etc/httpd/conf.d/welcome.conf
        - /etc/httpd/conf.modules.d
        - /etc/httpd/conf.modules.d/00-base.conf
        - /etc/httpd/conf.modules.d/00-dav.conf
        - /etc/httpd/conf.modules.d/00-lua.conf
        - /etc/httpd/conf.modules.d/00-mpm.conf
        - /etc/httpd/conf.modules.d/00-proxy.conf
        - /etc/httpd/conf.modules.d/00-systemd.conf
        - /etc/httpd/conf.modules.d/01-cgi.conf
        - /etc/httpd/conf/httpd.conf
        - /etc/httpd/conf/magic
        - /etc/httpd/logs
......

//当不提供参数时,将会列出当前系统中所有已安装软件的文件列表
[root@master ~]# salt 'node1' pkg.file_list 
node1:
    ----------
    errors:
    files:
        VALUE_TRIMMED

pkg.group_info

查看包组的信息

[root@master ~]# salt 'node1' pkg.group_info 'Development Tools'
node1:
    ----------
    conditional:
    default:
        - byacc
        - cscope
        - ctags
        - diffstat
        - doxygen
        - elfutils
        - gcc-gfortran
        - git
        - indent
        - intltool
        - patchutils
        - rcs
        - subversion
        - swig
        - systemtap
    description:
        A basic development environment.
    group:
        Development Tools
    id:
        development
    mandatory:
        - autoconf
        - automake
        - binutils
        - bison
        - flex
        - gcc
        - gcc-c++
        - gettext
        - libtool
        - make
        - patch
        - pkgconfig
        - redhat-rpm-config
        - rpm-build
        - rpm-sign
    optional:
        - ElectricFence
        - ant
        - babel
        - bzr
        - ccache
        - chrpath
        - clips
        - clips-devel
        - clips-doc
        - clips-emacs
        - clips-xclips
        - clipsmm-devel
        - clipsmm-doc
        - cmake
        - cmucl
        - colordiff
        - compat-gcc-44
        - compat-gcc-44-c++
        - cvs
        - cvsps
        - darcs
        - dejagnu
        - email2trac
        - expect
        - ftnchek
        - gcc-gnat
        - gcc-objc
        - gcc-objc++
        - ghc
        - git
        - haskell-platform
        - imake
        - javapackages-tools
        - ksc
        - lua
        - mercurial
        - mock
        - mod_dav_svn
        - nasm
        - nqc
        - nqc-doc
        - ocaml
        - perltidy
        - qgit
        - rpmdevtools
        - rpmlint
        - sbcl
        - scorep
        - systemtap-sdt-devel
        - systemtap-server
        - trac
        - trac-git-plugin
        - trac-mercurial-plugin
        - trac-webadmin
        - translate-toolkit
    type:
        package group
[root@master ~]# 

pkg.group_list

列出系统中所有的包组

[root@master ~]# salt 'node1' pkg.group_list
node1:
    ----------
    available:
        - Additional Development
        - Anaconda Tools
        - Backup Client
        - Backup Server
        - Base
        - Buildsystem building group
        - CentOS Linux Client product core
        - CentOS Linux ComputeNode product core
        - CentOS Linux Server product core
        - CentOS Linux Workstation product core
        - Cinnamon
        - Common NetworkManager submodules
        - Compatibility Libraries
        - Conflicts (Client)
        - Conflicts (ComputeNode)
        - Conflicts (Server)
        - Conflicts (Workstation)
        - Console Internet Tools
        - Core
        - DNS Name Server
        - Debugging Tools
        - Desktop Debugging and Performance Tools
        - Development Tools
        - Dial-up Networking Support
        - Directory Client
        - Directory Server
        - E-mail Server
        - Educational Software
        - Electronic Lab
        - Emacs
        - FTP Server
        - Fedora Packager
        - File and Storage Server
        - Fonts
        - GNOME
        - GNOME Applications
        - General Purpose Desktop
        - Graphical Administration Tools
        - Graphics Creation Tools
        - Guest Agents
        - Guest Desktop Agents
        - Hardware Monitoring Utilities
        - Haskell
        - High Availability
        - Hyper-v platform specific packages
        - Identity Management Server
        - Infiniband Support
        - Input Methods
        - Internet Applications
        - Internet Browser
        - Java Platform
        - KDE
        - KDE Applications
        - KDE Multimedia Support
        - KVM platform specific packages
        - LXQt Desktop
        - Large Systems Performance
        - Legacy UNIX Compatibility
        - Legacy X Window System Compatibility
        - Load Balancer
        - MATE
        - Mainframe Access
        - MariaDB Database Client
        - MariaDB Database Server
        - Milkymist
        - Multimedia
        - Network File System Client
        - Network Infrastructure Server
        - Networking Tools
        - Office Suite and Productivity
        - PHP Support
        - Performance Tools
        - Perl Support
        - Perl for Web
        - Platform Development
        - PostgreSQL Database Client
        - PostgreSQL Database Server
        - Print Server
        - Printing Client
        - Python
        - Remote Desktop Clients
        - Remote Management for Linux
        - Resilient Storage
        - Ruby Support
        - Scientific Support
        - Security Tools
        - Smart Card Support
        - System Administration Tools
        - System Management
        - Technical Writing
        - TurboGears application framework
        - VMware platform specific packages
        - Virtualization Client
        - Virtualization Hypervisor
        - Virtualization Platform
        - Virtualization Tools
        - Web Server
        - Web Servlet Engine
        - X Window System
        - Xfce
    available environments:
        - Minimal Install
        - Compute Node
        - Infrastructure Server
        - File and Print Server
        - Cinnamon Desktop
        - MATE Desktop
        - Basic Web Server
        - Virtualization Host
        - Server with GUI
        - GNOME Desktop
        - KDE Plasma Workspaces
        - Development and Creative Workstation
    available languages:
        ----------
    installed:
    installed environments:
[root@master ~]# 

pkg.list_downloaded

列出已下载到本地的软件包

[root@master ~]# salt 'node1' pkg.list_downloaded
node1:
    ----------
    wget:
        ----------
        1.14-18.el7_6.1:
            ----------
            creation_date_time:
                2021-11-03T15:09:58
            creation_date_time_t:
                1635923398
            path:
                /var/cache/yum/packages/wget-1.14-18.el7_6.1.x86_64.rpm
            size:
                560272
[root@master ~]# 

 pkg.install

安装软件

[root@master ~]# salt 'node3' pkg.install nginx
node3:
    ----------
    nginx:
        ----------
        new:
            1:1.14.1-9.module_el8.0.0+184+e34fea82
        old:
    nginx-all-modules:
        ----------
        new:
            1:1.14.1-9.module_el8.0.0+184+e34fea82
        old:
    nginx-filesystem:
        ----------
        new:
            1:1.14.1-9.module_el8.0.0+184+e34fea82
        old:
    nginx-mod-http-image-filter:
        ----------
        new:
            1:1.14.1-9.module_el8.0.0+184+e34fea82
        old:
    nginx-mod-http-perl:
        ----------
        new:
            1:1.14.1-9.module_el8.0.0+184+e34fea82
        old:
    nginx-mod-http-xslt-filter:
        ----------
        new:
            1:1.14.1-9.module_el8.0.0+184+e34fea82
        old:
    nginx-mod-mail:
        ----------
        new:
            1:1.14.1-9.module_el8.0.0+184+e34fea82
        old:
    nginx-mod-stream:
        ----------
        new:
            1:1.14.1-9.module_el8.0.0+184+e34fea82
        old:
[root@master ~]# 

 pkg.list_pkgs

以字典的方式列出当前已安装的软件包

[root@master ~]# salt 'node1' pkg.list_pkgs
node1:
    ----------
    GeoIP:
        1.5.0-11.el7
    NetworkManager:
        1:1.10.2-13.el7
    NetworkManager-libnm:
        1:1.10.2-13.el7
    NetworkManager-team:
        1:1.10.2-13.el7
    NetworkManager-tui:
        1:1.10.2-13.el7
    acl:
......

pkg.owner

列出指定文件是由哪个包提供的

[root@master ~]# salt 'node2' pkg.owner /usr/sbin/nginx
node2:
    nginx

 pkg.remove

卸载指定软件

[root@master ~]# salt 'node1' pkg.remove httpd
node1:
    ----------
    httpd:
        ----------
        new:
        old:
            2.4.6-97.el7.centos.1
//若要卸载多个文件,中间需要用逗号隔开

pkg.upgrade

升级系统中所有的软件包或升级指定的软件包

[root@master ~]# salt 'node1' pkg.upgrade name=openssl
node1:
    ----------
    openssl:
        ----------
        new:
            1:1.0.2k-22.el7_9
        old:
            1:1.0.2k-12.el7
    openssl-libs:
        ----------
        new:
            1:1.0.2k-22.el7_9
        old:
            1:1.0.2k-12.el7

SaltStack常用模块之state

 state.show_highstate

显示当前系统中有哪些高级状态

[root@master ~]# salt '*' state.show_highstate
node1:
    ----------
    mysql-install:
        ----------
        __env__:
            base
        __sls__:
            database.mariadb.install
        pkg:
            |_
              ----------
              name:
                  mariadb-server
            - installed
            |_
              ----------
              order:
                  10000
    mysql-service:
        ----------
        __env__:
            base
        __sls__:
            database.mariadb.install
        service:
            |_
              ----------
              name:
                  mariadb
            |_
              ----------
              enable:
                  ture
            - running
            |_
              ----------
              order:
                  10001
node3:
    ----------
    mysql-install:
        ----------
        __env__:
            base
        __sls__:
            database.mariadb.install
        pkg:
            |_
              ----------
              name:
                  mariadb-server
            - installed
            |_
              ----------
              order:
                  10000
    mysql-service:
        ----------
        __env__:
            base
        __sls__:
            database.mariadb.install
        service:
            |_
              ----------
              name:
                  mariadb
            |_
              ----------
              enable:
                  ture
            - running
            |_
              ----------
              order:
                  10001
master:
    ----------
    mysql-install:
        ----------
        __env__:
            base
        __sls__:
            database.mariadb.install
        pkg:
            |_
              ----------
              name:
                  mariadb-server
            - installed
            |_
              ----------
              order:
                  10000
    mysql-service:
        ----------
        __env__:
            base
        __sls__:
            database.mariadb.install
        service:
            |_
              ----------
              name:
                  mariadb
            |_
              ----------
              enable:
                  ture
            - running
            |_
              ----------
              order:
                  10001
node2:
    ----------
    mysql-install:
        ----------
        __env__:
            base
        __sls__:
            database.mariadb.install
        pkg:
            |_
              ----------
              name:
                  mariadb-server
            - installed
            |_
              ----------
              order:
                  10000
    mysql-service:
        ----------
        __env__:
            base
        __sls__:
            database.mariadb.install
        service:
            |_
              ----------
              name:
                  mariadb
            |_
              ----------
              enable:
                  ture
            - running
            |_
              ----------
              order:
                  10001
[root@master ~]# 

 state.show_state_usage

显示当前系统中的高级状态执行情况

[root@master ~]# salt 'node1' state.show_state_usage
node1:
    ----------
    base:
        ----------
        count_all:
            2
        count_unused:
            1
        count_used:
            1
        unused:
            - top
        used:
            - web.apache.install
    test:
        ----------
        count_all:
            0
        count_unused:
            0
        count_used:
            0
        unused:
        used:

state.show_top

返回minion将用于highstate的顶级数据

[root@master ~]# salt 'node1' state.show_top
node1:
    ----------
    base:
        - web.apache.apache

state.top

执行指定的top file,而不是默认的

[root@master ~]# salt 'node1' state.show_sls web.apache.install
node1:
    ----------
    apache-install:
        ----------
        __env__:
            base
        __sls__:
            web.apache.install
        pkg:
            |_
              ----------
              name:
                  httpd
            - installed
            |_
              ----------
              order:
                  10000
    apache-service:
        ----------
        __env__:
            base
        __sls__:
            web.apache.install
        service:
            |_
              ----------
              name:
                  httpd
            |_
              ----------
              enable:
                  True
            - running
            |_
              ----------
              order:
                  10001

state.show_sls

显示 master 上特定sls或sls文件列表中的状态数据

[root@master salt]# salt 'node1' state.show_sls web.apache.install
node1:
    ----------
    apache-install:
        ----------
        __env__:
            base
        __sls__:
            web.apache.install
        pkg:
            |_
              ----------
              name:
                  httpd
            - installed
            |_
              ----------
              order:
                  10000
    apache-service:
        ----------
        __env__:
            base
        __sls__:
            web.apache.install
        service:
            |_
              ----------
              name:
                  httpd
            |_
              ----------
              enable:
                  ture
            - running
            |_
              ----------
              order:
                  10001
[root@master salt]# 

SaltStack常用模块之user

user.add

创建用户

[root@master ~]# salt 'node1' user.add yang
node1:
    True
[root@master ~]# salt 'node1' cmd.run 'id yang'
node1:
    uid=1001(yang) gid=1001(yang) groups=1001(yang)

user.chuid

更改用户uid

[root@master ~]# salt 'node1' user.chuid yang 666
node1:
    True
[root@master ~]# salt 'node1' cmd.run 'id yang'
node1:
    uid=666(yang) gid=1001(yang) groups=1001(yang)

user.chgid

更改用户gid

[root@master ~]# salt 'node1' user.chgid yang 888
node1:
    True
[root@master ~]# salt 'node1' cmd.run 'id yang'
node1:
    uid=666(yang) gid=1001(yang) groups=888(yang)

user.chgroups

更改用户属组,追加指定用户组

[root@master ~]# salt 'node1' user.chgroups yang root true
node1:
    True
[root@master ~]# salt 'node1' cmd.run 'id yang'
node1:
    uid=666(yang) gid=1001(yang) groups=888(yang),0(root)

user.delete

删除用户

[root@master ~]# salt 'node1' user.delete yang remove=True force=True
node1:
    True
[root@master ~]# salt 'node1' cmd.run 'id yang'
node1:
    id: yang: no such user
ERROR: Minions returned with non-zero exit code

file模块

file.access

检查指定路径是否存在

[root@master ~]# salt 'node2' file.access /etc/selinux/config f
node2:
    True

检查指定文件的权限信息

[root@master ~]# salt 'node2' cmd.run 'ls -l /etc/selinux/config'
node2:
    -rw-r--r-- 1 root root 547 Nov  2 14:52 /etc/selinux/config
[root@master ~]# salt 'node2' file.access /etc/selinux/config r    //检查有无读权限
node2:
    True
[root@master ~]# salt 'node2' file.access /etc/selinux/config w    //写权限
node2:
    True
[root@master ~]# salt 'node2' file.access /etc/selinux/config x    //执行权限
node2:
    False

file.append

往一个文件里追加内容,若此文件不存在则会报异常

[root@master ~]# salt 'node2' cmd.run 'cat /opt/niuwa'
node2:
[root@master ~]# salt 'node2' file.append /opt/niuwa "EDG加油!"
node2:
    Wrote 1 lines to "/opt/niuwa"
[root@master ~]# salt 'node2' cmd.run 'cat /opt/niuwa'node2:
    EDG加油!
[root@master ~]# 

file.check_hash

检查指定的文件与hash字符串是否匹配,匹配则返回 True 否则返回 False

[root@master ~]# salt 'node2' cmd.run 'md5sum /etc/passwd'
node2:
    fb377afded88dbf0df947a77e0ccaa5d  /etc/passwd
[root@master ~]# salt 'node2' file.check_hash /etc/passwd fb377afded88dbf0df947a77e0ccaa5d
node2:
    True

file.chattr

修改指定文件的属性

属性对文件的意义对目录的意义
a只允许在这个文件之后追加数据,
不允许任何进程覆盖或截断这个文件
只允许在这个目录下建立和修改文件,
而不允许删除任何文件
i不允许对这个文件进行任何的修改,
不能删除、更改、移动
任何的进程只能修改目录之下的文件,
不允许建立和删除文件

给指定文件添加属性

[root@master ~]# salt 'node2' file.chattr /opt/test.sh operator=add attributes=ai
node2:
    True
[root@master ~]# salt 'node2' cmd.run 'lsattr /opt/test.sh'
node2:
    ----ia-------------- /opt/test.sh

//不能覆盖,删除,只能追加
[root@node2 ~]# echo wuhu < /opt/test.sh
wuhu
[root@node2 ~]# rm /opt/test.sh
rm:是否删除普通文件 '/opt/test.sh'?y
rm: 无法删除'/opt/test.sh': 不允许的操作

给指定文件去除属性

[root@master ~]# salt 'node2' file.chattr /opt/test.sh operator=add attributes=i
node2:
    True
[root@master ~]# salt 'node2' cmd.run 'lsattr /opt/test.sh'
node2:
    ----i--------------- /opt/test.sh

file.copy

在远程主机上复制文件或目录

拷贝文件

[root@master ~]# salt '*' file.copy /opt/ /root/opt recurse=True
node2:
    True

[root@master ~]# salt 'node2' cmd.run 'ls  /root'node2:
    1
    anaconda-ks.cfg
    opt
    pymp-7gf64n0c
    pymp-caei6nbu
    pymp-dta32xl9
    pymp-ma28jywj
    systemd-private-0ae4d59f8418415bb1b87132a17d64d6-chronyd.service-YHDD7e
    systemd-private-0ae4d59f8418415bb1b87132a17d64d6-nginx.service-aqEWdf
    vmware-root_1004-2957649132
    vmware-root_1010-2957124853
    vmware-root_1013-4290232108
    vmware-root_1030-2965579130
    vmware-root_984-2999526209
    vmware-root_992-2999657319
    vmware-root_995-4257069371
    yang.txt
    !
[root@master ~]#

删除目标目录中同名的文件或目录并拷贝新内容至其中
 

[root@master ~]# salt 'node2' cmd.run 'ls -l /opt/'
node2:
    total 4
    -rw-r--r-- 1 root root  0 Nov  4 10:59 axiba
    -rw-r--r-- 1 root root 18 Nov  4 10:12 test.sh
    -rw-r--r-- 1 root root  0 Nov  4 10:47 yang.txt
[root@master ~]# salt 'node2' cmd.run 'ls -l /root/opt/'
node2:
    total 4
    -rw-r--r-- 1 root root 18 Nov  4 10:12 test.sh
    -rw-r--r-- 1 root root  0 Nov  4 10:47 yang.txt
[root@master ~]# salt 'node2' file.copy /opt/ /root/opt recurse=True remove_existing=True
node2:
    True
[root@master ~]# salt 'node2' cmd.run 'ls -l /root/opt/'
node2:
    total 4
    -rw-r--r-- 1 root root  0 Nov  4 10:59 axiba
    -rw-r--r-- 1 root root 18 Nov  4 10:12 test.sh
    -rw-r--r-- 1 root root  0 Nov  4 10:47 yang.txt

file.ditectory_exists

判断指定目录是否存在,存在则返回 True ,否则返回 False

[root@master ~]# salt 'node2' file.directory_exists /opt/
node2:
    True

判断指定文件是否存在
 

root@master ~]# salt 'node2' cmd.run 'ls -l /opt/'
node2:
    total 4
    -rw-r--r-- 1 root root  0 Nov  4 10:59 axiba
    -rw-r--r-- 1 root root 18 Nov  4 10:12 test.sh
    drwxr-xr-x 2 root root  6 Nov  4 11:08 test_dir
    -rw-r--r-- 1 root root  0 Nov  4 10:47 yang.txt
[root@master ~]# salt 'node2' file.file_exists /opt/yang.txt
node2:
    True
[root@master ~]# salt 'node2' file.file_exists /opt/test_dir
node2:
    False  //返回False是因为abc是目录而非文件

file.get_gid

获取指定文件的gid

[root@master ~]# salt 'node2' cmd.run 'ls -l /opt/'
node2:
    total 4
    -rw-r--r-- 1 root root  0 Nov  4 10:59 axiba
    -rw-r--r-- 1 root root 18 Nov  4 10:12 test.sh
    drwxr-xr-x 2 root root  6 Nov  4 11:08 test_dir
    -rw-r--r-- 1 root root  0 Nov  4 10:47 yang.txt
[root@master ~]# salt 'node2' file.get_gid /optde2:
    0
[root@master ~]#

file.get_group

获取指定文件的组名

[root@master ~]# salt 'node2' file.get_group /opt/ 
node2:
    root

file.get_hash

获取指定文件的hash值,该值通过 sha256 算法得来

[root@master ~]# salt 'node2' cmd.run 'sha256sum /opt/yang.txt'
node2:
    e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855  /opt/yang.txt
[root@master ~]# salt 'node2' file.get_hash /opt/yang.txt
node2:
    e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855

file.get_mode

获取指定文件的权限,以数字方式显示

[root@master ~]# salt 'node2' cmd.run 'ls -l /opt/'
node2:
    total 4
    -rw-r--r-- 1 root root  0 Nov  4 10:59 axiba
    -rw-r--r-- 1 root root 18 Nov  4 10:12 test.sh
    drwxr-xr-x 2 root root  6 Nov  4 11:08 test_dir
    -rw-r--r-- 1 root root  0 Nov  4 10:47 yang.txt
[root@master ~]# salt 'node2' file.get_mode /opt/test_dir
node2:
    0755

file.get_selinux_context

获取指定文件的 SELINUX 上下文信息

[root@master ~]# salt 'node2' file.get_selinux_context /etc/selinux
node2:
    system_u:object_r:selinux_config_t:s0

file.get_sum

按照指定的算法计算指定文件的特征码并显示,默认使用的sha256算法。
该函数可使用的算法参数有:

  • md5
  • sha1
  • sha224
  • sha256 (default)
  • sha384
  • sha512
[root@master ~]# salt 'node2' file.get_sum /opt/yang.txt
node2:
    e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
[root@master ~]# salt 'node2' file.get_sum /opt/yang.txt md5
node2:
    d41d8cd98f00b204e9800998ecf8427e

file.get_uid与file.get_user

获取指定文件的 uid 或 用户名

[root@master ~]# salt 'node2' cmd.run 'ls -l /opt/'
node2:
    total 4
    -rw-r--r-- 1 root root  0 Nov  4 10:59 axiba
    -rw-r--r-- 1 root root 18 Nov  4 10:12 test.sh
    drwxr-xr-x 2 root root 18 Nov  4 11:26 test_dir
    -rw-r--r-- 1 root root  0 Nov  4 10:47 yang.txt
[root@master ~]# salt 'node2' file.get_uid /opt/yang.txt
node2:
    0
[root@master ~]# salt 'node2' file.get_user /opt/yang.txt
node2:
    root

 file.gid_to_group

将指定的 gid 转换为组名并显示

[root@master ~]# salt 'node2' cmd.run 'id yang'node2:
    uid=1001(yang) gid=1001(yang) groups=1001(yang)
[root@master ~]# salt 'node2' file.gid_to_group 1001
node2:
    yang
[root@master ~]# salt 'node2' file.gid_to_group 0
node2:
    root

file.group_to_gid

将指定的组名转换为 gid 并显示

[root@master ~]# salt 'node2' file.group_to_gid yang
node2:
    1001
[root@master ~]# salt 'node2' file.group_to_gid root
node2:
    0

file.grep

在指定文件中检索指定内容
该函数支持通配符,若在指定的路径中用通配符则必须用双引号引起来

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

file.is_blkdev

判断指定的文件是否是块设备文件

[root@master ~]# salt 'node2' cmd.run 'ls -l /dev/sr0'
node2:
    brw-rw---- 1 root cdrom 11, 0 Nov  4 10:07 /dev/sr0
[root@master ~]# salt 'node2' file.is_blkdev /dev/sr0
node2:
    True

 file.lsattr

检查并显示出指定文件的属性信息

[root@master ~]# salt 'node2' file.lsattr /opt/yang.txt
node2:
    ----------
    /opt/yang.txt:

file.mkdir

创建目录并设置属主、属组及权限

[root@master ~]# salt 'node2' cmd.run 'ls -l /opt'
node2:
    total 4
    -rw-r--r-- 1 root root  0 Nov  4 10:59 axiba
    -rw-r--r-- 1 root root 18 Nov  4 10:12 test.sh
    drwxr-xr-x 2 root root 18 Nov  4 11:26 test_dir
    -rw-r--r-- 1 root root  0 Nov  4 10:47 yang.txt
[root@master ~]# salt 'node2' file.mkdir /opt/a
node2:
    True
[root@master ~]# salt 'node2' cmd.run 'ls -l /opt'
node2:
    total 4
    drwxr-xr-x 2 root root  6 Nov  4 11:49 a
    -rw-r--r-- 1 root root  0 Nov  4 10:59 axiba
    -rw-r--r-- 1 root root 18 Nov  4 10:12 test.sh
    drwxr-xr-x 2 root root 18 Nov  4 11:26 test_dir
    -rw-r--r-- 1 root root  0 Nov  4 10:47 yang.txt
[root@master ~]# salt 'node2' file.mkdir /opt/b yang yang 650
node2:
    True
[root@master ~]# salt 'node2' cmd.run 'ls -l /opt'
node2:
    total 4
    drwxr-xr-x 2 root root  6 Nov  4 11:49 a
    -rw-r--r-- 1 root root  0 Nov  4 10:59 axiba
    drw-r-x--- 2 yang yang  6 Nov  4 11:50 b
    -rw-r--r-- 1 root root 18 Nov  4 10:12 test.sh
    drwxr-xr-x 2 root root 18 Nov  4 11:26 test_dir
    -rw-r--r-- 1 root root  0 Nov  4 10:47 yang.txt

file.move

移动或重命名

//改名
[root@master ~]# salt 'node2' cmd.run 'ls -l /opt'
node2:
    total 4
    -rw-r--r-- 1 root root  0 Nov  4 10:59 axiba
    -rw-r--r-- 1 root root 18 Nov  4 10:12 test.sh
    drwxr-xr-x 2 root root 18 Nov  4 11:26 test_dir
    -rw-r--r-- 1 root root  0 Nov  4 10:47 yang.txt
[root@master ~]# salt 'node2' file.mkdir /opt/a
[root@master ~]# salt 'node2' cmd.run 'ls -l /opt'
node2:
    total 4
    drwxr-xr-x 2 root root  6 Nov  4 11:49 a
    -rw-r--r-- 1 root root  0 Nov  4 10:59 axiba
    drw-r-x--- 2 yang yang  6 Nov  4 11:50 b
    -rw-r--r-- 1 root root 18 Nov  4 10:12 test.sh
    drwxr-xr-x 2 root root 18 Nov  4 11:26 test_dir
    -rw-r--r-- 1 root root  0 Nov  4 10:47 yang.txt
[root@master ~]# salt 'node2' file.move /opt/a /opt/aa
node2:
    ----------
    comment:
        '/opt/a' moved to '/opt/aa'
    result:
        True
[root@master ~]# salt 'node2' cmd.run 'ls -l /opt'
node2:
    total 4
    drwxr-xr-x 2 root root  6 Nov  4 11:49 aa
    -rw-r--r-- 1 root root  0 Nov  4 10:59 axiba
    drw-r-x--- 2 yang yang  6 Nov  4 11:50 b
    -rw-r--r-- 1 root root 18 Nov  4 10:12 test.sh
    drwxr-xr-x 2 root root 18 Nov  4 11:26 test_dir
    -rw-r--r-- 1 root root  0 Nov  4 10:47 yang.txt
[root@master ~]# 

//移动
[root@master ~]# salt 'node2' file.move /opt/aa /opt/b/
node2:
    ----------
    comment:
        '/opt/aa' moved to '/opt/b/'
    result:
        True
[root@master ~]# salt 'node2' cmd.run 'ls -l /opt/b/'
node2:
    total 0
    drwxr-xr-x 2 root root 6 Nov  4 11:49 aa

file.prepend

把文本插入指定文件的开头

[root@master ~]# salt 'node2' cmd.run 'cat /opt/yang.txt'
node2:
    大漠孤烟直
    长河落日圆
    
    wuhu
    
    天生我才必有用
[root@master ~]# salt 'node2' file.prepend /opt/yang.txt " 君不见,高堂明镜悲白发,朝如青丝暮成雪"
node2:
    Prepended 1 lines to "/opt/yang.txt"
[root@master ~]# salt 'node2' cmd.run 'cat /opt/yang.txt'node2:
    君不见,高堂明镜悲白发,朝如青丝暮成雪
    大漠孤烟直
    长河落日圆
    
    wuhu
    
    天生我才必有用

 file.sed

修改文本文件的内容

[root@master ~]# salt 'node2' cmd.run 'cat /opt/yang.txt'node2:
    君不见,高堂明镜悲白发,朝如青丝暮成雪
    大漠孤烟直
    长河落日圆
    
    wuhu
    
    天生我才必有用
[root@master ~]# salt 'node2' file.sed /opt/yang.txt 'wuhu' 'xinei!'
node2:
    ----------
    pid:
        195345
    retcode:
        0
    stderr:
    stdout:
[root@master ~]# salt 'node2' cmd.run 'cat /opt/yang.txt'node2:
    君不见,高堂明镜悲白发,朝如青丝暮成雪
    大漠孤烟直
    长河落日圆
    
    xinei!
    
    天生我才必有用

 file.read

读取文件内容

[root@master ~]# salt 'node2' file.read /opt/yang.txt
node2:
    君不见,高堂明镜悲白发,朝如青丝暮成雪
    大漠孤烟直
    长河落日圆
    
    xinei!
    
    天生我才必有用

file.readdir

列出指定目录下的所有文件或目录,包括隐藏文件

[root@master ~]# salt 'node2' file.readdir /root/
node2:
    - .
    - ..
    - .bash_logout
    - .bash_profile
    - .bashrc
    - .cshrc
    - .tcshrc
    - anaconda-ks.cfg
    - .config
    - 1
    - .bash_history
    - !
    - yang.txt
    - vmware-root_1010-2957124853
    - vmware-root_1030-2965579130
    - .X11-unix
    - .ICE-unix
    - .XIM-unix
    - .font-unix
    - .Test-unix
    - vmware-root_984-2999526209
    - vmware-root_992-2999657319
    - pymp-caei6nbu
    - vmware-root_1013-4290232108
    - pymp-ma28jywj
    - vmware-root_995-4257069371
    - pymp-dta32xl9
    - systemd-private-0ae4d59f8418415bb1b87132a17d64d6-chronyd.service-YHDD7e
    - vmware-root_1004-2957649132
    - systemd-private-0ae4d59f8418415bb1b87132a17d64d6-nginx.service-aqEWdf
    - pymp-7gf64n0c
    - opt
    - .viminfo
[root@master ~]# 

file.remove

删除指定的文件或目录,若给出的是目录,将递归删除

[root@master ~]# salt 'node2' cmd.run 'ls -l /opt/'
node2:
    total 12
    -rw-r--r-- 1 root root   0 Nov  4 10:59 axiba
    drw-r-x--- 3 yang yang  16 Nov  4 11:56 b
    -rw-r--r-- 1 root root  18 Nov  4 10:12 test.sh
    drwxr-xr-x 2 root root  18 Nov  4 11:26 test_dir
    -rw-r--r-- 1 root root 121 Nov  4 12:02 yang.txt
    -rw-r--r-- 1 root root 119 Nov  4 11:59 yang.txt.bak
[root@master ~]# salt 'node2' file.remove /opt/test_dir
node2:
    True
[root@master ~]# salt 'node2' cmd.run 'ls -l /opt/'
node2:
    total 12
    -rw-r--r-- 1 root root   0 Nov  4 10:59 axiba
    drw-r-x--- 3 yang yang  16 Nov  4 11:56 b
    -rw-r--r-- 1 root root  18 Nov  4 10:12 test.sh
    -rw-r--r-- 1 root root 121 Nov  4 12:02 yang.txt
    -rw-r--r-- 1 root root 119 Nov  4 11:59 yang.txt.bak
[root@master ~]# salt 'node2' file.remove /opt/a
node2:
    False
[root@master ~]# salt 'node2' file.remove /opt/b
node2:
    True
[root@master ~]# salt 'node2' cmd.run 'ls -l /opt/'
node2:
    total 12
    -rw-r--r-- 1 root root   0 Nov  4 10:59 axiba
    -rw-r--r-- 1 root root  18 Nov  4 10:12 test.sh
    -rw-r--r-- 1 root root 121 Nov  4 12:02 yang.txt
    -rw-r--r-- 1 root root 119 Nov  4 11:59 yang.txt.bak
[root@master ~]# 

file.rename

重命名文件或目录

[root@master ~]# salt 'node2' cmd.run 'ls -l /opt/'
node2:
    total 12
    -rw-r--r-- 1 root root   0 Nov  4 10:59 axiba
    -rw-r--r-- 1 root root  18 Nov  4 10:12 test.sh
    -rw-r--r-- 1 root root 121 Nov  4 12:02 yang.txt
    -rw-r--r-- 1 root root 119 Nov  4 11:59 yang.txt.bak
[root@master ~]# salt 'node2' file.rename /opt/axiba /opt/niuwa
node2:
    True
[root@master ~]# salt 'node2' cmd.run 'ls -l /opt/'
node2:
    total 12
    -rw-r--r-- 1 root root   0 Nov  4 10:59 niuwa
    -rw-r--r-- 1 root root  18 Nov  4 10:12 test.sh
    -rw-r--r-- 1 root root 121 Nov  4 12:02 yang.txt
    -rw-r--r-- 1 root root 119 Nov  4 11:59 yang.txt.bak

 file.set_mode

给指定文件设置权限

[root@master ~]# salt 'node2' cmd.run 'ls -l /opt/yang.txt'
node2:
    -rw-r--r-- 1 root root 121 Nov  4 12:02 /opt/yang.txt
[root@master ~]# salt 'node2' file.set_mode /opt/yang.txt 2700
node2:
    2700
[root@master ~]# salt 'node2' cmd.run 'ls -l /opt/yang.txt'
node2:
    -rwx--S--- 1 root root 121 Nov  4 12:02 /opt/yang.txt

给指定的文件创建软链接

[root@master ~]# salt 'node2' cmd.run 'tree /opt/'
node2:
    /opt/
    |-- a
    |   `-- b
    |       `-- c
    |-- niuwa
    |-- test.sh
    |-- yang.txt
    `-- yang.txt.bak
    
    2 directories, 5 files
[root@master ~]# salt 'node2' file.symlink /opt/a/b/c /opt/c
node2:
    True
[root@master ~]# salt 'node2' cmd.run 'ls -l /opt/'node2:
    total 12
    drwxr-xr-x 3 root root  15 Nov  4 15:32 a
    lrwxrwxrwx 1 root root  10 Nov  4 15:33 c -> /opt/a/b/c
    -rw-r--r-- 1 root root   0 Nov  4 10:59 niuwa
    -rw-r--r-- 1 root root  18 Nov  4 10:12 test.sh
    -rwx--S--- 1 root root 121 Nov  4 12:02 yang.txt
    -rw-r--r-- 1 root root 119 Nov  4 11:59 yang.txt.bak
[root@master ~]# salt 'node2' cmd.run 'cat /opt/c'
node2:
    test saltstack file moudle create link

file.write

往一个指定的文件里覆盖写入指定内容

[root@master ~]# salt 'node2' cmd.run 'cat /opt/yang.txt'
node2:
    君不见,高堂明镜悲白发,朝如青丝暮成雪
    大漠孤烟直
    长河落日圆
    
    xinei!
    
    天生我才必有用
[root@master ~]# salt 'node2' file.write /opt/yang.txt "this is test saltstack file moudle file.write" "EDG冲啊!"
node2:
    Wrote 2 lines to "/opt/yang.txt"
[root@master ~]# salt 'node2' cmd.run 'cat /opt/yang.txt'node2:
    this is test saltstack file moudle file.write
    EDG冲啊!
[root@master ~]# 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值