SaltStack常用的模块

SaltStack常用的模块

文章目录

1. SaltStack模块介绍

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

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

1.1 查看所有module列表
[root@master ~]# salt 'node2' sys.list_modules
node2:
    - acl
    - aliases
    - alternatives
    - apache
    - 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
    - webutil
    - x509
    - xfs
    - xml
    - zabbix
    - zenoss
[root@master ~]# 

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

3 查看指定module的用法
[root@master ~]# salt 'node2' sys.doc cmd
cmd.exec_code:

    Pass in two strings, the first naming the executable language, aka -
    python2, python3, ruby, perl, lua, etc. the second string containing
    the code you wish to execute. The stdout will be returned.

    All parameters from :mod:`cmd.run_all <salt.modules.cmdmod.run_all>` except python_shell can be used.

    CLI Example:

        salt '*' cmd.exec_code ruby 'puts "cheese"'
        salt '*' cmd.exec_code ruby 'puts "cheese"' args='["arg1", "arg2"]' env='{"FOO": "bar"}'
    



//SaltStack默认也支持一次执行多个Module,Module之间通过逗号隔开,默认传参之间也是用逗号分隔,也支持指定传参分隔符号--args-separator=@即可
[root@master ~]# salt 'node2' test.echo,cmd.run,service.status shunzi,date,nginx
node2:
    ----------
    cmd.run:
        Wed Nov  3 06:27:46 EDT 2021
    service.status:
        False
    test.echo:
        shunzi
[root@master ~]# 

2 SaltStack常用模块

2.1 SaltStack常用模块之network

2.1.1 network.active_tcp

返回所有活动的tcp连接

[root@master ~]# salt 'node2' network.active_tcp
node2:
    ----------
    0:
        ----------
        local_addr:
            192.168.200.153
        local_port:
            42178
        remote_addr:
            192.168.200.145
        remote_port:
            4505
    1:
        ----------
        local_addr:
            192.168.200.153
        local_port:
            22
        remote_addr:
            192.168.200.1
        remote_port:
            63818
    2:
        ----------
        local_addr:
            192.168.200.153
        local_port:
            22
        remote_addr:
            192.168.200.1
        remote_port:
            63821
[root@master ~]# 



[root@node2 ~]# ss -antl
State  Recv-Q Send-Q Local Address:Port   Peer Address:Port Process 
LISTEN 0      128          0.0.0.0:22          0.0.0.0:*            
LISTEN 0      128                *:80                *:*            
LISTEN 0      128             [::]:22             [::]:*            
[root@node2 ~]# 

2.1.2 network.calc_net

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

[root@master ~]# salt 'node2' network.calc_net 192.168.200.153 255.255.255.0
node2:
    192.168.200.0/24


[root@master ~]# salt 'node2' network.calc_net 192.168.200.144 255.255.255.0
node2:
    192.168.200.0/24
[root@master ~]# 

2.1.3 network.connect

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

[root@master ~]# salt 'node2' network.connect baidu.com 80
node2:
    ----------
    comment:
        Successfully connected to baidu.com (220.181.38.148) on tcp port 80
    result:
        True
[root@master ~]# 


[root@master ~]# salt 'node2' network.connect 192.168.200.144 22
node2:
    ----------
    comment:
        Successfully connected to 192.168.200.144 (192.168.200.144) on tcp port 22
    result:
        True
[root@master ~]# 

2.1.4 network.default_route

查看默认路由

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


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

2.1.5 network.get_fqdn

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

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


[root@master ~]# salt '192.168.200.144' network.get_fqdn
192.168.200.144:
    node1
[root@master ~]# 

2.1.6 network.get_hostname

获取主机名

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

2.1.7 network.get_route

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

[root@master ~]# salt '192.168.200.144' network.get_route 192.168.200.153
192.168.200.144:
    ----------
    destination:
        192.168.200.153
    gateway:
        None
    interface:
        ens33
    source:
        192.168.200.144
[root@master ~]# 


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

2.1.8 network.hw_addr

返回指定网卡的MAC地址

[root@master ~]# salt '192.168.200.144' network.hw_addr ens33
192.168.200.144:
    00:0c:29:3e:b6:07


[root@master ~]# salt '192.168.200.144' network.hw_addr lo
192.168.200.144:
    00:00:00:00:00:00
[root@master ~]# 

2.1.9 network.ifacestartswith

从特定CIDR检索接口名称

[root@master ~]# salt '192.168.200.144' network.ifacestartswith 192.168
192.168.200.144:
    - ens33
[root@master ~]# 

2.1.10 network.in_subnet

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

[root@master ~]# salt '192.168.200.144' network.in_subnet 192.168.0.0/24
192.168.200.144:
    False
    
[root@master ~]# salt '192.168.200.144' network.in_subnet 192.168.0.0/16
192.168.200.144:
    True
    
[root@master ~]# salt '192.168.200.144' network.in_subnet 192.168.0.0/32
192.168.200.144:
    False
[root@master ~]# 

2.1.11 network.interface

返回指定网卡的信息

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



[root@master ~]# salt '192.168.200.144' network.interface lo
192.168.200.144:
    |_
      ----------
      address:
          127.0.0.1
      broadcast:
          None
      label:
          lo
      netmask:
          255.0.0.0
[root@master ~]# 

2.1.12 network.interface_ip

返回指定网卡的IP地址

[root@master ~]# salt '192.168.200.144' network.interface_ip ens33
192.168.200.144:
    192.168.200.144
    
[root@master ~]# salt '192.168.200.144' network.interface_ip lo
192.168.200.144:
    127.0.0.1
[root@master ~]# 

2.1.13 network.interfaces

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

[root@master ~]# salt '192.168.200.144' network.interfaces
192.168.200.144:
    ----------
    ens33:
        ----------
        hwaddr:
            00:0c:29:3e:b6:07
        inet:
            |_
              ----------
              address:
                  192.168.200.144
              broadcast:
                  192.168.200.255
              label:
                  ens33
              netmask:
                  255.255.255.0
        inet6:
            |_
              ----------
              address:
                  fe80::a544:8217:9027:be94
              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 ~]# 

2.1.14 network.ip_addrs

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

[root@master ~]# salt '192.168.200.144' network.ip_addrs
192.168.200.144:
    - 192.168.200.144
    
[root@master ~]# salt  'node2' network.ip_addrs
node2:
    - 192.168.200.153
[root@master ~]# 

//在node1被控机上添加一个临时IP
[root@node1 ~]# ip addr add 192.168.200.140/24 dev ens33
[root@node1 ~]# 
[root@master ~]# salt '192.168.200.144' network.ip_addrs
192.168.200.144:
    - 192.168.200.140
    - 192.168.200.144
[root@master ~]# 


2.1.15 network.netstat

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

[root@master ~]# salt '192.168.200.144' network.netstat
192.168.200.144:
    |_
      ----------
      inode:
          28637
      local-address:
          192.168.200.144%ens33:68
      program:
          NetworkManager
      proto:
          udp
      recv-q:
          0
      remote-address:
          192.168.200.254:67
      send-q:
          0
      user:
          0
    |_
      ----------
      inode:
          26708
      local-address:
          127.0.0.1:323
      program:
          chronyd
      proto:
          udp
      recv-q:
          0
      remote-address:
          0.0.0.0:*
      send-q:
          0
      user:
          0
    |_
      ----------
      inode:
          26709
      local-address:
          [::1]:323
      program:
          chronyd
      proto:
          udp
      recv-q:
          0
      remote-address:
          [::]:*
      send-q:
          0
      user:
          0
    |_
      ----------
      inode:
          26922
      local-address:
          0.0.0.0:22
      program:
          sshd
      proto:
          tcp
      recv-q:
          0
      remote-address:
          0.0.0.0:*
      send-q:
          128
      state:
          LISTEN
      user:
          0
    |_
      ----------
      inode:
          28733
      local-address:
          0.0.0.0:80
      program:
          nginx
      proto:
          tcp
      recv-q:
          0
      remote-address:
          0.0.0.0:*
      send-q:
          128
      state:
          LISTEN
      user:
          0
    |_
      ----------
      inode:
          30574
      local-address:
          192.168.200.144:44760
      program:
          salt-minion
      proto:
          tcp
      recv-q:
          0
      remote-address:
          192.168.200.145:4505
      send-q:
          0
      state:
          ESTABLISHED
      user:
          0
    |_
      ----------
      inode:
          32045
      local-address:
          192.168.200.144:22
      program:
          sshd
      proto:
          tcp
      recv-q:
          0
      remote-address:
          192.168.200.1:64378
      send-q:
          0
      state:
          ESTABLISHED
      user:
          0
    |_
      ----------
      inode:
          32824
      local-address:
          192.168.200.144:22
      program:
          sshd
      proto:
          tcp
      recv-q:
          0
      remote-address:
          192.168.200.1:64379
      send-q:
          0
      state:
          ESTABLISHED
      user:
          0
    |_
      ----------
      inode:
          26933
      local-address:
          [::]:22
      program:
          sshd
      proto:
          tcp
      recv-q:
          0
      remote-address:
          [::]:*
      send-q:
          128
      state:
          LISTEN
      user:
          0
    |_
      ----------
      inode:
          28734
      local-address:
          [::]:80
      program:
          nginx
      proto:
          tcp
      recv-q:
          0
      remote-address:
          [::]:*
      send-q:
          128
      state:
          LISTEN
      user:
          0
[root@master ~]# 

2.1.16 network.ping

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

[root@master ~]# salt '192.168.200.144' network.ping baidu.com
192.168.200.144:
    PING baidu.com (220.181.38.251) 56(84) bytes of data.
    64 bytes from 220.181.38.251 (220.181.38.251): icmp_seq=1 ttl=128 time=40.4 ms
    64 bytes from 220.181.38.251 (220.181.38.251): icmp_seq=2 ttl=128 time=41.0 ms
    64 bytes from 220.181.38.251 (220.181.38.251): icmp_seq=3 ttl=128 time=42.8 ms
    64 bytes from 220.181.38.251 (220.181.38.251): icmp_seq=4 ttl=128 time=40.2 ms
    
    --- baidu.com ping statistics ---
    4 packets transmitted, 4 received, 0% packet loss, time 3006ms
    rtt min/avg/max/mdev = 40.169/41.095/42.791/1.055 ms
[root@master ~]# 

2.1.17 network.reverse_ip

返回一个指定的IP地址的反向地址

[root@master ~]# salt '192.168.200.144' network.reverse_ip 192.168.200.144
192.168.200.144:
    144.200.168.192.in-addr.arpa
[root@master ~]# 

2.2 SaltStack常用模块之service

2.2.1 service.available

判断指定的服务是否可用

[root@master ~]# salt '192.168.200.144' network.reverse_ip 192.168.200.144
192.168.200.144:
    144.200.168.192.in-addr.arpa
[root@master ~]# salt '192.168.200.144' service.available nginx
192.168.200.144:
    True
[root@master ~]# salt '192.168.200.144' service.available sshd
192.168.200.144:
    True
[root@master ~]# 

2.2.2 service.get_all

获取所有正在运行的服务

[root@master ~]# salt '192.168.200.144' service.get_all
192.168.200.144:
    - NetworkManager
    - NetworkManager-dispatcher
    - NetworkManager-wait-online
    - auditd
    - autovt@
    - basic.target
    - blk-availability
    - bluetooth.target
    - boot-complete.target
    - chrony-dnssrv@
    - chrony-dnssrv@.timer
    - chrony-wait
    - chronyd
    - 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
    - dev-hugepages.mount
    - dev-mqueue.mount
    - dm-event
    - dm-event.socket
    - dnf-makecache
    - dnf-makecache.timer
    - dracut-cmdline
    - dracut-initqueue
    - dracut-mount
    - dracut-pre-mount
    - dracut-pre-pivot
    - dracut-pre-trigger
    - dracut-pre-udev
    - dracut-shutdown
    - ebtables
    - emergency
    - emergency.target
    - exit.target
    - final.target
    - firewalld
    - fstrim
    - fstrim.timer
    - getty-pre.target
    - getty.target
    - getty@
    - graphical.target
    - grub-boot-indeterminate
    - halt-local
    - halt.target
    - hibernate.target
    - hybrid-sleep.target
    - import-state
    - initrd-cleanup
    - initrd-fs.target
    - initrd-parse-etc
    - initrd-root-device.target
    - initrd-root-fs.target
    - initrd-switch-root
    - initrd-switch-root.target
    - initrd-udevadm-cleanup-db
    - initrd.target
    - iprdump
    - iprinit
    - iprupdate
    - iprutils.target
    - irqbalance
    - kdump
    - kexec.target
    - kmod-static-nodes
    - ldconfig
    - loadmodules
    - local-fs-pre.target
    - local-fs.target
    - lvm2-lvmpolld
    - lvm2-lvmpolld.socket
    - lvm2-monitor
    - lvm2-pvscan@
    - man-db-cache-update
    - messagebus
    - microcode
    - multi-user.target
    - network-online.target
    - network-pre.target
    - network.target
    - nftables
    - nginx
    - nis-domainname
    - nss-lookup.target
    - nss-user-lookup.target
    - paths.target
    - plymouth-halt
    - plymouth-kexec
    - plymouth-poweroff
    - plymouth-quit
    - plymouth-quit-wait
    - plymouth-read-write
    - plymouth-reboot
    - plymouth-start
    - plymouth-switch-root
    - plymouth-switch-root-initramfs
    - polkit
    - poweroff.target
    - printer.target
    - proc-sys-fs-binfmt_misc.automount
    - proc-sys-fs-binfmt_misc.mount
    - quotaon
    - rc-local
    - rdisc
    - reboot.target
    - remote-cryptsetup.target
    - remote-fs-pre.target
    - remote-fs.target
    - rescue
    - rescue.target
    - rhsm
    - rhsm-facts
    - rhsmcertd
    - rngd
    - rngd-wake-threshold
    - rpcbind.target
    - rsyslog
    - run-vmblock\x2dfuse.mount
    - runlevel0.target
    - runlevel1.target
    - runlevel2.target
    - runlevel3.target
    - runlevel4.target
    - runlevel5.target
    - runlevel6.target
    - salt-minion
    - salt-proxy@
    - selinux-autorelabel
    - selinux-autorelabel-mark
    - selinux-autorelabel.target
    - serial-getty@
    - shutdown.target
    - sigpwr.target
    - sleep.target
    - slices.target
    - smartcard.target
    - sockets.target
    - sound.target
    - sshd
    - sshd-keygen.target
    - sshd-keygen@
    - sshd.socket
    - sshd@
    - sssd
    - sssd-autofs
    - sssd-autofs.socket
    - sssd-kcm
    - sssd-kcm.socket
    - sssd-nss
    - sssd-nss.socket
    - sssd-pac
    - sssd-pac.socket
    - sssd-pam
    - sssd-pam-priv.socket
    - sssd-pam.socket
    - sssd-ssh
    - sssd-ssh.socket
    - sssd-sudo
    - sssd-sudo.socket
    - suspend-then-hibernate.target
    - suspend.target
    - swap.target
    - sys-fs-fuse-connections.mount
    - sys-kernel-config.mount
    - sys-kernel-debug.mount
    - sysinit.target
    - syslog
    - syslog.socket
    - system-update-cleanup
    - system-update-pre.target
    - system-update.target
    - systemd-ask-password-console
    - systemd-ask-password-console.path
    - systemd-ask-password-plymouth
    - systemd-ask-password-plymouth.path
    - systemd-ask-password-wall
    - systemd-ask-password-wall.path
    - systemd-backlight@
    - systemd-binfmt
    - systemd-coredump.socket
    - systemd-coredump@
    - systemd-exit
    - systemd-firstboot
    - systemd-fsck-root
    - systemd-fsck@
    - systemd-halt
    - systemd-hibernate
    - systemd-hibernate-resume@
    - systemd-hostnamed
    - systemd-hwdb-update
    - systemd-hybrid-sleep
    - systemd-initctl
    - systemd-initctl.socket
    - systemd-journal-catalog-update
    - systemd-journal-flush
    - systemd-journald
    - systemd-journald-audit.socket
    - systemd-journald-dev-log.socket
    - systemd-journald.socket
    - systemd-kexec
    - systemd-localed
    - systemd-logind
    - systemd-machine-id-commit
    - systemd-modules-load
    - systemd-portabled
    - systemd-poweroff
    - systemd-quotacheck
    - systemd-random-seed
    - systemd-reboot
    - systemd-remount-fs
    - systemd-resolved
    - systemd-rfkill
    - systemd-rfkill.socket
    - systemd-suspend
    - systemd-suspend-then-hibernate
    - systemd-sysctl
    - systemd-sysusers
    - systemd-timedated
    - systemd-tmpfiles-clean
    - systemd-tmpfiles-clean.timer
    - systemd-tmpfiles-setup
    - systemd-tmpfiles-setup-dev
    - systemd-udev-settle
    - systemd-udev-trigger
    - systemd-udevd
    - systemd-udevd-control.socket
    - systemd-udevd-kernel.socket
    - systemd-update-done
    - systemd-update-utmp
    - systemd-update-utmp-runlevel
    - systemd-user-sessions
    - systemd-vconsole-setup
    - systemd-volatile-root
    - tcsd
    - teamd@
    - time-sync.target
    - timedatex
    - timers.target
    - tmp.mount
    - tuned
    - umount.target
    - unbound-anchor
    - unbound-anchor.timer
    - user-runtime-dir@
    - user@
    - vgauthd
    - vmtoolsd
[root@master ~]# 

2.2.3 service.disabled

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

[root@master ~]# salt '192.168.200.144' service.disabled nginx
192.168.200.144:
    False
[root@master ~]# 


[root@node1 ~]# ip addr add 192.168.200.140/24 dev ens33
[root@node1 ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; >
   Active: active (running) since Wed 2021-11-03 18:40:37 CST; 33mi>
  Process: 986 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUC>
  Process: 964 ExecStartPre=/usr/sbin/nginx -t (code=exited, status>
  Process: 958 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exi>
 Main PID: 1004 (nginx)
    Tasks: 5 (limit: 49290)
   Memory: 16.4M
   CGroup: /system.slice/nginx.service
           ├─1004 nginx: master process /usr/sbin/nginx
           ├─1007 nginx: worker process
           ├─1008 nginx: worker process
           ├─1009 nginx: worker process
           └─1012 nginx: worker process

11月 03 18:40:37 node1 systemd[1]: Starting The nginx HTTP and reve>
11月 03 18:40:37 node1 nginx[964]: nginx: the configuration file /e>
11月 03 18:40:37 node1 nginx[964]: nginx: configuration file /etc/n>
11月 03 18:40:37 node1 systemd[1]: nginx.service: Failed to parse P>
11月 03 18:40:37 node1 systemd[1]: Started The nginx HTTP and rever>
lines 1-21/21 (END)

2.2.4 service.enabled

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

[root@master ~]# salt '192.168.200.144' service.enabled nginx
192.168.200.144:
    True
[root@master ~]# 


[root@node1 ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; >
   Active: active (running) since Wed 2021-11-03 18:40:37 CST; 34mi>
  Process: 986 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUC>
  Process: 964 ExecStartPre=/usr/sbin/nginx -t (code=exited, status>
  Process: 958 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exi>
 Main PID: 1004 (nginx)
    Tasks: 5 (limit: 49290)
   Memory: 16.4M
   CGroup: /system.slice/nginx.service
           ├─1004 nginx: master process /usr/sbin/nginx
           ├─1007 nginx: worker process
           ├─1008 nginx: worker process
           ├─1009 nginx: worker process
           └─1012 nginx: worker process

11月 03 18:40:37 node1 systemd[1]: Starting The nginx HTTP and reve>
11月 03 18:40:37 node1 nginx[964]: nginx: the configuration file /e>
11月 03 18:40:37 node1 nginx[964]: nginx: configuration file /etc/n>
11月 03 18:40:37 node1 systemd[1]: nginx.service: Failed to parse P>
11月 03 18:40:37 node1 systemd[1]: Started The nginx HTTP and rever>
lines 1-21/21 (END)

2.2.5 service.disable

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

[root@master ~]# salt '192.168.200.144' service.disable nginx
192.168.200.144:
    True
[root@master ~]# salt '192.168.200.144' service.enabled nginx
192.168.200.144:
    False
[root@master ~]# 



[root@node1 ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled;>
   Active: active (running) since Wed 2021-11-03 18:40:37 CST; 36mi>
 Main PID: 1004 (nginx)
    Tasks: 5 (limit: 49290)
   Memory: 16.4M
   CGroup: /system.slice/nginx.service
           ├─1004 nginx: master process /usr/sbin/nginx
           ├─1007 nginx: worker process
           ├─1008 nginx: worker process
           ├─1009 nginx: worker process
           └─1012 nginx: worker process

11月 03 18:40:37 node1 systemd[1]: Starting The nginx HTTP and reve>
11月 03 18:40:37 node1 nginx[964]: nginx: the configuration file /e>
11月 03 18:40:37 node1 nginx[964]: nginx: configuration file /etc/n>
11月 03 18:40:37 node1 systemd[1]: nginx.service: Failed to parse P>
11月 03 18:40:37 node1 systemd[1]: Started The nginx HTTP and rever>
lines 1-18/18 (END)

2.2.6 service.enable

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

[root@master ~]# salt '192.168.200.144' service.enable nginx
192.168.200.144:
    True
[root@master ~]# salt '192.168.200.144' service.enabled nginx
192.168.200.144:
    True
[root@master ~]# 



[root@node1 ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; >
   Active: active (running) since Wed 2021-11-03 18:40:37 CST; 38mi>
 Main PID: 1004 (nginx)
    Tasks: 5 (limit: 49290)
   Memory: 16.4M
   CGroup: /system.slice/nginx.service
           ├─1004 nginx: master process /usr/sbin/nginx
           ├─1007 nginx: worker process
           ├─1008 nginx: worker process
           ├─1009 nginx: worker process
           └─1012 nginx: worker process

11月 03 18:40:37 node1 systemd[1]: Starting The nginx HTTP and reve>
11月 03 18:40:37 node1 nginx[964]: nginx: the configuration file /e>
11月 03 18:40:37 node1 nginx[964]: nginx: configuration file /etc/n>
11月 03 18:40:37 node1 systemd[1]: nginx.service: Failed to parse P>
11月 03 18:40:37 node1 systemd[1]: Started The nginx HTTP and rever>
lines 1-18/18 (END)

2.2.7 service.reload

重新加载指定服务

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

2.2.8 service.stop

停止指定服务

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



[root@node1 ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; >
   Active: inactive (dead) since Wed 2021-11-03 19:21:25 CST; 21s a>
  Process: 62910 ExecReload=/bin/kill -s HUP $MAINPID (code=exited,>
 Main PID: 1004 (code=exited, status=0/SUCCESS)

11月 03 18:40:37 node1 systemd[1]: Starting The nginx HTTP and reve>
11月 03 18:40:37 node1 nginx[964]: nginx: the configuration file /e>
11月 03 18:40:37 node1 nginx[964]: nginx: configuration file /etc/n>
11月 03 18:40:37 node1 systemd[1]: nginx.service: Failed to parse P>
11月 03 18:40:37 node1 systemd[1]: Started The nginx HTTP and rever>
11月 03 19:19:25 node1 systemd[1]: Reloading The nginx HTTP and rev>
11月 03 19:19:25 node1 systemd[1]: Reloaded The nginx HTTP and reve>
11月 03 19:21:25 node1 systemd[1]: Stopping The nginx HTTP and reve>
11月 03 19:21:25 node1 systemd[1]: nginx.service: Succeeded.
11月 03 19:21:25 node1 systemd
2.2.9 service.start

启动指定服务

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


[root@node1 ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; >
   Active: active (running) since Wed 2021-11-03 19:22:28 CST; 36s >
  Process: 62910 ExecReload=/bin/kill -s HUP $MAINPID (code=exited,>
  Process: 67934 ExecStart=/usr/sbin/nginx (code=exited, status=0/S>
  Process: 67932 ExecStartPre=/usr/sbin/nginx -t (code=exited, stat>
  Process: 67930 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=e>
 Main PID: 67935 (nginx)
    Tasks: 5 (limit: 49290)
   Memory: 8.8M
   CGroup: /system.slice/nginx.service
           ├─67935 nginx: master process /usr/sbin/nginx
           ├─67936 nginx: worker process
           ├─67937 nginx: worker process
           ├─67938 nginx: worker process
           └─67939 nginx: worker process

11月 03 19:22:28 node1 systemd[1]: Starting The nginx HTTP and reve>
11月 03 19:22:28 node1 nginx[67932]: nginx: the configuration file >
11月 03 19:22:28 node1 nginx[67932]: nginx: configuration file /etc>
11月 03 19:22:28 node1 systemd[1]: Started The nginx HTTP and rever>
lines 1-21/21 (END)

2.2.10 service.restart

重启指定服务

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


2.2.11 service.status

查看指定服务的状态

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

2.2.12 service.missing

查看指定服务是否存在

[root@master ~]# salt '192.168.200.144' service.missing nginx
192.168.200.144:
    False
[root@master ~]# 


[root@master ~]# salt '192.168.200.144' service.missing httpd
192.168.200.144:
    True
[root@master ~]# 

2.3 SaltStack常用模块之pkg

2.3.1 pkg.download

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

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



[root@node1 ~]# ls /var/cache/yum/packages/
wget-1.19.5-10.el8.x86_64.rpm
[root@node1 ~]# 

2.3.2 pkg.file_list

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

//列出已安装的nginx软件包提供的所有文件
[root@master ~]# salt '192.168.200.144' pkg.file_list nginx
192.168.200.144:
    ----------
    errors:
    files:
        - /etc/logrotate.d/nginx
        - /etc/nginx/fastcgi.conf
        - /etc/nginx/fastcgi.conf.default
        - /etc/nginx/fastcgi_params
        - /etc/nginx/fastcgi_params.default
        - /etc/nginx/koi-utf
        - /etc/nginx/koi-win
        - /etc/nginx/mime.types
        - /etc/nginx/mime.types.default
        - /etc/nginx/nginx.conf
        - /etc/nginx/nginx.conf.default
        - /etc/nginx/scgi_params
        - /etc/nginx/scgi_params.default
        - /etc/nginx/uwsgi_params
        - /etc/nginx/uwsgi_params.default
        - /etc/nginx/win-utf
        - /usr/bin/nginx-upgrade
        - /usr/lib/.build-id
        - /usr/lib/.build-id/2d
        - /usr/lib/.build-id/2d/da6018ae12edb856ad3d2cf61bf586b6b4873c
        - /usr/lib/systemd/system/nginx.service
        - /usr/lib64/nginx/modules
        - /usr/sbin/nginx
        - /usr/share/doc/nginx
        - /usr/share/doc/nginx/CHANGES
        - /usr/share/doc/nginx/README
        - /usr/share/doc/nginx/README.dynamic
        - /usr/share/licenses/nginx
        - /usr/share/licenses/nginx/LICENSE
        - /usr/share/man/man3/nginx.3pm.gz
        - /usr/share/man/man8/nginx-upgrade.8.gz
        - /usr/share/man/man8/nginx.8.gz
        - /usr/share/nginx/html/404.html
        - /usr/share/nginx/html/50x.html
        - /usr/share/nginx/html/index.html
        - /usr/share/nginx/html/nginx-logo.png
        - /usr/share/nginx/html/poweredby.png
        - /usr/share/vim/vimfiles/ftdetect/nginx.vim
        - /usr/share/vim/vimfiles/indent/nginx.vim
        - /usr/share/vim/vimfiles/syntax/nginx.vim
        - /var/lib/nginx
        - /var/lib/nginx/tmp
        - /var/log/nginx
[root@master ~]# 
//当不提供参数时,将会列出当前系统中所有已安装软件的文件列表
2.3.3 pkg.group_list

列出系统中所有的包组

[root@master ~]# salt '192.168.200.144' pkg.group_list
192.168.200.144:
    ----------
    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
        - Development Tools
        - 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
    available environments:
        - Server with GUI
        - Server
        - Workstation
        - Virtualization Host
        - Custom Operating System
    available languages:
        ----------
    installed:
        - VMware platform specific packages
        - Core
    installed environments:
        - Minimal Install
[root@master ~]# 

2.3.4 pkg.group_info

查看包组的信息

[root@master ~]# salt '192.168.200.144' pkg.group_info 'Development Tools'
192.168.200.144:
    ----------
    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
[root@master ~]# 

2.3.5 pkg.install

安装软件

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


[root@master ~]# salt '192.168.200.144' cmd.run 'rpm -q wget'
192.168.200.144:
    wget-1.19.5-10.el8.x86_64
[root@master ~]# 


2.3.6 pkg.list_downloaded

列出已下载到本地的软件包(本机上的软件包)

[root@master ~]# salt '192.168.200.144' pkg.list_downloaded
192.168.200.144:
    ----------
    wget:
        ----------
        1.14-18.el7:
            ----------
            creation_date_time:
                2019-02-22T17:11:52
            creation_date_time_t:
                1550826712
            path:
                /var/cache/yum/packages/wget-1.14-18.el7.x86_64.rpm
            size:
                560152

2.3.7 pkg.list_pkgs

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

[root@master ~]# salt '192.168.200.144' pkg.list_pkgs
192.168.200.144:
    ----------
    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
    at-spi2-atk:
        2.26.2-1.el8
    at-spi2-core:
        2.28.0-1.el8
    atk:
        2.28.1-1.el8

...此处省略N行

2.3.8 pkg.owner

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

 //要使用绝对路径
[root@master ~]# salt '192.168.200.144' pkg.owner /usr/sbin/nginx
192.168.200.144:
    nginx

//查看文件由那个软件包提供
[root@master ~]# salt '192.168.200.144' pkg.owner /etc/passwd
192.168.200.144:
    setup
[root@master ~]# 

2.3.9 pkg.remove

卸载指定软件

[root@master ~]# salt '192.168.200.144' cmd.run 'rpm -qa|grep wget'
192.168.200.144:
    wget-1.19.5-10.el8.x86_64
    
[root@master ~]# salt '192.168.200.144' pkg.remove wget
192.168.200.144:
    ----------
    wget:
        ----------
        new:
        old:
            1.19.5-10.el8
[root@master ~]# salt '192.168.200.144' cmd.run 'rpm -qa|grep wget'
192.168.200.144:

ERROR: Minions returned with non-zero exit code
[root@master ~]# 
/若要卸载多个文件,中间需要用逗号隔开
2.3.10 pkg.upgrade

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

[root@master ~]# salt '192.168.200.144' cmd.run 'rpm -qa|grep wget'
192.168.200.144:
    wget-1.19.5-10.el8.x86_64
[root@master ~]# salt '192.168.200.144' pkg.upgrade name=wget
192.168.200.144:
    ----------
    wget:
        ----------
        new:
            1.19.5-10.el8
        old:
            1.14-18.el7_6.1
//若想升级系统中所有的软件包则把 name 参数去掉即可
2.3.11 pkg.group_install

安装包组

[root@master ~]# salt '192.168.200.144' pkg.group_install 'Development Tools'
192.168.25.147:
    ----------
    adobe-mappings-cmap:
        ----------
        new:
            20171205-3.el8
        old:
    adobe-mappings-cmap-deprecated:
        ----------
        new:
            20171205-3.el8
        old:
    adobe-mappings-pdf:
        ----------
        new:
            20180407-1.el8
        old:
    annobin:
        ----------
        new:
            10.15-2.el8
        old:
    asciidoc:
        ----------
        new:
            8.6.10-0.5.20180627gitf7c2274.el8
        old:
    autoconf:
        ----------
        new:
            2.69-29.el8
        old:
    automake:
        ----------
        new:
            1.16.1-7.el8
        old:
    binutils:
        ----------
        new:
            2.30-108.el8
        old:
    bison:
        ----------
        new:
            3.0.4-10.el8
        old:
    boost-atomic:
        ----------
        new:
            1.66.0-10.el8
        old:


//如果安装多个包组,包组之间使用‘,’隔开

2.4 SaltStack常用模块之state

2.4.1 state.show_highstate

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

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

2.4.2 state.highstate

执行高级状态

[root@master ~]# salt '192.168.200.144' state.highstate web.apache.install
192.168.200.144:
----------
          ID: nginx-install
    Function: pkg.installed
        Name: nginx
      Result: True
     Comment: All specified packages are already installed
     Started: 19:49:27.239847
    Duration: 783.405 ms
     Changes:   
----------
          ID: nginx-service
    Function: service.running
        Name: nginx
      Result: True
     Comment: The service nginx is already running
     Started: 19:49:28.027918
    Duration: 47.764 ms
     Changes:   

Summary for 192.168.200.144
------------
Succeeded: 2
Failed:    0
------------
Total states run:     2
Total run time: 831.169 ms
[root@master ~]# 

2.4.3 state.show_state_usage

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

[root@master ~]# salt '*' state.show_state_usage
node2:
    Passed invalid arguments to state.show_state_usage: argument of type 'NoneType' is not iterable
    
        Retrieve the highstate data from the salt master to analyse used and unused states
    
        Custom Pillar data can be passed with the ``pillar`` kwarg.
    
        CLI Example:
    
        .. code-block:: bash
    
            salt '*' state.show_state_usage
        
master:
    Passed invalid arguments to state.show_state_usage: argument of type 'NoneType' is not iterable
    
        Retrieve the highstate data from the salt master to analyse used and unused states
    
        Custom Pillar data can be passed with the ``pillar`` kwarg.
    
        CLI Example:
    
        .. code-block:: bash
    
            salt '*' state.show_state_usage
        
192.168.200.144:
    ----------
    base:
        ----------
        count_all:
            3
        count_unused:
            2
        count_used:
            1
        unused:
            - top
            - web.apache.install
        used:
            - web.nginx.install
    dev:
        ----------
        count_all:
            0
        count_unused:
            0
        count_used:
            0
        unused:
        used:
    prod:
        ----------
        count_all:
            0
        count_unused:
            0
        count_used:
            0
        unused:
        used:
    test:
        ----------
        count_all:
            0
        count_unused:
            0
        count_used:
            0
        unused:
        used:

2.4.4 state.show_top

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

[root@master ~]# salt '*' state.show_top
192.168.200.144:
    ----------
    base:
        - web.nginx.install
master:
    ----------
node2:
    ----------
[root@master ~]# 

2.4.5 state.top

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

[root@master base]# salt '192.168.200.144' state.top top.sls
192.168.200.144:
----------
          ID: nginx-install
    Function: pkg.installed
        Name: nginx
      Result: True
     Comment: All specified packages are already installed
     Started: 19:53:11.703731
    Duration: 871.135 ms
     Changes:   
----------
          ID: nginx-service
    Function: service.running
        Name: nginx
      Result: True
     Comment: The service nginx is already running
     Started: 19:53:12.576870
    Duration: 42.662 ms
     Changes:   

Summary for 192.168.200.144
------------
Succeeded: 2
Failed:    0
------------
Total states run:     2
Total run time: 913.797 ms
[root@master base]# 

2.4.6 state.show_sls

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

[root@master base]# salt '192.168.200.144' state.show_sls web.apache.install
192.168.200.144:
    ----------
    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
[root@master base]# 

2.5 SaltStack常用模块之user

2.5.1 user.add

创建用户

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


[root@master base]# salt '192.168.200.144' user.add tom 555
192.168.200.144:
    True
[root@master base]# salt '192.168.200.144' cmd.run 'id tom'
192.168.200.144:
    uid=555(tom) gid=1001(tom) groups=1001(tom)
[root@master base]# 

2.5.2 user.chfullname

更改用户的全名(对用户进行描述)

[root@master base]# salt '192.168.200.144' user.chfullname tom "无效用户"
192.168.200.144:
    True
[root@master base]# salt '192.168.200.144' cmd.run 'cat /etc/passwd | tail -1'
192.168.200.144:
    tom:x:555:1001:无效用户:/home/tom:/bin/bash
[root@master base]# 

2.5.3 user.chuid

为指定用户更改 uid

[root@master base]# salt '192.168.200.144' cmd.run 'id tom'
192.168.200.144:
    uid=555(tom) gid=1001(tom) groups=1001(tom)
[root@master base]# salt '192.168.200.144' user.chuid tom 556
192.168.200.144:
    True
[root@master base]# salt '192.168.200.144' cmd.run 'id tom'
192.168.200.144:
    uid=556(tom) gid=1001(tom) groups=1001(tom)
[root@master base]# 

2.5.4 user.chgid

更改用户GID(强制使用 GID 作为新的主要组)

[root@master base]# salt '192.168.200.144' cmd.run 'id tom'
192.168.200.144:
    uid=556(tom) gid=1001(tom) groups=1001(tom)

[root@master base]# salt '192.168.200.144' user.chgid tom 995
192.168.200.144:
    True
[root@master base]# 

[root@master base]# salt '192.168.200.144' cmd.run 'id tom'
192.168.200.144:
    uid=556(tom) gid=995(ssh_keys) groups=995(ssh_keys)
[root@master base]# 
2.5.5 user.chgroups

更改此用户所属的组(也可添加附加组)

[root@master base]# salt '192.168.200.144' user.chgroups tom root,tom True
192.168.200.144:
    True
[root@master base]# salt '192.168.200.144' cmd.run 'id tom'
192.168.200.144:
    uid=556(tom) gid=995(ssh_keys) groups=995(ssh_keys),1001(tom),0(root)
[root@master base]# 

2.5.6 user.chhome

更改用户的家居目录,如果旧主页目录存在,则通过 True 将文件移动到新家庭目录

[root@master ~]# salt '192.168.200.144' user.chhome tom /home/users/tom True
192.168.200.144:
    True
[root@master ~]# salt '192.168.200.144' cmd.run 'cat /etc/passwd | tail -1'
192.168.200.144:
    tom:x:556:995:无效用户:/home/users/tom:/bin/bash
[root@master ~]# 

2.5.7 user.chshell

修改用户的默认shell

[root@master ~]# salt '192.168.200.144' user.chshell tom /bin/haha
192.168.200.144:
    True
[root@master ~]# 

2.5.8 user.list_groups

返回指定用户所属的组列表

[root@master ~]# salt '192.168.200.144' user.list_groups tom
192.168.200.144:
    - root
    - ssh_keys
    - tom
[root@master ~]# 

2.5.9 user.list_users

返回所有用户的列表

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

2.5.10 user.rename

修改命名用户的用户名

[root@master ~]# salt '192.168.200.144' user.rename tom jeery
192.168.200.144:
    False
[root@master ~]# salt '192.168.200.144' cmd.run 'id jeery'
192.168.200.144:
    uid=556(jeery) gid=995(ssh_keys) groups=995(ssh_keys),1001(tom),0(root)
[root@master ~]# 

2.5.11 user.info

返回用户信息

[root@master ~]# salt '192.168.200.144' user.info jeery
192.168.200.144:
    ----------
    fullname:
        无效用户
    gid:
        995
    groups:
        - root
        - ssh_keys
        - tom
    home:
        /home/users/tom
    homephone:
    name:
        jeery
    other:
    passwd:
        x
    roomnumber:
    shell:
        /bin/haha
    uid:
        556
    workphone:
[root@master ~]# 

2.5.12 user.getent

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

[root@master ~]# salt '192.168.200.144' user.getent
192.168.200.144:
    |_
      ----------
      fullname:
          root
      gid:
          0
      groups:
          - root
      home:
          /root
      homephone:
      name:
          root
      other:
      passwd:
          x
      roomnumber:
      shell:
          /bin/bash
      uid:
          0
      workphone:
    |_
      ----------
      fullname:
          bin
      gid:
          1
      groups:
          - bin
      home:
          /bin
      homephone:
      name:
          bin
      other:
      passwd:
          x
      roomnumber:
      shell:
          /sbin/nologin
      uid:
          1
      workphone:
    |_
      ----------
      fullname:
          daemon
      gid:
          2
      groups:
          - daemon
      home:
          /sbin
      homephone:
      name:
          daemon
      other:
      passwd:
          x
      roomnumber:
      shell:
          /sbin/nologin
      uid:
          2
      workphone:
    |_
      ----------
      fullname:
          adm
      gid:
          4
      groups:
          - adm
      home:
          /var/adm
      homephone:
      name:
          adm
      other:
      passwd:
          x
      roomnumber:
      shell:
          /sbin/nologin
      uid:
          3
      workphone:
    |_
      ----------
      fullname:
          lp
      gid:
          7
      groups:
          - lp
      home:
          /var/spool/lpd
      homephone:
      name:
          lp
      other:
      passwd:
          x
      roomnumber:
      shell:
          /sbin/nologin
      uid:
          4
      workphone:
    |_
      ----------
      fullname:
          sync
      gid:
          0
      groups:
          - root
      home:
          /sbin
      homephone:
      name:
          sync
      other:
      passwd:
          x
      roomnumber:
      shell:
          /bin/sync
      uid:
          5
      workphone:
    |_
      ----------
      fullname:
          shutdown
      gid:
          0
      groups:
          - root
      home:
          /sbin
      homephone:
      name:
          shutdown
      other:
      passwd:
          x
      roomnumber:
      shell:
          /sbin/shutdown
      uid:
          6
      workphone:
    |_
      ----------
      fullname:
          halt
      gid:
          0
      groups:
          - root
      home:
          /sbin
      homephone:
      name:
          halt
      other:
      passwd:
          x
      roomnumber:
      shell:
          /sbin/halt
      uid:
          7
      workphone:
    |_
      ----------
      fullname:
          mail
      gid:
          12
      groups:
          - mail
      home:
          /var/spool/mail
      homephone:
      name:
          mail
      other:
      passwd:
          x
      roomnumber:
      shell:
          /sbin/nologin
      uid:
          8
      workphone:
    |_
      ----------
      fullname:
          operator
      gid:
          0
      groups:
          - root
      home:
          /root
      homephone:
      name:
          operator
      other:
      passwd:
          x
      roomnumber:
      shell:
          /sbin/nologin
      uid:
          11
      workphone:
    |_
      ----------
      fullname:
          games
      gid:
          100
      groups:
          - users
      home:
          /usr/games
      homephone:
      name:
          games
      other:
      passwd:
          x
      roomnumber:
      shell:
          /sbin/nologin
      uid:
          12
      workphone:
    |_
      ----------
      fullname:
          FTP User
      gid:
          50
      groups:
          - ftp
      home:
          /var/ftp
      homephone:
      name:
          ftp
      other:
      passwd:
          x
      roomnumber:
      shell:
          /sbin/nologin
      uid:
          14
      workphone:
    |_
      ----------
      fullname:
          Kernel Overflow User
      gid:
          65534
      groups:
          - nobody
      home:
          /
      homephone:
      name:
          nobody
      other:
      passwd:
          x
      roomnumber:
      shell:
          /sbin/nologin
      uid:
          65534
      workphone:
    |_
      ----------
      fullname:
          System message bus
      gid:
          81
      groups:
          - dbus
      home:
          /
      homephone:
      name:
          dbus
  

...省略N行
2.5.13 user.delete

删除用户

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

2.6 SaltStack常用模块之salt-cp

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

//拷贝单个文件到目标主机的/usr/src目录下

[root@master ~]# salt '192.168.200.144' cmd.run 'ls /usr/src'
192.168.200.144:
   debug
   kernels
   
[root@master ~]# salt-cp '192.168.200.144' /etc/passwd /usr/src/
192.168.200.144:
   ----------
   /usr/src/passwd:
       True
       
[root@master ~]# salt '192.168.200.144' cmd.run 'ls /usr/src'
192.168.200.144:
   debug
   kernels
   passwd
[root@master ~]# 


//拷贝多个文件到目标主机的/usr/src目录下   
[root@master etc]# salt-cp '192.168.200.144' /etc/shadow /etc/group /usr/src/
192.168.200.144:
   ----------
   /usr/src/group:
       True
   /usr/src/shadow:
       True
[root@master etc]# 

[root@master etc]# salt '192.168.200.144' cmd.run 'ls /usr/src'192.168.200.144:
   debug
   group
   kernels
   passwd
   shadow
[root@master etc]# 

2.7 SaltStack常用模块之file

2.7.1 file.access

检查指定路径是否存在

[root@master etc]# salt '192.168.200.144' cmd.run 'ls /usr/src'
192.168.200.144:
    debug
    group
    kernels
    passwd
    shadow
[root@master etc]# 


[root@master etc]# salt '192.168.200.144' file.access /usr/src/passwd f
192.168.200.144:
    True
[root@master etc]# 

[root@master etc]# salt '192.168.200.144' file.access /usr/src/abc f 
192.168.200.144:
    False
[root@master etc]# 

检查指定文件的权限信息

[root@master ~]# salt '192.168.200.144' cmd.run 'ls -l /usr/src'
192.168.200.144:
    total 12
    drwxr-xr-x. 2 root root    6 May 19  2020 debug
    -rw-r--r--  1 root root  499 Nov  4 15:21 group
    drwxr-xr-x. 3 root root   35 Nov  3 19:49 kernels
    -rw-r--r--  1 root root 1070 Nov  4 15:03 passwd
    -rw-r--r--  1 root root  654 Nov  4 15:21 shadow
[root@master ~]# 

//是否有读权限
[root@master ~]# salt '192.168.200.144' file.access /usr/src/passwd r
192.168.200.144:
    True
[root@master ~]# 

//是否有写权限
[root@master ~]# salt '192.168.200.144' file.access /usr/src/passwd w
192.168.200.144:
    True
[root@master ~]# 

//是否有执行权限
[root@master ~]# salt '192.168.200.144' file.access /usr/src/passwd x
192.168.200.144:
    False
[root@master ~]# 
2.7.2 file.append

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

[root@master ~]# salt '192.168.200.144' cmd.run 'ls -l /root/a'
192.168.200.144:
    -rw-r--r-- 1 root root 0 Nov  4 15:34 /root/a
[root@master ~]# 

[root@master ~]# salt '192.168.200.144' file.append /root/a "ha" "hei" 
192.168.200.144:
    Wrote 2 lines to "/root/a"
    
[root@master ~]# salt '192.168.200.144' cmd.run 'ls -l /root/a'192.168.200.144:
    -rw-r--r-- 1 root root 7 Nov  4 15:35 /root/a

[root@master ~]# salt '192.168.200.144' cmd.run 'cat /root/a'
192.168.200.144:
    ha
    hei
[root@master ~]# 
2.7.3 file.basename

获取指定路径的基名

[root@master ~]# salt '192.168.200.144' file.basename '/root/aaa/bbb'
192.168.200.144:
    bbb
[root@master ~]# 
2.7.4 file.dirname

获取指定路径的目录名

[root@master ~]# salt '192.168.200.144' file.dirname '/root/aaa/bbb' 
192.168.200.144:
    /root/aaa
[root@master ~]# 
2.7.5 file.check_hash

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

[root@master ~]# salt '192.168.200.144' cmd.run 'md5sum /etc/passwd'  
192.168.200.144:
    213b791eae5c3141d1b7c24a833c8a18  /etc/passwd
[root@master ~]# salt '192.168.200.144' file.check_hash /etc/passwd 213b791eae5c3141d1b7c24a833c8a18
192.168.200.144:
    True
[root@master ~]# 
2.7.6 file.chattr

修改指定文件的属性

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

给指定文件添加属性

//查看当前属性
[root@master ~]# salt '192.168.200.144' cmd.run 'lsattr /root'
192.168.200.144:
    -------------------- /root/anaconda-ks.cfg
    -------------------- /root/a
[root@master ~]# 

//添加属性
[root@master ~]# salt '192.168.200.144' file.chattr /root/a operator=add attributes=ai
192.168.200.144:
    True
[root@master ~]# salt '192.168.200.144' cmd.run 'lsattr /root'
192.168.200.144:
    -------------------- /root/anaconda-ks.cfg
    ----ia-------------- /root/a
[root@master ~]# 

给指定文件去除属性

[root@master ~]# salt '192.168.200.144' file.chattr /root/a operator=remove attributes=i
192.168.200.144:
    True

[root@master ~]# salt '192.168.200.144' cmd.run 'lsattr /root'
192.168.200.144:
    -------------------- /root/anaconda-ks.cfg
    -----a-------------- /root/a
[root@master ~]# 
2.7.7 file.chown

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

[root@master ~]# salt '192.168.200.144' cmd.run 'lsattr /root'192.168.200.144:
    -------------------- /root/anaconda-ks.cfg
    -------------------- /root/a
[root@master ~]# 

[root@master ~]# salt '192.168.200.144' file.chown /root/a haha haha
192.168.200.144:
    None
    
[root@master ~]# salt '192.168.200.144' cmd.run 'lsattr /root'
192.168.200.144:
    -------------------- /root/anaconda-ks.cfg
    -------------------- /root/a
[root@master ~]# 
2.7.8 file.copy

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

拷贝文件

[root@master ~]# salt '192.168.200.144' cmd.run 'ls -l /root'
192.168.200.144:
    total 8
    -rw-r--r--  1 root root    7 Nov  4 15:35 a
    -rw-------. 1 root root 1084 Sep 30 09:21 anaconda-ks.cfg
    
[root@master ~]# salt '192.168.200.144' file.copy /root/a /root/cc
192.168.200.144:
    True
[root@master ~]# salt '192.168.200.144' cmd.run 'ls -l /root'
192.168.200.144:
    total 12
    -rw-r--r--  1 root root    7 Nov  4 15:35 a
    -rw-------. 1 root root 1084 Sep 30 09:21 anaconda-ks.cfg
    -rw-r--r--  1 root root    7 Nov  4 16:05 cc
[root@master ~]# 

覆盖并拷贝目录,将会覆盖同名文件或目录

[root@master ~]# salt '192.168.200.144' cmd.run 'ls -l /root'
192.168.200.144:
    total 12
    -rw-r--r--  1 root root    7 Nov  4 15:35 a
    -rw-------. 1 root root 1084 Sep 30 09:21 anaconda-ks.cfg
    -rw-r--r--  1 root root    7 Nov  4 16:05 cc
[root@master ~]# salt '192.168.200.144' file.copy /tmp/ /root/abc recurse=True
192.168.200.144:
    True
[root@master ~]# salt '192.168.200.144' cmd.run 'ls -l /root'192.168.200.144:
    total 16
    -rw-r--r--   1 root root    7 Nov  4 15:35 a
    drwxrwxrwt  14 root root 4096 Nov  4 16:06 abc
    -rw-------.  1 root root 1084 Sep 30 09:21 anaconda-ks.cfg
    -rw-r--r--   1 root root    7 Nov  4 16:05 cc
[root@master ~]# 

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

[root@master ~]# salt '192.168.200.144' cmd.run 'ls -l /root/abc'
192.168.200.144:
    total 0
    drwxr-xr-x 2 root root 20 Nov  4 16:06 hsperfdata_root
    drwxr-xr-x 3 root root 17 Nov  4 16:06 systemd-private-0c371a29020541f8b13eab5f31739fb9-chronyd.service-m5r8Hi
    drwxr-xr-x 3 root root 17 Nov  4 16:06 systemd-private-0c371a29020541f8b13eab5f31739fb9-nginx.service-PvnDUi
    drwxr-xr-x 2 root root  6 Nov  4 16:06 vmware-root_918-2697532712
    drwxr-xr-x 2 root root  6 Nov  4 16:06 vmware-root_933-3988752732
    drwxr-xr-x 2 root root  6 Nov  4 16:06 vmware-root_938-2689078411
    drwxr-xr-x 2 root root  6 Nov  4 16:06 vmware-root_949-4021784396


[root@master ~]# 
[root@master ~]# salt '192.168.200.144' cmd.run 'ls -l /opt/abc'
192.168.200.144:
    total 0
    -rw-r--r-- 1 root root 0 Nov  4 16:09 haha
    -rw-r--r-- 1 root root 0 Nov  4 16:09 xixi


[root@master ~]# salt '192.168.200.144' file.copy /opt/abc /root/abc recurse=True remove_existing=True
192.168.200.144:
    True
[root@master ~]# salt '192.168.200.144' cmd.run 'ls -l /root/abc'192.168.200.144:
    total 0
    -rw-r--r-- 1 root root 0 Nov  4 16:09 haha
    -rw-r--r-- 1 root root 0 Nov  4 16:09 xixi
[root@master ~]# 
2.7.9 file.ditectory_exists

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

[root@master ~]# salt '192.168.200.144' cmd.run 'ls -l /opt'
192.168.200.144:
    total 0
    drwxr-xr-x 2 root root 30 Nov  4 16:09 abc

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

[root@master ~]# salt '192.168.200.144' file.directory_exists /opt/aaa
192.168.200.144:
    False
[root@master ~]# 

2.7.10 file.diskusage

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

[root@master ~]# salt '192.168.200.144' cmd.run 'du -sb /opt'
192.168.200.144:
    47  /opt
[root@master ~]# salt '192.168.200.144' file.diskusage 'du -sb /opt' 
192.168.200.144:
    0
2.7.11 file.file_exists

判断指定文件是否存在

[root@master ~]# salt '192.168.200.144' file.file_exists /root/a
192.168.200.144:
    True
[root@master ~]# salt '192.168.200.144' file.file_exists /root/abc
192.168.200.144:
    False    //返回False是因为abc是目录而非文件
[root@master ~]# 
2.7.12 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

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
salt '*' file.find /var mtime=+30d size=+10m print=path,size,mtime
salt '*' file.find /var/log name=\*.[0-9] mtime=+30d size=+10m delete
2.7.13 file.get_gid

获取指定文件的gid

[root@master ~]# salt '192.168.200.144' cmd.run 'ls -l /root/a'
192.168.200.144:
    -rw-r--r-- 1 root root 7 Nov  4 15:35 /root/a
[root@master ~]# salt '192.168.200.144' file.get_gid /root/a
192.168.200.144:
    0
[root@master ~]# 
2.7.14 file.get_group

获取指定文件的组名

[root@master ~]# salt '192.168.200.144' cmd.run 'ls -l /root/a'
192.168.200.144:
    -rw-r--r-- 1 root root 7 Nov  4 15:35 /root/a
[root@master ~]# salt '192.168.200.144' file.get_group /root/a
192.168.200.144:
    root
[root@master ~]# 
2.7.15 file.get_hash

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

[root@master ~]# salt '192.168.200.144' cmd.run 'sha256sum /root/a'
192.168.200.144:
    999071875d972b47905cf8cd2c37130e3897bcc0b85440f0cfe5eff748e76204  /root/a
[root@master ~]# salt '192.168.200.144' file.get_hash /root/a
192.168.200.144:
    999071875d972b47905cf8cd2c37130e3897bcc0b85440f0cfe5eff748e76204
[root@master ~]# 
2.7.16 file.get_mode

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

[root@master ~]# salt '192.168.200.144' cmd.run 'ls -l /root/a'
192.168.200.144:
    -rw-r--r-- 1 root root 7 Nov  4 15:35 /root/a
[root@master ~]# salt '192.168.200.144' file.get_mode /root/a
192.168.200.144:
    0644
[root@master ~]#
2.7.17 file.get_selinux_context

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

[root@master ~]# salt '192.168.200.144' cmd.run 'ls -Z /root/anaconda-ks.cfg'
192.168.200.144:
    system_u:object_r:admin_home_t:s0 /root/anaconda-ks.cfg
    
[root@master ~]# salt '192.168.200.144' file.get_selinux_context /root/anaconda-ks.cfg
192.168.200.144:
    system_u:object_r:admin_home_t:s0
[root@master ~]# 
2.7.18 file.get_sum

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

  • md5
  • sha1
  • sha224
  • sha256 (default)
  • sha384
  • sha512
[root@master ~]# salt '192.168.200.144' cmd.run 'sha256sum /root/a'
192.168.200.144:
    999071875d972b47905cf8cd2c37130e3897bcc0b85440f0cfe5eff748e76204  /root/a

[root@master ~]# salt '192.168.200.144' file.get_sum /root/a
192.168.200.144:
    999071875d972b47905cf8cd2c37130e3897bcc0b85440f0cfe5eff748e76204

[root@master ~]# salt '192.168.200.144' cmd.run 'md5sum /root/a'
192.168.200.144:
    3acd1679dcc16d76a16cb97721f57485  /root/a
[root@master ~]# 

[root@master ~]# salt '192.168.200.144' file.get_sum /root/a md5
192.168.200.144:
    3acd1679dcc16d76a16cb97721f57485
[root@master ~]# 
2.7.19 file.get_uid与file.get_user

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

[root@master ~]# salt '192.168.200.144' cmd.run 'ls -l /root/a'
192.168.200.144:
    -rw-r--r-- 1 root root 7 Nov  4 15:35 /root/a
[root@master ~]# salt '192.168.200.144' file.get_uid /root/a
192.168.200.144:
    0
[root@master ~]# salt '192.168.200.144' file.get_user /root/a
192.168.200.144:
    root
[root@master ~]# 
2.7.20 file.gid_to_group
[root@master ~]# salt '192.168.200.144' file.gid_to_group 1000
192.168.200.144:
    abc
[root@master ~]# salt '192.168.200.144' file.gid_to_group 0
192.168.200.144:
    root
[root@master ~]# 
2.7.21 file.group_to_gid

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

[root@master ~]# salt '192.168.200.144' file.group_to_gid root
192.168.200.144:
    0
[root@master ~]# salt '192.168.200.144' file.group_to_gid tom
192.168.200.144:
    1001
[root@master ~]# 
2.7.22 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
2.7.23 file.is_blkdev

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


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

//不是块设备文件,返回值为False
[root@master ~]# salt '192.168.200.144' file.is_blkdev /root/a
192.168.200.144:
    False
[root@master ~]# 
2.7.24 file.lsattr

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

[root@master ~]# salt '192.168.200.144' cmd.run 'lsattr /root/a'
192.168.200.144:
    -------------------- /root/a

[root@master ~]# salt '192.168.200.144' cmd.run 'chattr +i /root/a'192.168.200.144:

[root@master ~]# salt '192.168.200.144' cmd.run 'lsattr /root/a'
192.168.200.144:
    ----i--------------- /root/a
[root@master ~]# salt '192.168.200.144' file.lsattr /root/a
192.168.200.144:
    ----------
    /root/a:
        - i
[root@master ~]# 
2.7.25 file.mkdir

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

[root@master ~]# salt '192.168.200.144' cmd.run 'ls -l /root/'
192.168.200.144:
    total 12
    -rw-r--r--  1 root root    7 Nov  4 15:35 a
    drwxr-xr-x  2 root root   30 Nov  4 16:09 abc
    -rw-------. 1 root root 1084 Sep 30 09:21 anaconda-ks.cfg
    -rw-r--r--  1 root root    7 Nov  4 16:05 cc
    
[root@master ~]# salt '192.168.200.144' file.mkdir /root/bbb
192.168.200.144:
    True
    
[root@master ~]# salt '192.168.200.144' cmd.run 'ls -l /root/'
192.168.200.144:
    total 12
    -rw-r--r--  1 root root    7 Nov  4 15:35 a
    drwxr-xr-x  2 root root   30 Nov  4 16:09 abc
    -rw-------. 1 root root 1084 Sep 30 09:21 anaconda-ks.cfg
    drwxr-xr-x  2 root root    6 Nov  4 16:57 bbb
    -rw-r--r--  1 root root    7 Nov  4 16:05 cc
[root@master ~]# 

2.7.26 file.move

移动或重命名

//重命名
[root@master ~]# salt '192.168.200.144' cmd.run 'ls -l /root/'
192.168.200.144:
    total 12
    -rw-r--r--  1 root root    7 Nov  4 15:35 a
    drwxr-xr-x  2 root root   30 Nov  4 16:09 abc
    -rw-------. 1 root root 1084 Sep 30 09:21 anaconda-ks.cfg
    drwxr-xr-x  2 root root    6 Nov  4 16:57 bbb
    -rw-r--r--  1 root root    7 Nov  4 16:05 cc
[root@master ~]# 


[root@master ~]# salt '192.168.200.144' file.move /root/abc /root/cba
192.168.200.144:
    ----------
    comment:
        '/root/abc' moved to '/root/cba'
    result:
        True
        
[root@master ~]# salt '192.168.200.144' cmd.run 'ls -l /root/'192.168.200.144:
    total 16
    -rw-r--r--  1 root root    7 Nov  4 15:35 a
    -rw-------. 1 root root 1084 Sep 30 09:21 anaconda-ks.cfg
    -rw-r--r--  1 root root    7 Nov  4 15:35 b
    drwxr-xr-x  2 root root    6 Nov  4 16:57 bbb
    drwxr-xr-x  2 root root   30 Nov  4 16:09 cba
    -rw-r--r--  1 root root    7 Nov  4 16:05 cc
[root@master ~]# 



//移动
[root@master ~]# salt '192.168.200.144' cmd.run 'ls -l /root/'192.168.200.144:
    total 16
    -rw-r--r--  1 root root    7 Nov  4 15:35 a
    -rw-------. 1 root root 1084 Sep 30 09:21 anaconda-ks.cfg
    -rw-r--r--  1 root root    7 Nov  4 15:35 b
    drwxr-xr-x  2 root root    6 Nov  4 16:57 bbb
    drwxr-xr-x  2 root root   30 Nov  4 16:09 cba
    -rw-r--r--  1 root root    7 Nov  4 16:05 cc
    
[root@master ~]# salt '192.168.200.144' cmd.run 'ls -l /opt'
192.168.200.144:
    total 0
    drwxr-xr-x 2 root root 30 Nov  4 16:09 abc
    
[root@master ~]# salt '192.168.200.144' file.move /root/cba /opt/
192.168.200.144:
    ----------
    comment:
        '/root/cba' moved to '/opt/'
    result:
        True
        
[root@master ~]# salt '192.168.200.144' cmd.run 'ls -l /opt'
192.168.200.144:
    total 0
    drwxr-xr-x 2 root root 30 Nov  4 16:09 abc
    drwxr-xr-x 2 root root 30 Nov  4 16:09 cba
[root@master ~]# 

2.7.27 file.prepend

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

[root@master ~]# salt '192.168.200.144' cmd.run  'cat /root/b'
192.168.200.144:
    ha
    hei
[root@master ~]# 

[root@master ~]# salt '192.168.200.144' file.prepend /root/b "yiyi"  "erer" "sansan"
192.168.200.144:
    Prepended 3 lines to "/root/b"


[root@master ~]# salt "192.168.200.144" cmd.run 'cat /root/b'
192.168.200.144:
    yiyi
    erer
    sansan
    ha
    hei


[root@master ~]# salt '192.168.200.144' file.prepend /root/b "yiyi"  "erer" "sansan"
192.168.200.144:
    Prepended 3 lines to "/root/b"


[root@master ~]# salt "192.168.200.144" cmd.run 'cat /root/b'192.168.200.144:
    yiyi
    erer
    sansan
    yiyi
    erer
    sansan
    ha
    hei
[root@master ~]# 
2.7.28 file.sed

修改文本文件的内容

[root@master ~]# salt "192.168.200.144" cmd.run 'cat /root/b'
192.168.200.144:
    yiyi
    erer
    sansan
    yiyi
    erer
    sansan
    ha
    hei

[root@master ~]# salt '*' file.sed /root/b 'yiyi' 'edgnb' 
192.168.200.144:
    ----------
    pid:
        15384
    retcode:
        0
    stderr:
    stdout:
node2:
    False
master:
    False


[root@master ~]# salt "192.168.200.144" cmd.run 'cat /root/b'
192.168.200.144:
    edgnb
    erer
    sansan
    edgnb
    erer
    sansan
    ha
    hei
[root@master ~]# 



[root@master ~]# salt "192.168.200.144" cmd.run 'cat /root/b'
192.168.200.144:
    edg nb
    edg nb  edg nb edg nb
    edg nb  edg nb
    ha
    hei


[root@master ~]# salt "192.168.200.144" file.sed /root/b 'nb'  'yyds' flags=2
192.168.200.144:
    ----------
    pid:
        23389
    retcode:
        0
    stderr:
    stdout:
[root@master ~]# salt "192.168.200.144" cmd.run 'cat /root/b'192.168.200.144:
    edg nb
    edg nb  edg yyds edg nb
    edg nb  edg yyds
    ha
    hei
[root@master ~]# 

2.7.29 file.read

读取文件内容

[root@master ~]# salt "192.168.200.144" cmd.run 'cat /root/b'192.168.200.144:
    edg nb
    edg nb  edg yyds edg nb
    edg nb  edg yyds
    ha
    hei
[root@master ~]# salt "192.168.200.144" file.read  /root/b
192.168.200.144:
    edg nb
    edg nb  edg yyds edg nb
    edg nb  edg yyds
    ha
    hei
[root@master ~]# 
2.7.30 file.readdir

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

[root@master ~]# salt '192.168.200.144' file.readdir /root
192.168.200.144:
    - .
    - ..
    - .bash_logout
    - .bash_profile
    - .bashrc
    - .cshrc
    - .tcshrc
    - anaconda-ks.cfg
    - .config
    - a
    - cc
    - bbb
    - .bash_history
    - b.bak
    - b
[root@master ~]# 

2.7.31 file.remove

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

[root@master ~]# salt '192.168.200.144' cmd.run 'ls -l /root'
192.168.200.144:
    total 20
    -rw-r--r--  1 root root    7 Nov  4 15:35 a
    -rw-------. 1 root root 1084 Sep 30 09:21 anaconda-ks.cfg
    -rw-r--r--  1 root root   55 Nov  7 15:28 b
    -rw-r--r--  1 root root   51 Nov  7 15:26 b.bak
    drwxr-xr-x  2 root root    6 Nov  4 16:57 bbb
    -rw-r--r--  1 root root    7 Nov  4 16:05 cc
[root@master ~]# salt '192.168.200.144' file.remove /root/b
192.168.200.144:
    True
[root@master ~]# salt '192.168.200.144' file.remove /root/bbb
192.168.200.144:
    True
[root@master ~]# salt '192.168.200.144' cmd.run 'ls -l /root'
192.168.200.144:
    total 16
    -rw-r--r--  1 root root    7 Nov  4 15:35 a
    -rw-------. 1 root root 1084 Sep 30 09:21 anaconda-ks.cfg
    -rw-r--r--  1 root root   51 Nov  7 15:26 b.bak
    -rw-r--r--  1 root root    7 Nov  4 16:05 cc
[root@master ~]# 

2.7.32 file.rename

重命名文件或目录

[root@master ~]# salt '192.168.200.144' cmd.run 'ls -l /root'
192.168.200.144:
    total 16
    -rw-r--r--  1 root root    7 Nov  4 15:35 a
    -rw-------. 1 root root 1084 Sep 30 09:21 anaconda-ks.cfg
    -rw-r--r--  1 root root   51 Nov  7 15:26 b.bak
    -rw-r--r--  1 root root    7 Nov  4 16:05 cc
[root@master ~]# salt '192.168.200.144' file.rename /root/cc /root/b
192.168.200.144:
    True
[root@master ~]# salt '192.168.200.144' cmd.run 'ls -l /root'192.168.200.144:
    total 16
    -rw-r--r--  1 root root    7 Nov  4 15:35 a
    -rw-------. 1 root root 1084 Sep 30 09:21 anaconda-ks.cfg
    -rw-r--r--  1 root root    7 Nov  4 16:05 b
    -rw-r--r--  1 root root   51 Nov  7 15:26 b.bak
[root@master ~]# 
2.7.33 file.set_mode

给指定文件设置权限

[root@master ~]# salt '192.168.200.144' cmd.run 'ls -l /root'
192.168.200.144:
    total 16
    -rw-r--r--  1 root root    7 Nov  4 15:35 a
    -rw-------. 1 root root 1084 Sep 30 09:21 anaconda-ks.cfg
    -rw-r--r--  1 root root    7 Nov  4 16:05 b
    -rw-r--r--  1 root root   51 Nov  7 15:26 b.bak
[root@master ~]# salt '192.168.200.144' file.set_mode /root/b 0411
192.168.200.144:
    0411
[root@master ~]# salt '192.168.200.144' cmd.run 'ls -l /root'
192.168.200.144:
    total 16
    -rw-r--r--  1 root root    7 Nov  4 15:35 a
    -rw-------. 1 root root 1084 Sep 30 09:21 anaconda-ks.cfg
    -r----x--x  1 root root    7 Nov  4 16:05 b
    -rw-r--r--  1 root root   51 Nov  7 15:26 b.bak
[root@master ~]# 
2.7.34 file.symlink

给指定的文件创建软链接

[root@master ~]# salt '192.168.200.144' file.symlink /root/a /opt/a
192.168.200.144:
    True
[root@master ~]# 
[root@master ~]# salt '192.168.200.144' cmd.run 'ls -l /root;ls -l /opt'
192.168.200.144:
    total 16
    -rw-r--r--  1 root root    7 Nov  4 15:35 a
    -rw-------. 1 root root 1084 Sep 30 09:21 anaconda-ks.cfg
    -r----x--x  1 root root    7 Nov  4 16:05 b
    -rw-r--r--  1 root root   51 Nov  7 15:26 b.bak
    total 0
    lrwxrwxrwx 1 root root  7 Nov  7 15:46 a -> /root/a
    drwxr-xr-x 2 root root 30 Nov  7 15:44 abc
    -rw-r--r-- 1 root root  0 Nov  7 15:45 b
2.7.35 file.touch

创建空文件或更新时间戳


[root@master ~]# salt '192.168.200.144' file.touch /opt/ccc
192.168.200.144:
    True
[root@master ~]# salt '192.168.200.144' file.touch /opt/aa
192.168.200.144:
    True
[root@master ~]# salt '192.168.200.144' cmd.run 'ls -l /opt'
192.168.200.144:
    total 0
    lrwxrwxrwx 1 root root  7 Nov  7 15:46 a -> /root/a
    -rw-r--r-- 1 root root  0 Nov  7 15:48 aa
    drwxr-xr-x 2 root root 30 Nov  7 15:44 abc
    -rw-r--r-- 1 root root  0 Nov  7 15:45 b
    -rw-r--r-- 1 root root  0 Nov  7 15:48 ccc
[root@master ~]# salt '192.168.200.144' file.touch /opt/aa
192.168.200.144:
    True
[root@master ~]# salt '192.168.200.144' cmd.run 'ls -l /opt'
192.168.200.144:
    total 0
    lrwxrwxrwx 1 root root  7 Nov  7 15:46 a -> /root/a
    -rw-r--r-- 1 root root  0 Nov  7 15:49 aa
    drwxr-xr-x 2 root root 30 Nov  7 15:44 abc
    -rw-r--r-- 1 root root  0 Nov  7 15:45 b
    -rw-r--r-- 1 root root  0 Nov  7 15:48 ccc
[root@master ~]# 
2.7.36 file.uid_to_user

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

[root@master ~]# salt '192.168.200.144' file.uid_to_user 0
192.168.200.144:
    root
[root@master ~]# salt '192.168.200.144' file.uid_to_user 1001
192.168.200.144:
    haha
[root@master ~]# 
2.7.37 file.user_to_uid

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

[root@master ~]# salt '192.168.200.144' file.user_to_uid haha
192.168.200.144:
    1001
[root@master ~]# salt '192.168.200.144' file.user_to_uid root
192.168.200.144:
    0
[root@master ~]# 
2.7.38 file.write

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

[root@master ~]# salt '192.168.200.144' cmd.run 'cat /root/b'
192.168.200.144:
    ha
    hei
[root@master ~]# salt '192.168.200.144' file.write /root/b "qqq" "www" "eee"
192.168.200.144:
    Wrote 3 lines to "/root/b"
[root@master ~]# salt '192.168.200.144' cmd.run 'cat /root/b'192.168.200.144:
    qqq
    www
    eee
[root@master ~]# 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值