SaltStack常用的模块

因为模块太多了,我就不一一列出,想看更多的就点击链接去官方文档去学习
官方文档:https://docs.saltproject.io/en/latest/ref/modules/all/index.html

常用模块

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

#查看所有module列表
[root@master ~]# salt 'master' sys.list_modules
master:
    - acl
    - aliases
    - alternatives
    - archive
    - artifactory
    - baredoc
    - beacons
    - bigip
    - btrfs
    - buildout
    - chroot
    - cloud
    - cmd
    - composer
    - config
    - consul
    - container_resource
    - cp
    - cron
    - cryptdev
    - data
    - defaults
    - devinfo
    - devmap
    - disk
    - django
    - dnsmasq
    - dnsutil
    - drbd
    - environ
    - ethtool
    - event
    - extfs
    - file
    - firewalld
    - freezer
    - gem
    - genesis
    - glassfish
    - gnome
    - google_chat
    - grafana4
    - grains
    - group
    - hashutil
    - helm
    - highstate_doc
    - hosts
    - http
    - hue
    - incron
    - ini
    - inspector
    - introspect
    - iosconfig
    - ip
    - ipset
    - iptables
    - jboss7
    - jboss7_cli
    - jinja
    - k8s
    - kernelpkg
    - key
    - keyboard
    - kmod
    - kubeadm
    - locale
    - locate
    - log
    - logrotate
    - lowpkg
    - lvm
    - mandrill
    - match
    - mattermost
    - mine
    - minion
    - modjk
    - mount
    - msteams
    - nagios_rpc
    - namecheap_domains
    - namecheap_domains_dns
    - namecheap_domains_ns
    - namecheap_ssl
    - namecheap_users
    - network
    - nexus
    - nftables
    - nova
    - nspawn
    - nxos
    - nxos_api
    - nxos_upgrade
    - openscap
    - openstack_config
    - opsgenie
    - out
    - pagerduty
    - pagerduty_util
    - pam
    - parallels
    - partition
    - peeringdb
    - pillar
    - pip
    - pkg
    - pkg_resource
    - ps
    - publish
    - pushover
    - pyenv
    - random
    - random_org
    - rbenv
    - rest_sample_utils
    - restartcheck
    - ret
    - rvm
    - s3
    - s6
    - salt_proxy
    - salt_version
    - saltcheck
    - saltutil
    - schedule
    - scsi
    - sdb
    - seed
    - serverdensity_device
    - service
    - shadow
    - slack
    - slsutil
    - smbios
    - smtp
    - solrcloud
    - sqlite3
    - ssh
    - state
    - status
    - statuspage
    - supervisord
    - sys
    - sysctl
    - sysfs
    - syslog_ng
    - system
    - telegram
    - telemetry
    - temp
    - test
    - timezone
    - tuned
    - udev
    - uptime
    - user
    - vault
    - vbox_guest
    - virtualenv
    - vsphere
    - x509
    - xfs
    - xml
    - zabbix
    - zenoss
#查看指定module的所有function
[root@master ~]# salt 'minion' sys.list_functions cmd
minion:
    - 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 'minion' sys.doc test
test.arg:

    Print out the data passed into the function ``*args`` and ``kwargs``, this
    is used to both test the publication data and CLI argument passing, but
    also to display the information available within the publication data.

    :return: ``{"args": args, "kwargs": kwargs}``
    :rtype: dict

    CLI Example:

        salt '*' test.arg 1 "two" 3.1 txt="hello" wow='{a: 1, b: "hello"}'
        
-----分割君------

#SaltStack默认也支持一次执行多个Module,Module之间通过逗号隔开,默认传参之间也是用逗号分隔,也支持指定传参分隔符号--args-separator=@即可
[root@master ~]# salt 'minion' test.echo,cmd.run,service.status "hello world",date,sshd
minion:
    ----------
    cmd.run:
        Wed Nov  3 14:38:31 CST 2021
    service.status:
        True
    test.echo:
        hello world

多个模块之间用","隔开一一对应,按照上面的例子就是
test.echo ——→ "hello world"  (因为是一个命令中间有空格,所以要用引号引起来)
cmd.run ——→ date
service.status ——→ sshd

常用模块—network

network.active_tcp

返回所有活动的tcp连接


[root@master ~]# salt 'minion' network.active_tcp
minion:
    ----------
    0:
        ----------
        local_addr:
            192.168.230.139
        local_port:
            22
        remote_addr:
            192.168.230.1
        remote_port:
            57239
    1:
        ----------
        local_addr:
            192.168.230.139
        local_port:
            52238
        remote_addr:
            192.168.230.131
        remote_port:
            4505
network.calc_net

通过IP和子网掩码计算出网段(子网划分)

[root@master ~]# salt 'minion' network.calc_net 192.168.1.1 255.255.255.0
minion:
    192.168.1.0/24
[root@master ~]# salt 'minion' network.calc_net 192.168.1.1 255.255.248.0
minion:
    192.168.0.0/21
[root@master ~]# salt 'minion' network.calc_net 192.168.1.1 255.255.0.0
minion:
    192.168.0.0/16
network.connect

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

[root@master ~]# salt 'minion' network.connect www.baidu.com 80     #连接百度端口号为80,可以连接
minion:
    ----------
    comment:
        Successfully connected to www.baidu.com (182.61.200.7) on tcp port 80
    result:
        True
[root@master ~]# salt 'minion' network.connect www.baidu.com 22
minion:
    ----------
    comment:
        Unable to connect to www.baidu.com (182.61.200.7) on tcp port 22   #连接百度端口号为22,不能连接,因为端口号不对
    result:
        False
ERROR: Minions returned with non-zero exit code

[root@master ~]# salt 'minion' network.connect 192.168.230.131 22    #连接本机,端口号为22,可以连接
minion:
    ----------
    comment:
        Successfully connected to 192.168.230.131 (192.168.230.131) on tcp port 22
    result:
        True
network.default_route

查看默认路由

[root@master ~]# salt 'minion' network.default_route
minion:
    |_
      ----------
      addr_family:
          inet
      destination:
          0.0.0.0
      flags:
          UG
      gateway:
          192.168.230.2
      interface:
          ens33
      netmask:
          0.0.0.0
    |_
      ----------
       addr_family:
          inet6
      destination:
          ::/0
      flags:
          !n
      gateway:
          ::
      interface:
          lo
      netmask:
    |_
      ----------
      addr_family:
          inet6
      destination:
          ::/0
      flags:
          !n
      gateway:
          ::
      interface:
          lo
      netmask:
network.get_fqdn

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

[root@master ~]# salt 'minion' network.get_fqdn
minion:
    minion
network.get_hostname

获取主机名

[root@master ~]# salt 'minion' network.get_hostname
minion:
    minion

network.get_route

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

[root@master ~]# salt '*' network.get_route 192.168.230.2
minion:
    ----------
    destination:
        192.168.230.2
    gateway:
        None
    interface:
        ens33
    source:
        192.168.230.139
master:
    ----------
    destination:
        192.168.230.2
    gateway:
        None
    interface:
        ens33
    source:
        192.168.230.131
network.hw_addr

返回指定网卡的MAC地址

[root@master ~]# salt 'minion' network.hw_addr ens33
minion:
    00:0c:29:dc:91:71
network.ifacestartswith

从特定CIDR检索接口名称

[root@master ~]# salt '*' network.ifacestartswith 192.168
minion:
    - ens33
master:
    - ens33
#如果使用全称的话是查询不到的
[root@master ~]# salt 'minion' network.ifacestartswith 192.168.230.255
minion:
[root@master ~]# salt 'minion' network.ifacestartswith 192.168.230
minion:
    - ens33

network.in_subnet

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

[root@master ~]# salt 'minion' network.in_subnet 192.168.230.0/24
minion:
    True

network.interface

返回指定网卡的信息

[root@master ~]# salt 'minion' network.interface ens33
minion:
    |_
      ----------
      address:
          192.168.230.139
      broadcast:
          192.168.230.255
      label:
          ens33
      netmask:
          255.255.255.0

network.interface_ip

返回指定网卡的IP地址

[root@master ~]# salt 'minion' network.interface_ip ens33
minion:
    192.168.230.139

network.interfaces

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

[root@master ~]# salt 'minion' network.interfaces
minion:
    ----------
    ens33:
        ----------
        hwaddr:
            00:0c:29:dc:91:71
        inet:
            |_
              ----------
              address:
                  192.168.230.139
              broadcast:
                  192.168.230.255
              label:
                  ens33
              netmask:
                  255.255.255.0
        inet6:
            |_
              ----------
               address:
                  fe80::cb64:63f2:9132:3a0a
              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

network.ip_addrs

返回IPv4的地址列表(只返回有效的)

[root@master ~]# salt 'minion' network.ip_addrs
minion:
    - 192.168.230.139
[root@minion ~]# ip a|grep 'inet ' |grep -v 127|awk -F '[ /]+' '{print $3}'
192.168.230.139
对比下上面更方便
network.netstat

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

[root@master ~]# salt 'minion' network.netstat
minion:
    |_
      ----------
      inode:
          30083
      local-address:
          0.0.0.0:22
      program:
          1056/sshd
      proto:
          tcp
      recv-q:
          0
      remote-address:
          0.0.0.0:*
      send-q:
          0
      state:
          LISTEN
      user:
          0
    |_
      ----------
----分割君----

network.ping

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

[root@master ~]# salt 'minion' network.ping www.baidu.com
minion:
    PING www.a.shifen.com (182.61.200.6) 56(84) bytes of data.
    64 bytes from localhost (182.61.200.6): icmp_seq=1 ttl=128 time=38.7 ms
    64 bytes from localhost (182.61.200.6): icmp_seq=2 ttl=128 time=43.4 ms
    64 bytes from localhost (182.61.200.6): icmp_seq=3 ttl=128 time=46.10 ms
    64 bytes from localhost (182.61.200.6): icmp_seq=4 ttl=128 time=45.8 ms
    
    --- www.a.shifen.com ping statistics ---
    4 packets transmitted, 4 received, 0% packet loss, time 3003ms
    rtt min/avg/max/mdev = 38.655/43.697/46.990/3.194 ms

network.reverse_ip

返回一个指定的IP地址的反向地址(很少使用)
反转有两个区域:

正向区域:把域名解析成IP
反向区域:把IP解析成域名
[root@master ~]# salt 'minion' network.reverse_ip 192.168.1.0
minion:
    0.1.168.192.in-addr.arpa

常用模块—service

service.available

判断指定的服务是否可用(只看你装了没有,即便你没启动)

[root@master ~]# salt 'minion' service.available sshd
minion:
    True
[root@master ~]# salt 'minion' service.available httpd
minion:
    True

service.get_all

获取所有正在运行的服务(进程)

#先在minion装下进程叔
[root@minion ~]# yum -y install psmisc
[root@minion ~]# pstree
systemd─┬─NetworkManager───2*[{NetworkManager}]
        ├─VGAuthService
        ├─auditd───{auditd}
        ├─crond
        ├─dbus-daemon───{dbus-daemon}
        ├─irqbalance───{irqbalance}
        ├─login───bash
        ├─nm-dispatcher───2*[{nm-dispatcher}]
        ├─polkitd───5*[{polkitd}]
        ├─rhsmcertd
        ├─rngd───{rngd}
        ├─rsyslogd───2*[{rsyslogd}]
        ├─salt-minion───salt-minion─┬─salt-minion
        │                           └─3*[{salt-minion}]
        ├─sshd───sshd───sshd─┬─bash───pstree
        │                    ├─bash───top
        │                    ├─bash───sleep
        │                    └─sftp-server
        ├─sssd─┬─sssd_be
        │      └─sssd_nss
        ├─systemd───(sd-pam)
        ├─systemd-journal
        ├─systemd-logind
        ├─systemd-udevd
        ├─tuned───3*[{tuned}]
        └─vmtoolsd───2*[{vmtoolsd}]

#查看
[root@master ~]# salt 'minion' service.get_all
minion:
    - NetworkManager
    - NetworkManager-dispatcher
    - NetworkManager-wait-online
    - arp-ethers
    - auditd
    - autovt@
    - basic.target
    - blk-availability
    - bluetooth.target
    - boot-complete.target
    - console-getty
    - container-getty@
    - cpupower
    - crond
    - cryptsetup-pre.target
    - cryptsetup.target
    - ctrl-alt-del.target
    - dbus
    - dbus-org.freedesktop.hostname1
    - dbus-org.freedesktop.locale1
    - dbus-org.freedesktop.login1
    - dbus-org.freedesktop.nm-dispatcher
    - dbus-org.freedesktop.portable1
    - dbus-org.freedesktop.timedate1
    - dbus.socket
    - debug-shell
    - default.target

service.disabled

检查指定服务是否开机不自动启动(状态)

[root@master ~]# salt 'minion' service.disabled httpd
minion:
    False

service.enabled

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

[root@master ~]# salt 'minion' service.enabled httpd
minion:
    True

service.disable

设置指定服务开机不自动启动

#先查询下是否开机自启
[root@minion ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; v>
   Active: inactive (dead) since Tue 2021-11-02 19:23:53 CST; 20h ago
     Docs: man:httpd.service(8)
 Main PID: 743738 (code=exited, status=0/SUCCESS)
   Status: "Running, listening on: port 80"

1102 19:18:10 minion systemd[1]: Starting The Apache HTTP Server.>
1102 19:18:25 minion httpd[743738]: AH00558: httpd: Could not rel>
1102 19:18:25 minion systemd[1]: Started The Apache HTTP Server.
1102 19:18:35 minion httpd[743738]: Server configured, listening >
1102 19:23:52 minion systemd[1]: Stopping The Apache HTTP Server.>
1102 19:23:53 minion systemd[1]: httpd.service: Succeeded.
1102 19:23:53 minion systemd[1]: Stopped The Apache HTTP Server.

[root@master ~]# salt 'minion' service.disable httpd
minion:
    True

#使用命令后再次查询
[root@minion ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; >
   Active: inactive (dead)
     Docs: man:httpd.service(8)

1102 19:06:59 minion systemd[1]: Stopping The Apache HTTP Server.>
1102 19:07:00 minion systemd[1]: httpd.service: Succeeded.
1102 19:07:00 minion systemd[1]: Stopped The Apache HTTP Server.
1102 19:18:10 minion systemd[1]: Starting The Apache HTTP Server.>
1102 19:18:25 minion httpd[743738]: AH00558: httpd: Could not rel>
1102 19:18:25 minion systemd[1]: Started The Apache HTTP Server.
1102 19:18:35 minion httpd[743738]: Server configured, listening >
1102 19:23:52 minion systemd[1]: Stopping The Apache HTTP Server.>
1102 19:23:53 minion systemd[1]: httpd.service: Succeeded.
1102 19:23:53 minion systemd[1]: Stopped The Apache HTTP Server.

service.enable

设置指定服务开机自动启动

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

#去minion查询
[root@minion ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; v>
   Active: inactive (dead)
     Docs: man:httpd.service(8)

1102 19:06:59 minion systemd[1]: Stopping The Apache HTTP Server.>
1102 19:07:00 minion systemd[1]: httpd.service: Succeeded.
1102 19:07:00 minion systemd[1]: Stopped The Apache HTTP Server.
1102 19:18:10 minion systemd[1]: Starting The Apache HTTP Server.>
1102 19:18:25 minion httpd[743738]: AH00558: httpd: Could not rel>
1102 19:18:25 minion systemd[1]: Started The Apache HTTP Server.
1102 19:18:35 minion httpd[743738]: Server configured, listening >
1102 19:23:52 minion systemd[1]: Stopping The Apache HTTP Server.>
1102 19:23:53 minion systemd[1]: httpd.service: Succeeded.
1102 19:23:53 minion systemd[1]: Stopped The Apache HTTP Server.

service.reload

重新加载指定服务

[root@master ~]# salt 'minion' service.reload httpd
minion:
    True
service.stop/start

停止/启动指定服务

[root@master ~]# salt 'minion' service.stop httpd
minion:
    True
[root@master ~]# salt 'minion' service.start httpd
minion:
    True

service.restart/status

重启指定服务/查看指定服务

[root@master ~]# salt 'minion' service.restart httpd
minion:
    True
[root@master ~]# salt 'minion' service.status httpd
minion:
    True

常用模块—pkg

pkg.download

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

[root@master ~]# salt 'minion' cmd.run 'yum -y install yum-utils'
minion:
    Last metadata expiration check: 0:31:45 ago on Wed Nov  3 15:25:41 2021.
    Package yum-utils-4.0.21-3.el8.noarch is already installed.
    Dependencies resolved.
    Nothing to do.
    Complete!

[root@master ~]# salt 'minion' pkg.download httpd
minion:
    ----------
    httpd:
        /var/cache/yum/packages/httpd-2.4.37-40.module_el8.5.0+852+0aafc63b.x86_64.rpm

[root@master ~]# salt 'minion' pkg.download wget
minion:
    ----------
    wget:
        /var/cache/yum/packages/wget-1.19.5-10.el8.x86_64.rpm

#到minion去查看
[root@minion ~]# cd /var/cache/yum/packages/
[root@minion packages]# ls
httpd-2.4.37-40.module_el8.5.0+852+0aafc63b.x86_64.rpm
[root@minion packages]# ls
httpd-2.4.37-40.module_el8.5.0+852+0aafc63b.x86_64.rpm
wget-1.19.5-10.el8.x86_64.rpm
#查看后是没有发现wget安装的,只是拥有这个安装包
[root@minion packages]# rpm -aq | grep wget
我用了两个测试,安装httpd是因为测试下会不会把依赖包也安装过去,安装wget是为了测试只是下载指定的软件包,却没有进行安装
pkg.file_list

列出安装的时候生成的文件

[root@master ~]# salt 'minion' pkg.file_list httpd
minion:
    ----------
    errors:
    files:
        - /etc/httpd/conf
        - /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-optional.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.modules.d/README
        - /etc/httpd/conf/httpd.conf
        - /etc/httpd/conf/magic
        - /etc/httpd/logs
        - /etc/httpd/modules
        - /etc/httpd/run
        - /etc/httpd/state
        - /etc/logrotate.d/httpd
        - /etc/sysconfig/htcacheclean
        - /run/httpd

[root@master ~]# salt 'minion' pkg.file_list openssh (相当于apr)
minion:
    ----------
    errors:
    files:
        - /etc/ssh
        - /etc/ssh/moduli
        - /usr/bin/ssh-keygen
        - /usr/lib/.build-id
        - /usr/lib/.build-id/0b
        - /usr/lib/.build-id/0b/bdc4d92ff3c605b56714b6510fccde281765e5
        - /usr/lib/.build-id/58
        - /usr/lib/.build-id/58/7ef4180fd17a06c2f3ae9c1ccb8bcdb46b4fe5
        - /usr/libexec/openssh
        - /usr/libexec/openssh/ssh-keysign
        - /usr/share/doc/openssh
        - /usr/share/doc/openssh/CREDITS
        - /usr/share/doc/openssh/ChangeLog
        - /usr/share/doc/openssh/INSTALL
        - /usr/share/doc/openssh/OVERVIEW
        - /usr/share/doc/openssh/PROTOCOL
        - /usr/share/doc/openssh/PROTOCOL.agent
        - /usr/share/doc/openssh/PROTOCOL.certkeys
        - /usr/share/doc/openssh/PROTOCOL.chacha20poly1305
        - /usr/share/doc/openssh/PROTOCOL.key
        - /usr/share/doc/openssh/PROTOCOL.krl
        - /usr/share/doc/openssh/PROTOCOL.mux
        - /usr/share/doc/openssh/README
        - /usr/share/doc/openssh/README.dns
        - /usr/share/doc/openssh/README.platform
        - /usr/share/doc/openssh/README.privsep
        - /usr/share/doc/openssh/README.tun
        - /usr/share/doc/openssh/TODO
        - /usr/share/licenses/openssh
        - /usr/share/licenses/openssh/LICENCE
        - /usr/share/man/man1/ssh-keygen.1.gz
        - /usr/share/man/man8/ssh-keysign.8.gz

pkg.group_info

查看包组的信息

[root@master ~]# salt 'minion' pkg.group_info 'Development Tools'
minion:
    ----------
    conditional:
    default:
        - asciidoc
        - byacc
        - ctags
        - diffstat
        - elfutils-libelf-devel
        - git
        - intltool
        - jna
        - ltrace
        - patchutils
        - perl-Fedora-VSP
        - perl-Sys-Syslog
        - perl-generators
        - pesign
        - source-highlight
        - systemtap
        - valgrind
        - valgrind-devel
    description:
        A basic development environment.
    group:
        Development Tools
    id:
        None
    mandatory:
        - autoconf
        - automake
        - binutils
        - bison
        - flex
        - gcc
        - gcc-c++
        - gdb
        - glibc-devel
        - libtool
        - make
        - pkgconf
        - pkgconf-m4
        - pkgconf-pkg-config
        - redhat-rpm-config
        - rpm-build
        - rpm-sign
        - strace
    optional:
        - cmake
        - expect
        - rpmdevtools
        - rpmlint
    type:
        package group

pkg.group_list

列出系统中所有的包组

[root@master ~]# salt 'minion' pkg.group_list
minion:
    ----------
    available:    #可用的包组
        - Backup Client
        - base-x
        - Conflicts AppStream
        - Container Management
        - Debugging Tools
        - Desktop Debugging and Performance Tools
        - .NET Core Development
        - FTP Server
        - GNOME Applications
        - Graphics Creation Tools
        - Guest Agents
        - Guest Desktop Agents
        - Input Methods
        - Internet Applications
        - Internet Browser
        - Java Platform
        - Legacy X Window System Compatibility
        - Multimedia
        - Office Suite and Productivity
        - Atomic Host ostree support
        - KVM platform specific packages
        - Hyper-v platform specific packages
        - Printing Client
        - Remote Desktop Clients
        - RPM Development Tools
        - TeX formatting system
        - Virtualization Client
        - Virtualization Hypervisor
        - Virtualization Platform
        - Virtualization Tools
        - Basic Web Server
        - Additional Development
        - Anaconda tools
        - Base
        - Conflicts BaseOS
        - Dial-up Networking Support
        - File and Storage Server
        - Fonts
        - GNOME
        - Graphical Administration Tools
        - Hardware Monitoring Utilities
        - Hardware Support
        - Headless Management
        - Infiniband Support
        - Large Systems Performance
        - Legacy UNIX Compatibility
        - Mail Server
        - Mainframe Access
        - Network File System Client
        - Network Servers
        - Networking Tools
        - Common NetworkManager submodules
        - Performance Tools
        - Platform Development
        - Python Web
        - Remote Management for Linux
        - Scientific Support
        - Security Tools
        - Server product core
        - Smart Card Support
        - Windows File Server
        - Standard
        - System Tools
        - Workstation product core
        - Critical Path (KDE)
        - Fedora Packager
        - Firefox Web Browser
        - KDE Applications
        - KDE
        - KDE Educational applications
        - KDE Multimedia support
        - KDE Office
        - KDE Software Development
        - KDE Frameworks 5 Software Development
        - Xfce
    available environments:
        - Server with GUI
        - Server
        - Workstation
        - KDE Plasma Workspaces
        - Virtualization Host
        - Custom Operating System
    available languages:
        ----------
    installed:
        - VMware platform specific packages
        - Core
        - Development Tools
    installed environments:
        - Minimal Install

pkg.install

安装软件

[root@master ~]# salt 'minion' pkg.install wget
minion:
    ----------
    wget:
        ----------
        new:
            1.19.5-10.el8
        old:

pkg.list_downloaded

列出已下载到本地的软件包(手动下载是查询不到的)

[root@master ~]# salt 'minion' pkg.list_downloaded
minion:
    ----------
pkg.list_pkgs

以字典的方式列出当前已安装的软件包(极少使用)

[root@master ~]# salt 'minion' pkg.list_pkgs
minion:
    ----------
    NetworkManager:
        1:1.30.0-0.3.el8
    NetworkManager-libnm:
        1:1.30.0-0.3.el8
    NetworkManager-team:
        1:1.30.0-0.3.el8
    NetworkManager-tui:
        1:1.30.0-0.3.el8
    abattis-cantarell-fonts:
        0.0.25-4.el8
    acl:
        2.2.53-1.el8
    adwaita-cursor-theme:
        3.28.0-2.el8
    adwaita-icon-theme:
        3.28.0-2.el8
    apr:
        1.6.3-12.el8
    apr-util:
        1.6.1-6.el8
    apr-util-bdb:
        1.6.1-6.el8
    apr-util-openssl:
        1.6.1-6.el8
    at-spi2-atk:
        2.26.2-1.el8
    at-spi2-core:
        2.28.0-1.el8
    atk:
        2.28.1-1.el8
    audit:
        3.0-0.17.20191104git1c2f876.el8
    audit-libs:
        3.0-0.17.20191104git1c2f876.el8
    authselect:
        1.2.2-1.el8
----分割君-----

pkg.owner

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

[root@master ~]# salt 'minion' pkg.owner /usr/bin/ls
minion:
    coreutils
[root@master ~]# salt 'minion' pkg.owner /etc/passwd
minion:
    setup

#也可以执行多个命令查询
[root@master ~]# salt 'minion' pkg.owner /etc/passwd /usr/bin/ls
minion:
    ----------
    /etc/passwd:
        setup
    /usr/bin/ls:
        coreutils

pkg.remove

卸载指定软件

[root@master ~]# salt 'minion' pkg.remove wget
minion:
    ----------
    wget:
        ----------
        new:
        old:
            1.19.5-10.el8

#去minion查看
[root@minion packages]# rpm -aq | grep wget
wget-1.19.5-10.el8.x86_64
[root@minion packages]# rpm -aq | grep wget

pkg.upgrade

升级系统中所有的软件包或升级指定的软件包(必须在安装后的前提下才能升级)

#因为没有需要升级的包,所有临时下了个做示范
[root@minion src]# wget http://mirror.centos.org/centos/7/os/x86_64/Packages/wget-1.14-18.el7_6.1.x86_64.rpm

[root@minion src]# rpm -ivh wget-1.14-18.el7_6.1.x86_64.rpm --nodeps    # --nodeps 强制执行
警告:wget-1.14-18.el7_6.1.x86_64.rpm: 头V3 RSA/SHA256 Signature, 密钥 ID f4a80eb5: NOKEY
Verifying...                          ################################# [100%]
准备中...                          ################################# [100%]
正在升级/安装...
   1:wget-1.14-18.el7_6.1             ################################# [100%]
[root@minion src]# rpm -qa | grep wget
wget-1.14-18.el7_6.1.x86_64

[root@master ~]# salt 'minion' pkg.upgrade name=wget
minion:
    ----------
    wget:
        ----------
        new:
            1.19.5-10.el8
        old:
            1.14-18.el7_6.1

常用模块—state

state.show_highstate

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

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

state.highstate

执行高级状态

[root@master ~]# salt 'minion' state.highstate
minion:
    Minion did not return. [No response]
    The minions may not have all finished running and any remaining minions will return upon completion. To look up the return data for this job later, run the following command:
    
    salt-run jobs.lookup_jid 20211103083931250447
ERROR: Minions returned with non-zero exit code

state.show_state_usage

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

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

state.show_top

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

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

state.top

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

[root@master ~]# salt 'minion' state.top test.sls
minion:
----------
          ID: apache-install
    Function: pkg.installed
        Name: httpd
      Result: True
     Comment: All specified packages are already installed
     Started: 14:43:15.678238
    Duration: 768.272 ms
     Changes:   
----------
          ID: apache-service
    Function: service.running
        Name: httpd
      Result: True
     Comment: The service httpd is already running
     Started: 14:52:16.877135
    Duration: 35.889 ms
     Changes:   

Summary for node1
------------
Succeeded: 2
Failed:    0
------------
Total states run:     2
Total run time:   1.384 s

state.show_sls

显示 主机 上特定sls或sls文件列表中的状态数据(值)

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


常用模块—cp

minion端进行使用(不是同级进行互传,是master对minion传)

salt://的意思:
salt根目录:在master中 file_roots 定义的路径
例如:假设在master中有如下定义:

[root@master ~]# vim /etc/salt/master


file_roots:
  base:
    - /srv/salt/base

那么:salt://test指的实际路径是:/srv/salt/base/test,这样做的好处是,可以满足state系统中环境的概念。

环境说明:

[root@master base]# pwd
/srv/salt/base
[root@master base]# tree
.
├── test
│   ├── test1
│   ├── test2
│   └── test3
│       ├── test4
│       └── test5
├── top.sls
├── vimrc
└── web
    ├── apache
    │   └── install.sls
    └── nginx
        └── install.sls

6 directories, 7 files
cache_dest

如果使用 缓存,则返回文件的预期缓存路径

[root@master salt]# salt 'minion' cp.cache_dest https://foo.com/bar.rpm
minion:
    /var/cache/salt/minion/extrn_files/base/foo.com/bar.rpm

cache_dir

从 master 下载并缓存目录下的所有内容

[root@master ~]# salt 'node1' cp.cache_dir salt://test/test2/
node1:
    - /var/cache/salt/minion/files/base/test/test2/test3
    - /var/cache/salt/minion/files/base/test/test2/test4

[root@node1 ~]# ls /var/cache/salt/minion/files/base/test/test2/
test3  test4
cache_local_file

在 localfiles 缓存中缓存 minion 上的本地文件

[root@master salt]# salt 'minion' cp.cache_local_file /etc/hosts
minion:
    /var/cache/salt/minion/localfiles/etc/hosts

get_file

master下载文件到客户端

[root@master ~]# salt 'minion' cp.get_dir salt://web/apache/ /minion/dest
minion:
    - /minion/dest/apache/install.sls

[root@minion ~]# ls /minion/dest/apache/install.sls 
/minion/dest/apache/install.sls

常用模块—useradd

添加用户,改变组,家目录等等

add

添加新用户

[root@master ~]# salt 'minion' user.add test
minion:
    True

[root@minion ~]# id test
uid=1001(test) gid=1001(test)=1001(test)

chfullname

更改用户的全名(显示改成功,但是查看后还是没有更改)

[root@master ~]# salt 'minion' user.chfullname test "test 1"
minion:
    True

chuid

更改用户UID

[root@master ~]# salt 'minion' user.chuid test 2000
minion:
    True

[root@minion ~]# id test
uid=2000(test) gid=1001(test)=1001(test)

gid

更改用户GID

[root@master ~]# salt 'minion' user.chgid test 1001
minion:
    True

[root@minion ~]# id test
uid=2000(test) gid=1001(test)=1001(test)

info

返回用户信息

[root@master ~]# salt 'minion' user.info test
minion:
    ----------
    fullname:
        ll
    gid:
        1001
    groups:
        - test
    home:
        /home/test
    homephone:
    name:
        test
    other:
    passwd:
        x
    roomnumber:
    shell:
        /bin/bash
    uid:
        2000
    workphone:

getent

返回所有系统用户信息的列表

[root@master ~]# salt '*' user.getent
master:
    |_
      ----------
      fullname:
          root
      gid:
          0
      groups:
          - root
      home:
          /root
      homephone:
      name:
          root
      other:
      passwd:
          x
      roomnumber:
      shell:
          /bin/bash
      uid:
          0
      workphone:
    |_
      ----------
      fullname:
          bin
          
---分割君-----
list_groups

列出指定用户所属组的列表

[root@master ~]# salt 'minion' user.list_groups test
minion:
    - test

list_users

返回所有用户的列表

[root@master ~]# salt 'minion' user.list_users
minion:
    - adm
    - apache
    - bin
    - daemon
    - dbus
    - ftp
    - games
    - halt
    - lp
    - mail
    - nobody
    - operator
    - polkitd
    - rngd
    - root
    - shutdown
    - sshd
    - sssd
    - sync
    - systemd-coredump
    - systemd-resolve
    - test
    - tss
    - unbound
    - zhuzhu

delete

删除用户

[root@master ~]# salt 'minion' user.delete test
minion:
    True

#进行查看
[root@minion ~]# id test
uid=2000(test) gid=1001(test)=1001(test)
[root@minion ~]# id test
id: “test”:无此用户

常用模块—salt-cp

salt-cp能够很方便的把 master 上的文件批量传到 minion上

[root@master ~]# salt-cp 'minion' /root/test /opt/
minion:
    ----------
    /opt/test:
        True

[root@master ~]# salt 'minion' cmd.run 'ls -l /opt'
minion:
    total 4
    -rw-r--r-- 1 root root 25 Nov  4 14:19 test

常用模块—file

环境说明:

[root@master ~]# salt-key -L
Accepted Keys:
minion
node1
Denied Keys:
Unaccepted Keys:
Rejected Keys:
access

检查指定路径是否存在

[root@master ~]# salt 'minion' file.access /opt/test f
minion:
    True

检查指定文件的权限信息

[root@master ~]# salt 'minion' file.access /opt/test r
minion:
    True
[root@master ~]# salt 'minion' file.access /opt/test x
minion:
    False
[root@master ~]# salt 'minion' cmd.run 'ls -l /opt'
minion:
    total 4
    -rw-r--r-- 1 root root 25 Nov  4 14:19 test

append

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

[root@master ~]# salt 'minion' file.append /opt/test hello
minion:
    Wrote 1 lines to "/opt/test"
[root@master ~]# salt 'minion' file.append /opt/abc hello     #没有abc文件
minion:
    The minion function caused an exception: Traceback (most recent call last):
      File "/usr/lib/python3.6/site-packages/salt/minion.py", line 1912, in _thread_return
        function_name, function_args, executors, opts, data
      File "/usr/lib/python3.6/site-packages/salt/minion.py", line 1868, in _execute_job_function

[root@master ~]# salt 'minion' file.append /opt/test.sh "date '+%Y%m%d'"
minion:
    Wrote 1 lines to "/opt/test.sh"
[root@master ~]# salt 'minion' cmd.run 'cat /opt/test.sh'
minion:
    #!/bin/bash
    
    hello world
    hello
    date '+%Y%m%d'

basename

获取指定路径的基名

[root@master ~]# salt 'minion' file.basename /usr/src/nginx.tar.xz      #取出名称
minion:
    nginx.tar.xz
[root@master ~]# salt 'minion' file.dirname /usr/src/nginx.tar.xz     #取出路径
minion:
    /usr/src

check_hash

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

[root@master ~]# salt '*' cmd.run 'md5sum /root/anaconda-ks.cfg'
minion:
    8d0c1ea239da561b74503ed6aaa6ef2c  /root/anaconda-ks.cfg
master:
    c5e5e1c4bad9feb10ae4709d3635ee83  /root/anaconda-ks.cfg

[root@master ~]# salt '*' file.check_hash /root/anaconda-ks.cfg c5e5e1c4bad9feb10ae4709d3635ee83
minion:
    False
master:
    True

chattr

修改指定文件的属性

属性对文件的意义对目录的意义
a只允许在这个文件之后追加数据,不允许任何进程覆盖或截断这个文件只允许在这个目录下建立和修改文件,而不允许删除任何文件前提是这里面有e的权限
i不允许对这个文件进行任何的修改,不能删除、更改、移动 任何的进程只能修改目录之下的文件,不允许建立和删除文件
[root@minion ~]# echo hello > abc
[root@minion ~]# ls
abc  anaconda-ks.cfg  rpmbuild
[root@minion ~]# ll -d abc 
-rw-r--r-- 1 root root 6 114 15:01 abc
[root@minion ~]# lsattr abc
-------------------- abc
[root@minion ~]# chattr +a abc 
[root@minion ~]# lsattr abc
-----a-------------- abc
[root@minion ~]# rm -rf abc 
rm: 无法删除'abc': 不允许的操作
[root@minion ~]# echo world > abc 
-bash: abc: 不允许的操作


chown

设置指定文件的属主、属组信息

[root@master ~]# salt 'minion' file.chown /opt/test.sh zhuzhu zhuzhu
minion:
    None
[root@master ~]# salt 'minion' cmd.run 'ls -l /opt '
minion:
    total 4
    -rw-r--r-- 1 zhuzhu zhuzhu 46 Nov  4 14:28 test.sh

copy

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

拷贝文件


[root@master ~]# salt '*' file.copy /root/123 /opt/ recurse=true
minion:
    True
node1:
    True

file.ditectory_exists

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

[root@master ~]# salt '*' cmd.run 'ls -l /root/'
node1:
    total 8
    -rw-r--r--. 1 root root    6 Nov  4 03:25 123
    -rw-------. 1 root root 1253 Sep 30 12:34 anaconda-ks.cfg
minion:
    total 12
    -rw-r--r--  1 root root    0 Nov  4 15:29 123
    drwxr-xr-x  2 root root    6 Nov  4 15:46 1234
    -rw-r--r--  1 root root    6 Nov  4 15:01 abc
    -rw-------. 1 root root 1258 Oct 30 22:20 anaconda-ks.cfg
    -rw-r--r--  1 root root 1164 Nov  4 15:18 passwd
    drwxr-xr-x. 4 root root   34 Nov  3 21:19 rpmbuild
[root@master ~]# salt '*' file.directory_exists /root/1234
minion:
    True
node1:
    False

file.diskusage

递归计算指定路径的磁盘使用情况并以字节为单位返回

[root@master ~]# salt '*' cmd.run 'ls -l /root/'
node1:
    total 8
    -rw-r--r--. 1 root root    6 Nov  4 03:25 123
    -rw-------. 1 root root 1253 Sep 30 12:34 anaconda-ks.cfg
minion:
    total 12
    -rw-r--r--  1 root root    0 Nov  4 15:29 123
    drwxr-xr-x  2 root root    6 Nov  4 15:46 1234
    -rw-r--r--  1 root root    6 Nov  4 15:01 abc
    -rw-------. 1 root root 1258 Oct 30 22:20 anaconda-ks.cfg
    -rw-r--r--  1 root root 1164 Nov  4 15:18 passwd
    drwxr-xr-x. 4 root root   34 Nov  3 21:19 rpmbuild
[root@master ~]# salt '*' file.diskusage /root/123
minion:
    0
node1:
    6
[root@master ~]# salt '*' file.diskusage /root/anaconda-ks.cfg 
minion:
    1258
node1:
    1253

file.file_exists

判断指定文件是否存在

[root@master ~]# salt '*' file.file_exists /root/abc
minion:
    True
node1:
    False

file.find

类似 find 命令并返回符合指定条件的路径列表

The options include match criteria:
name    = path-glob   (区分大小写,名称)              # case sensitive
iname   = path-glob   (不区分大小写,名称)         # case insensitive
regex   = path-regex                # case sensitive
iregex  = path-regex                # case insensitive
type    = file-types  (类型)          # match any listed type
user    = users       (用户)             # match any listed user
group   = groups      (用户组)               # match any listed group
size    = [+-]number[size-unit]   (大小)    # default unit = byte
mtime   = interval    (修改时间)          # modified since date
grep    = regex       (搜索文件内容)             # search file contents
and/or actions:
delete [= file-types]               # default type = 'f'
exec    = command [arg ...]         # where {} is replaced by pathname
print  [= print-opts]
and/or depth criteria:

maxdepth = maximum depth to transverse in path
mindepth = minimum depth to transverse before checking files or directories
The default action is print=path
path-glob:
*                = match zero or more chars
?                = match any char
[abc]            = match a, b, or c
[!abc] or [^abc] = match anything except a, b, and c
[x-y]            = match chars x through y
[!x-y] or [^x-y] = match anything except chars x through y
{a,b,c}          = match a or b or c

path-regex: a Python Regex (regular expression) pattern to match pathnames

file-types: a string of one or more of the following:

a: all file types
b: block device
c: character device
d: directory
p: FIFO (named pipe)
f: plain file
l: symlink
s: socket

users: a space and/or comma separated list of user names and/or uids

groups: a space and/or comma separated list of group names and/or gids
size-unit:
b: bytes
k: kilobytes
m: megabytes
g: gigabytes
t: terabytes
interval:
[<num>w] [<num>d] [<num>h] [<num>m] [<num>s]

where:
   w: week
    d: day
   h: hour
   m: minute
   s: second

print-opts: a comma and/or space separated list of one or more of the following:

group: group name
md5:   MD5 digest of file contents
mode:  file permissions (as integer)
mtime: last modification time (as time_t)
name:  file basename
path:  file absolute path
size:  file size in bytes
type:  file type
user:  user name

实例:

salt '*' file.find / type=f name=\*.bak size=+10m

意思:查找根目录下,文件类型为文件的名称为.bak结尾的(这里支持正则表达式),大小大于10MB的文件

type= (a:所有文件类型,b:块设备 ,c:字符设备,d:目录, p:FIFO(命名管道), f:普通文件 ,l:符号链接 ,s:套接字)
size= (b:字节,k:千字节,m:兆字节,g:GB,t:太字节也是TB)

salt '*' file.find /var mtime=+30d size=+10m print=path,size,mtime

意思:这里是查找/var目录下,最后一次更改时间是30天以前,大小是大于10MB的文件,并打印文件的路径、大小、更改时间

mtime= (w:周,d:天,h:小时,m:分钟,s:秒)

 salt '*' file.find /var/log name=\*.[0-9] mtime=+30d size=+10m delete

意思:查找/var/log目录下,名字是以0-9结尾的,最后一次更改时间为30天以前,大小是大于10MB的文件,找到后进行删除

最后执行的动作除了delete和print,还有exec command

file.get_gid

获取指定文件的gid

[root@master ~]# salt '*' file.get_group /root/123
minion:
    root
node1:
    root

file.get_hash

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

[root@master ~]# salt '*' file.get_hash /root/123
node1:
    5891b5b522d5df086d0ff0b110fbd9d21bb4fc7163af34d08286a2e846f6be03
minion:
    e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
file.get_mode

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

[root@master ~]# salt '*' file.get_mode /root/123
node1:
    0644
minion:
    0644

ile.get_selinux_context

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

[root@master ~]# salt '*' file.get_selinux_context /root/123
node1:
    unconfined_u:object_r:admin_home_t:s0
minion:
    No selinux context information is available for /root/123

file.get_sum

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

md5
sha1
sha224
sha256 (default)
sha384
sha512
[root@master ~]# salt '*' cmd.run 'sha256sum /root/123'
node1:
    5891b5b522d5df086d0ff0b110fbd9d21bb4fc7163af34d08286a2e846f6be03  /root/123
minion:
    e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855  /root/123

[root@master ~]# salt '*' file.get_sum /root/123 md5
minion:
    d41d8cd98f00b204e9800998ecf8427e
node1:
    b1946ac92492d2347c6235b4d2611184

file.get_uid与file.get_user

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

[root@master ~]# salt '*' file.get_uid /root/123
minion:
    0
node1:
    0
[root@master ~]# salt '*' file.get_user /root/123
minion:
    root
node1:
    root

file.gid_to_group

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

[root@master ~]# salt '*' file.gid_to_group 999
minion:
    input
node1:
    input

file.group_to_gid

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

[root@master ~]# salt '*' file.group_to_gid root
minion:
    0
node1:
    0
[root@master ~]# salt '*' file.group_to_gid zhuzhu
minion:
    1000
node1:
    1000

file.grep

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

#过滤/ect/passwd文件中包含nobody的行。
[root@master ~]# salt '*' file.grep /etc/passwd nobody
node1:
    ----------
    pid:      #grep运行的pid号
        142550
    retcode:    #状态码
        0      #成功过滤1为非成功过滤
    stderr:    #错误输出
    stdout:    #正常输出也就是我们要过滤的内容
        nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin
minion:
    ----------
    pid:
        311411
    retcode:
        0
    stderr:
    stdout:
        nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin

file.is_blkdev

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

[root@master ~]# salt '*' file.is_blkdev /dev/sr0
minion:
    True
node1:
    True

file.lsattr

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

[root@master ~]# salt '*' file.lsattr /root/123
minion:
    ----------
    /root/123:
node1:
    ----------
    /root/123:

file.mkdir

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

[root@master ~]# salt '*' file.mkdir /opt/1
minion:
    True
node1:
    True
[root@master ~]# salt '*' cmd.run 'ls -l /opt'
minion:
    total 4
    drwxr-xr-x 2 root   root    6 Nov  4 16:59 1
    -rw-r--r-- 1 zhuzhu zhuzhu 46 Nov  4 14:28 test.sh
node1:
    total 0
    drwxr-xr-x. 2 root root 6 Nov  4 04:59 1

[root@master ~]# salt '*' file.mkdir /opt/12 zhuzhu zhuzhu 777
minion:
    True
node1:
    True
[root@master ~]# salt '*' cmd.run 'ls -l /opt'
minion:
    total 4
    drwxr-xr-x 2 root   root    6 Nov  4 16:59 1
    drwxrwxrwx 2 zhuzhu zhuzhu  6 Nov  4 17:00 12
    -rw-r--r-- 1 zhuzhu zhuzhu 46 Nov  4 14:28 test.sh
node1:
    total 0
    drwxr-xr-x. 2 root   root   6 Nov  4 04:59 1
    drwxrwxrwx. 2 zhuzhu zhuzhu 6 Nov  4 05:00 12

file.move

移动或重命名


file.prepend

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

[root@master ~]# salt 'minion' file.prepend /root/123 "hello world"
minion:
    Prepended 1 lines to "/root/123"

[root@minion ~]# cat 123
hello world

file.sed

修改文本文件的内容

[root@master ~]# salt 'minion' file.prepend /root/123 'haha' '123'
minion:
    Prepended 2 lines to "/root/123"

[root@master ~]# salt 'minion' cmd.run 'cat /root/123'
minion:
    haha
    123
    haha
    123
    hello world

[root@master ~]# salt 'minion' file.sed /root/123 'haha' 'hehe'
minion:
    ----------
    pid:
        19720
    retcode:
        0
    stderr:
    stdout:

[root@master ~]# salt 'minion' cmd.run 'cat /root/123'
minion:
    hehe
    123
    hehe
    123
    hello world

[root@master ~]# salt 'minion' file.sed /root/123 'hello' '123' flags=2
minion:
    ----------
    pid:
        25752
    retcode:
        0
    stderr:
    stdout:

file.read

读取文件内容

[root@master ~]# salt 'minion' cmd.run 'cat /root/123'
minion:
    hehe
    123
    hehe
    123
    hello world
[root@master ~]# salt 'minion' file.read /root/123
minion:
    hehe
    123
    hehe
    123
    hello world

file.readdir

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

[root@master ~]# salt 'minion' file.readdir /root
minion:
    - .
    - ..
    - .bash_logout
    - .bash_profile
    - .bashrc
    - .cshrc
    - .tcshrc
    - anaconda-ks.cfg
    - .config
    - .wget-hsts
    - .bash_history
    - .ssh
    - rpmbuild
    - abc
    - passwd
    - 1234
    - .viminfo
    - 123.bak
    - 123

file.remove

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

[root@master ~]# salt 'minion' cmd.run 'ls -l /root/'
minion:
    total 20
    -rw-r--r--  1 root root   30 Nov  5 14:41 123
    -rw-r--r--  1 root root   30 Nov  5 14:39 123.bak
    drwxr-xr-x  2 root root    6 Nov  4 15:46 1234
    -rw-r--r--  1 root root    6 Nov  4 15:01 abc
    -rw-------. 1 root root 1258 Oct 30 22:20 anaconda-ks.cfg
    -rw-r--r--  1 root root 1164 Nov  4 15:18 passwd
    drwxr-xr-x. 4 root root   34 Nov  3 21:19 rpmbuild
[root@master ~]# salt 'minion' file.remove /root/1234
minion:
    True

file.rename

重命名文件或目录

[root@master ~]# salt 'minion' file.rename /root/123 /root/1234
minion:
    True
[root@master ~]# salt 'minion' cmd.run 'ls -l /root/'
minion:
    total 20
    -rw-r--r--  1 root root   30 Nov  5 14:39 123.bak
    -rw-r--r--  1 root root   30 Nov  5 14:41 1234
    -rw-r--r--  1 root root    6 Nov  4 15:01 abc
    -rw-------. 1 root root 1258 Oct 30 22:20 anaconda-ks.cfg
    -rw-r--r--  1 root root 1164 Nov  4 15:18 passwd
    drwxr-xr-x. 4 root root   34 Nov  3 21:19 rpmbuild
[root@master ~]# salt 'minion' file.rename /root/1234 /opt/123
minion:
    True
[root@master ~]# salt 'minion' cmd.run 'ls -l /opt'
minion:
    total 8
    drwxr-xr-x 2 root   root    6 Nov  4 16:59 1
    drwxrwxrwx 2 zhuzhu zhuzhu  6 Nov  4 17:00 12
    -rw-r--r-- 1 root   root   30 Nov  5 14:41 123
    -rw-r--r-- 1 zhuzhu zhuzhu 46 Nov  4 14:28 test.sh

file.set_mode

给指定文件设置权限

[root@master ~]# salt 'minion' cmd.run 'ls -l /root/'
minion:
    total 16
    -rw-r--r--  1 root root   30 Nov  5 14:39 123.bak
    -rw-r--r--  1 root root    6 Nov  4 15:01 abc
    -rw-------. 1 root root 1258 Oct 30 22:20 anaconda-ks.cfg
    -rw-r--r--  1 root root 1164 Nov  4 15:18 passwd
    drwxr-xr-x. 4 root root   34 Nov  3 21:19 rpmbuild
[root@master ~]# salt 'minion' file.set_mode /root/passwd 0777
minion:
    0777
[root@master ~]# salt 'minion' cmd.run 'ls -l /root/'
minion:
    total 16
    -rw-r--r--  1 root root   30 Nov  5 14:39 123.bak
    -rw-r--r--  1 root root    6 Nov  4 15:01 abc
    -rw-------. 1 root root 1258 Oct 30 22:20 anaconda-ks.cfg
    -rwxrwxrwx  1 root root 1164 Nov  4 15:18 passwd
    drwxr-xr-x. 4 root root   34 Nov  3 21:19 rpmbuild

file.symlink

给指定的文件创建软链接

[root@master ~]# salt 'minion' cmd.run 'ls -l /root/'
minion:
    total 8
    -rw-r--r--  1 root root    0 Nov  5 15:05 123
    drwxr-xr-x  2 root root    6 Nov  5 15:05 1234
    -rw-r--r--  1 root root    6 Nov  4 15:01 abc
    -rw-------. 1 root root 1258 Oct 30 22:20 anaconda-ks.cfg
    drwxr-xr-x. 4 root root   34 Nov  3 21:19 rpmbuild

[root@master ~]# salt 'minion' file.symlink /root/123 /opt/123
minion:
    True

[root@master ~]# salt 'minion' cmd.run 'ls -l /root;ls -l /opt/'
minion:
    total 8
    -rw-r--r--  1 root root    0 Nov  5 15:05 123
    drwxr-xr-x  2 root root    6 Nov  5 15:05 1234
    -rw-r--r--  1 root root    6 Nov  4 15:01 abc
    -rw-------. 1 root root 1258 Oct 30 22:20 anaconda-ks.cfg
    drwxr-xr-x. 4 root root   34 Nov  3 21:19 rpmbuild
    total 0
    lrwxrwxrwx 1 root root 9 Nov  5 15:08 123 -> /root/123
    drwxr-xr-x 2 root root 6 Nov  5 15:05 1234

file.touch

创建空文件或更新时间戳

[root@master ~]# salt 'minion' file.touch /root/a
minion:
    True
[root@master ~]# salt 'minion' cmd.run 'ls -l /root/'
minion:
    total 8
    -rw-r--r--  1 root root    0 Nov  5 15:05 123
    drwxr-xr-x  2 root root    6 Nov  5 15:05 1234
    -rw-r--r--  1 root root    0 Nov  5 15:09 a
    -rw-r--r--  1 root root    6 Nov  4 15:01 abc
    -rw-------. 1 root root 1258 Oct 30 22:20 anaconda-ks.cfg
    drwxr-xr-x. 4 root root   34 Nov  3 21:19 rpmbuild

file.uid_to_user

将指定的 uid 转换成用户名显示出来

[root@master ~]# salt 'minion' file.uid_to_user 0
minion:
    root
[root@master ~]# salt 'minion' file.uid_to_user 1000
minion:
    zhuzhu

file.write

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

[root@master ~]# salt 'minion' file.write /root/123 "abcd"
minion:
    Wrote 1 lines to "/root/123"
[root@master ~]# salt 'minion' cmd.run 'cat /root/123'
minion:
    abcd

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值