saltstack操作
阅读原文时间:2023年07月15日阅读:1

使用分组

修改minon的ID,做一个标识

[root@node2 ~]# vim /etc/salt/minion
master: 172.25.254.130
id: web1 #node2
[root@node2 ~]# systemctl restart salt-minion
[root@node3 ~]# vim /etc/salt/minion
master: 172.25.254.130
id: web2 #node3
[root@node3 ~]# vim /etc/salt/minion
[root@node4 ~]# systemctl restart salt-minion
master: 172.25.254.130
id: web3 #node3
[root@node4 ~]# systemctl restart salt-minion
[root@node5 ~]# vim /etc/salt/minion
master: 172.25.254.130
id: db1 #node4
[root@node5 ~]# systemctl restart salt-minion
[root@node6 ~]# vim /etc/salt/minion
master: 172.25.254.130
id: db2 #node5
[root@node6 ~]# systemctl restart salt-minion

master重新添加秘钥

[root@node1 ~]# salt-key -D

[root@node1 ~]# salt-key  -A

[root@node1 ~]# salt-key -L

Accepted Keys:
db1
db2
web1
web2
web3
Denied Keys:
Unaccepted Keys:
Rejected Keys:

测试连通性

[root@node1 ~]# salt '*' test.ping
web2:
True
web3:
True
db1:
True
db2:
True
web1:
True
[root@node1 ~]# salt '*' cmd.run 'date'
db2:
Wed Apr :: CST
web3:
Wed Apr :: CST
web2:
Wed Apr :: CST
db1:
Wed Apr :: CST
web1:
Wed Apr :: CST
[root@node1 ~]# salt -E "web\d" test.ping #\d 匹配一个数字  \d+ 匹配 1..n 个数字
web2:
True
web3:
True
web1:
True
[root@node1 ~]# salt -L "web1,web2,db2" test.ping #L 列表 后面跟随主机的名称,用 “,”分割
web2:
True
web1:
True
db2:
True

分组

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

nodegroups:
group1: "L@web1,web2,db2"
groupweb: "L@web1,web2,web3"
groupdb: "L@db1,db2"

[root@node1 ~]# systemctl restart salt-master

测试组

[root@node1 ~]# salt -S "172.25.254.0/24" test.ping
web2:
True
web3:
True
web1:
True
db1:
True
db2:
True
[root@node1 ~]# salt -N group1 test.ping
web2:
True
web1:
True
db2:
True
[root@node1 ~]# salt -N groupweb test.ping
web2:
True
web1:
True
web3:
True
[root@node1 ~]# salt -N groupdb test.ping
db2:
True
db1:
True

发送文件和文件夹

[root@node1 ~]# mkdir -p /srv/salt/files

[root@node1 ~]# cd /srv/salt/files

[root@node1 files]# echo testfile >> testfile

[root@node1 files]# salt -N groupweb cp.get_file salt://files/testfile  /var/tmp/f1

检测

[root@node1 files]# salt -N groupweb cmd.run 'md5sum /var/tmp/f1'

源文件对比

[root@node1 files]# md5sum testfile

e9409172a4036cc688f169c72131e921 testfile

查看内容

[root@node1 files]# salt -N groupweb cmd.run 'cat  /var/tmp/f1'

修改文件内容传递,内容覆盖

[root@node1 files]# echo >>testfile
[root@node1 files]# salt -N groupweb cp.get_file salt://files/testfile /var/tmp/f1
web3:
/var/tmp/f1
web1:
/var/tmp/f1
web2:
/var/tmp/f1
[root@node1 files]# salt -N groupweb cmd.run 'cat /var/tmp/f1'
web3:
testfile

web2:
testfile

web1:
testfile

传输文件夹

[root@node1 files]# cd ..

[root@node1 salt]# salt -N groupweb cp.get_dir salt://files /var/tmp/testdir

检测:

[root@node1 salt]# salt -N groupweb cmd.run 'cat  /var/tmp/testdir/files/testfile'

查看模块,功能和使用方法

[root@node1 salt]# salt 'web1' sys.list_modules

web1:
- acl
- aliases
- alternatives
- archive
- artifactory
- beacons
- bigip
- blockdev
- btrfs
- buildout
- cloud
- cmd
- composer
- config
- consul
- container_resource
- cp
- cron
- data
- defaults
- devmap
- disk
- django
- dnsmasq
- dnsutil
- drbd
- elasticsearch
- environ
- etcd
- ethtool
- event
- extfs
- file
- firewalld
- gem
- genesis
- gnome
- grains
- group
- hashutil
- hipchat
- hosts
- http
- img
- incron
- infoblox
- ini
- inspector
- introspect
- ip
- ipset
- iptables
- jboss7
- jboss7_cli
- k8s
- key
- keyboard
- kmod
- locale
- locate
- logrotate
- lowpkg
- lvm
- match
- mine
- minion
- modjk
- mount
- nagios_rpc
- network
- nova
- nspawn
- openscap
- openstack_config
- pagerduty
- pagerduty_util
- pam
- partition
- pillar
- pip
- pkg
- pkg_resource
- postfix
- publish
- pushover
- pyenv
- random
- random_org
- rbenv
- rest_sample_utils
- ret
- rvm
- s3
- s6
- salt_proxy
- saltutil
- schedule
- scsi
- sdb
- seed
- serverdensity_device
- service
- shadow
- slack
- slsutil
- smbios
- smtp
- sqlite3
- ssh
- state
- status
- supervisord
- sys
- sysctl
- sysfs
- syslog_ng
- system
- telemetry
- temp
- test
- timezone
- tuned
- udev
- uptime
- user
- vbox_guest
- virtualenv
- xfs
- zenoss

modules

[root@node1 salt]# salt 'web1' sys.list_functions test

web1:
- test.arg
- test.arg_repr
- test.arg_type
- test.assertion
- test.attr_call
- test.collatz
- test.conf_test
- test.cross_test
- test.echo
- test.exception
- test.false
- test.fib
- test.get_opts
- test.kwarg
- test.module_report
- test.not_loaded
- test.opts_pkg
- test.outputter
- test.ping
- test.provider
- test.providers
- test.rand_sleep
- test.rand_str
- test.retcode
- test.sleep
- test.stack
- test.true
- test.try_
- test.tty
- test.version
- test.versions
- test.versions_information
- test.versions_report

functions

[root@node1 salt]# salt web1  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.  
Returns {"args": args, "kwargs": kwargs}.

CLI Example:

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

test.arg_repr:

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.  
Returns {"args": repr(args), "kwargs": repr(kwargs)}.

CLI Example:

    salt '\*' test.arg\_repr  "two" 3.1 txt="hello" wow='{a: 1, b: "hello"}'

test.arg_type:

Print out the types of the args and kwargs. This is used to test the types  
of the args and kwargs passed down to the minion

CLI Example:

       salt '\*' test.arg\_type  'int'

test.assertion:

Assert the given argument

CLI Example:

    salt '\*' test.assertion False

test.attr_call:

Call grains.items via the attribute

CLI Example:

    salt '\*' test.attr\_call

test.collatz:

Execute the collatz conjecture from the passed starting number,  
returns the sequence and the time it took to compute. Used for  
performance tests.

CLI Example:

    salt '\*' test.collatz 

test.conf_test:

Return the value for test.foo in the minion configuration file, or return  
the default value

CLI Example:

    salt '\*' test.conf\_test

test.cross_test:

Execute a minion function via the \_\_salt\_\_ object in the test  
module, used to verify that the minion functions can be called  
via the \_\_salt\_\_ module.

CLI Example:

    salt '\*' test.cross\_test file.gid\_to\_group 

test.echo:

Return a string - used for testing the connection

CLI Example:

    salt '\*' test.echo 'foo bar baz quo qux'

test.exception:

Raise an exception

Optionally provide an error message or output the full stack.

CLI Example:

    salt '\*' test.exception 'Oh noes!'

test.false:

Always return False

CLI Example:

    salt '\*' test.false

test.fib:

Return the num-th Fibonacci number, and the time it took to compute in  
seconds. Used for performance tests.

This function is designed to have terrible performance.

CLI Example:

    salt '\*' test.fib 

test.get_opts:

Return the configuration options passed to this minion

CLI Example:

    salt '\*' test.get\_opts

test.kwarg:

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

CLI Example:

    salt '\*' test.kwarg num= txt="two" env='{a: 1, b: "hello"}'

test.module_report:

Return a dict containing all of the execution modules with a report on  
the overall availability via different references

CLI Example:

    salt '\*' test.module\_report

test.not_loaded:

List the modules that were not loaded by the salt loader system

CLI Example:

    salt '\*' test.not\_loaded

test.opts_pkg:

Return an opts package with the grains and opts for this minion.  
This is primarily used to create the options used for master side  
state compiling routines

CLI Example:

    salt '\*' test.opts\_pkg

test.outputter:

Test the outputter, pass in data to return

CLI Example:

    salt '\*' test.outputter foobar

test.ping:

Used to make sure the minion is up and responding. Not an ICMP ping.

Returns \`\`True\`\`.

CLI Example:

    salt '\*' test.ping

test.provider:

Pass in a function name to discover what provider is being used

CLI Example:

    salt '\*' test.provider service

test.providers:

Return a dict of the provider names and the files that provided them

CLI Example:

    salt '\*' test.providers

test.rand_sleep:

Sleep for a random number of seconds, used to test long-running commands  
and minions returning at differing intervals

CLI Example:

    salt '\*' test.rand\_sleep 

test.rand_str:

Return a random string

    size  
        size of the string to generate  
    hash\_type  
        hash type to use

        New in version 2015.5.

CLI Example:

    salt '\*' test.rand\_str

test.retcode:

Test that the returncode system is functioning correctly

CLI Example:

    salt '\*' test.retcode 

test.sleep:

Instruct the minion to initiate a process that will sleep for a given  
period of time.

CLI Example:

    salt '\*' test.sleep 

test.stack:

Return the current stack trace

CLI Example:

    salt '\*' test.stack

test.true:

Always return True

CLI Example:

    salt '\*' test.true

test.try_:

Try to run a module command. On an exception return None.  
If \`return\_try\_exception\` is set True return the exception.  
This can be helpful in templates where running a module might fail as expected.

CLI Example:

    <pre>  
    {% for i in range(,) %}  
        {{ salt\['test.try'\](module='ipmi.get\_users', bmc\_host='172.2.2.'+i)|yaml(False) }}  
    {% endfor %}  
    </pre>

test.tty:

Deprecated! Moved to cmdmod.

CLI Example:

    salt '\*' test.tty tty0 'This is a test'  
    salt '\*' test.tty pts3 'This is a test'

test.version:

Return the version of salt on the minion

CLI Example:

    salt '\*' test.version

test.versions:

This function is an alias of ``versions_report``.

Returns versions of components used by salt

CLI Example:

    salt '\*' test.versions\_report

test.versions_information:

Report the versions of dependent and system software

CLI Example:

    salt '\*' test.versions\_information

test.versions_report:

Returns versions of components used by salt

CLI Example:

    salt '\*' test.versions\_report

help

拷贝文件

相当于 ssh 到目的主机执行 cp

[root@node1 salt]# salt -N groupweb file.copy /etc/passwd /var/tmp/passwd
web3:
True
web1:
True
web2:
True
[root@node1 salt]# salt -N groupweb cmd.run 'ls /var/tmp/passwd'
web2:
/var/tmp/passwd
web3:
/var/tmp/passwd
web1:
/var/tmp/passwd

grains 模块

查看 节点 grains 的所有信息

[root@node1 salt]# salt 'web1'  grains.items

web1:
----------
SSDs:
biosreleasedate:
//
biosversion:
6.00
cpu_flags:
- fpu
- vme
- de
- pse
- tsc
- msr
- pae
- mce
- cx8
- apic
- sep
- mtrr
- pge
- mca
- cmov
- pat
- pse36
- clflush
- mmx
- fxsr
- sse
- sse2
- ss
- ht
- syscall
- nx
- pdpe1gb
- rdtscp
- lm
- constant_tsc
- arch_perfmon
- nopl
- xtopology
- tsc_reliable
- nonstop_tsc
- eagerfpu
- pni
- pclmulqdq
- ssse3
- fma
- cx16
- pcid
- sse4_1
- sse4_2
- x2apic
- movbe
- popcnt
- tsc_deadline_timer
- aes
- xsave
- avx
- f16c
- rdrand
- hypervisor
- lahf_lm
- abm
- fsgsbase
- tsc_adjust
- bmi1
- avx2
- smep
- bmi2
- invpcid
- xsaveopt
- arat
cpu_model:
Intel(R) Core(TM) i5- CPU @ .20GHz
cpuarch:
x86_64
disks:
- sda
- sdb
- sdc
- sdd
- sr0
- dm-
- dm-
dns:
----------
domain:
ip4_nameservers:
- 172.25.254.2
ip6_nameservers:
nameservers:
- 172.25.254.2
options:
search:
- localdomain
sortlist:
domain:
fqdn:
node2
fqdn_ip4:
- 172.25.254.131
fqdn_ip6:
- fe80:::96e2:b57b:be1d
gid:

gpus:  
    |\_  
      ----------  
      model:  
          SVGA II Adapter  
      vendor:  
          unknown  
groupname:  
    root  
host:  
    node2  
hwaddr\_interfaces:  
    ----------  
    ens33:  
        :0c:::2e:  
    ens34:  
        :0c:::2e:  
    lo:  
        :::::  
id:  
    web1  
init:  
    systemd  
ip4\_interfaces:  
    ----------  
    ens33:  
        - 172.25.254.131  
    ens34:  
    lo:  
        - 127.0.0.1  
ip6\_interfaces:  
    ----------  
    ens33:  
        - fe80:::96e2:b57b:be1d  
    ens34:  
    lo:  
        - ::  
ip\_interfaces:  
    ----------  
    ens33:  
        - 172.25.254.131  
        - fe80:::96e2:b57b:be1d  
    ens34:  
    lo:  
        - 127.0.0.1  
        - ::  
ipv4:  
    - 127.0.0.1  
    - 172.25.254.131  
ipv6:  
    - ::  
    - fe80:::96e2:b57b:be1d  
kernel:  
    Linux  
kernelrelease:  
    3.10.-.el7.x86\_64  
locale\_info:  
    ----------  
    defaultencoding:  
        UTF-  
    defaultlanguage:  
        en\_US  
    detectedencoding:  
        UTF-  
localhost:  
    node2  
lsb\_distrib\_codename:  
    CentOS Linux  (Core)  
lsb\_distrib\_id:  
    CentOS Linux  
machine\_id:  
    8ebd8732ba9148f79a787d6901b7e2a9  
manufacturer:  
    VMware, Inc.  
master:  
    172.25.254.130  
mdadm:  
mem\_total:

nodename:  
    node2  
num\_cpus:

num\_gpus:

os:  
    CentOS  
os\_family:  
    RedHat  
osarch:  
    x86\_64  
oscodename:  
    CentOS Linux  (Core)  
osfinger:  
    CentOS Linux-  
osfullname:  
    CentOS Linux  
osmajorrelease:

osrelease:  
    7.4.  
osrelease\_info:  
    -  
    -  
    -  
path:  
    /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/bin:/sbin  
pid:

productname:  
    VMware Virtual Platform  
ps:  
    ps -efH  
pythonexecutable:  
    /usr/bin/python  
pythonpath:  
    - /usr/bin  
    - /usr/lib64/python27.zip  
    - /usr/lib64/python2.  
    - /usr/lib64/python2./plat-linux2  
    - /usr/lib64/python2./lib-tk  
    - /usr/lib64/python2./lib-old  
    - /usr/lib64/python2./lib-dynload  
    - /usr/lib64/python2./site-packages  
    - /usr/lib/python2./site-packages  
pythonversion:  
    -  
    -  
    -  
    - final  
    -  
saltpath:  
    /usr/lib/python2./site-packages/salt  
saltversion:  
    2016.11.  
saltversioninfo:  
    -  
    -  
    -  
    -  
selinux:  
    ----------  
    enabled:  
        True  
    enforced:  
        Enforcing  
serialnumber:  
    VMware- 4d 8c fc   c0 - fd ee    2e  
server\_id:

shell:  
    /bin/sh  
systemd:  
    ----------  
    features:  
        +PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 -SECCOMP +BLKID +ELFUTILS +KMOD +IDN  
    version:

uid:

username:  
    root  
uuid:  
    fc8c4d56--88c0-77fd-ee6097122e59  
virtual:  
    VMware  
zmqversion:  
    4.1.

web-grains

查看网卡信息

[root@node1 salt]# salt  'web1' grains.item ip4_interfaces

web1:
----------
ip4_interfaces:
----------
ens33:
- 172.25.254.131
ens34:
lo:
- 127.0.0.1

查询主机名

[root@node1 salt]# salt  'web1' grains.item fqdn

web1:
----------
fqdn:
node2

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器