初识salt之saltstack配置应用以及基础用法

初识salt之saltstack配置应用以及基础用法

初识salt之saltstack配置应用以及基础用法

初识salt之saltstack配置应用以及基础用法

实施如下:

一、测试是否能管理client  
    使用模块cmd.run 可以查看到client的ip地址  
    [root@salt_server ~]# salt '*' cmd.run 'ip a'     
    salt_client1:    
    lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN  
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00  
    inet 127.0.0.1/8 scope host lo   
    inet6 ::1/128 scope host     
    valid_lft forever preferred_lft forever     
    eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000   
    link/ether 00:0c:29:f4:4f:0b brd ff:ff:ff:ff:ff:ff  
    inet 192.168.100.245/24 brd 192.168.100.255 scope global eth0  
    inet6 fe80::20c:29ff:fef4:4f0b/64 scope link   
    valid_lft forever preferred_lft forever  
    [root@salt_server ~]#   

二、查看版本    
[root@salt_server ~]# salt --versions-report  
           Salt: 2014.1.0  
         Python: 2.6.6 (r266:84292, Jun 18 2012, 14:18:47)
         Jinja2: unknown
       M2Crypto: 0.20.2
 msgpack-python: 0.1.13
   msgpack-pure: Not Installed
       pycrypto: 2.0.1
         PyYAML: 3.09
          PyZMQ: 2.2.0.1
            ZMQ: 3.2.4
[root@salt_server ~]# 

关于salt的命令 可以用salt -h 来查看具体用法

三、简单使用salt  
salt 在语句的后面加个 -t 时间,就可以做超时控制:  
[root@salt_server ~]# salt \* cmd.run 'sleep 3; echo ok' -t 3
salt_client1:
    ok
[root@salt_server ~]#   

salt \*  代表所有的在线客户端,离线的是无法显示出来的  

使用用shell来表示salt:  
[root@salt_server ~]# salt 'salt_client1' cmd.run 'dir'
salt_client1:
    anaconda-ks.cfg		     install.log.syslog
    epel-release-6-8.noarch.rpm  rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm
    install.log
[root@salt_server ~]#   
用正则来查看以salt开头的所有主机的目录  
[root@salt_server ~]# salt 'salt_*'  cmd.run 'dir'
salt_client1:
    anaconda-ks.cfg		     install.log.syslog
    epel-release-6-8.noarch.rpm  rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm
    install.log
[root@salt_server ~]#   

使用正则E来查看主机  
[root@salt_server ~]# salt -E 'salt_client*' test.ping  
salt_client1:
    True
salt_client2:
    True
[root@salt_server ~]# 

使用-L 来用列表方式来表示  
[root@salt_server ~]# salt -L 'salt_client1,salt_client2' test.ping
salt_client1:
    True
salt_client2:
    True
[root@salt_server ~]# 

  -N 是查看组。首先我们来添加组  
修改配置文件master vim /etc/salt/master
nodegroups:
   salt_group1: 'L@salt_client1,salt_client2'
   salt_group2: 'salt_client1'
   
[root@salt_server ~]# /etc/rc.d/init.d/salt-master restart  
Stopping salt-master daemon:                               [  OK  ]
Starting salt-master daemon:                               [  OK  ]
[root@salt_server ~]#   
等一分钟 然后在查看

 -N 参数
salt_client1 	这里指组名  
test.ping		这里指命令  
[root@salt_server ~]# salt -N salt_group1 test.ping  
salt_client1:
    True
salt_client2:
    True
[root@salt_server ~]# salt -N salt_group2 test.ping  
salt_client1:
    True
[root@salt_server ~]#   
[root@salt_server ~]# salt -N salt_group1 cmd.run 'hostname'  
salt_client1:
    salt_client1
salt_client2:
    salt_client2
[root@salt_server ~]#   


测试所有主机是否能连接  
[root@salt_server ~]# salt '*' test.ping  
salt_client1:
    True
测试salt_client1是否能连接  
[root@salt_server ~]# salt 'salt_client1' test.ping  
salt_client1:
    True
[root@salt_server ~]# 


[root@salt_server ~]# salt-syndic -l debug  

salt '*' -b 10 test.ping

salt -G 'os:RedHat' --batch-size 25% apache.signal restart  

四、使用grains 插件  
grains 是salt客户端自己收集机器的相关信息,以及特称,在服务端我们可以使用-G参数来查看

使用-G来应用salt  
这个参数很强大会根据用grair来指定最新的grains z这个参数和puppet里面的facter差不多
查看帮助如下:  
 -G, --grain         Instead of using shell globs to evaluate the target
                        use a grain value to identify targets, the syntax for
                        the target is the grain key followed by a
                        globexpression: "os:Arch*"
						
						
我的系统是CentOS我就输入CentOS。  
[root@salt_server ~]# salt -G 'os:CentOS'  test.ping
salt_client1:
    True
[root@salt_server ~]#  

[root@salt_server ~]# salt '*' grains.item os  
salt_client1:
  os: CentOS
[root@salt_server ~]#   
[root@salt_server ~]# salt -G 'cpuarch:x86_64' grains.item num_cpus  
salt_client1:
  num_cpus: 1
salt_client2:
  num_cpus: 1
[root@salt_server ~]# 
[root@salt_server ~]# salt '*' grains.items	查看有多少项目参数  
[root@salt_server ~]# salt '*' grains.ls	查看有哪些参数  

[root@salt_server salt]# salt 'salt_client1' grains.items 查看某个机器的基本信息  
salt_client1:    
  biosreleasedate: 07/31/2013  
  biosversion: 6.00  
  cpu_flags: fpu vme de pse tsc msr pae mce cx8 apic mtrr pge mca cmov pat pse36 clflush dts mmx fxsr sse sse2 ss syscall nx rdtscp lm constant_tsc up arch_perfmon pebs bts xtopology tsc_reliable nonstop_tsc aperfmperf unfair_spinlock pni pclmulqdq ssse3 cx16 sse4_1 sse4_2 x2apic popcnt xsave avx hypervisor lahf_lm ida arat epb xsaveopt pln pts dts
  cpu_model: Intel(R) Core(TM) i5-2520M CPU @ 2.50GHz
  cpuarch: x86_64
  defaultencoding: UTF8
  defaultlanguage: en_US
  domain: 
  external_ip:
  fqdn: salt_client1
  fqdn_ip4:
      192.168.100.245
  fqdn_ip6:
  gpus:
      {'model': 'SVGA II Adapter', 'vendor': 'unknown'}
  host: salt_client1
  hwaddr_interfaces: {'lo': '00:00:00:00:00:00', 'eth0': '00:0c:29:f4:4f:0b'}
  id: salt_client1
  ip_interfaces: {'lo': ['127.0.0.1'], 'eth0': ['192.168.100.245']}
  ipv4:
      127.0.0.1
      192.168.100.245
  ipv6:
  kernel: Linux
  kernelrelease: 2.6.32-279.el6.x86_64
  localhost: salt_client1
  lsb_distrib_codename: Final
  lsb_distrib_id: CentOS
  lsb_distrib_release: 6.3
  manufacturer: VMware, Inc.
  master: 192.168.100.228
  mem_total: 988
  nodename: salt_client1
  num_cpus: 1
  num_gpus: 1
  os: CentOS
  os_family: RedHat
  osarch: x86_64
  oscodename: Final
  osfinger: CentOS-6
  osfullname: CentOS
  osmajorrelease:
      6
      3
  osrelease: 6.3
  path: /sbin:/usr/sbin:/bin:/usr/bin
  productname: VMware Virtual Platform
  ps: ps -efH
  pythonpath:
      /usr/bin
      /usr/lib64/python26.zip
      /usr/lib64/python2.6
      /usr/lib64/python2.6/plat-linux2
      /usr/lib64/python2.6/lib-tk
      /usr/lib64/python2.6/lib-old
      /usr/lib64/python2.6/lib-dynload
      /usr/lib64/python2.6/site-packages
      /usr/lib/python2.6/site-packages
  pythonversion: 2.6.6.final.0
  saltpath: /usr/lib/python2.6/site-packages/salt
  saltversion: 2014.1.0
  saltversioninfo:
      2014
      1
      0
  serialnumber: VMware-56 4d 4b 45 d5 06 b3 89-63 e6 be 9b 64 f4 4f 0b
  server_id: 21436595
  shell: /bin/bash
  virtual: VMware
[root@salt_server salt]# 

五、使用salt来同步一个第一个简单的文件  
[root@salt_server ~]# mkdir /srv/salt/base -p  
[root@salt_server ~]# cd /srv/salt/base  
[root@salt_server salt]# echo "test salt first file" >> first.txt  
[root@salt_server salt]# salt 'salt_client1' cp.get_file salt://first.txt /home/first.txt
salt_client1:
    /home/first.txt
[root@salt_server salt]#   
在客户端查看
[root@salt_client1 home]# pwd  
/home
[root@salt_client1 home]# cat first.txt   
test salt first file
[root@salt_client1 home]#   


[root@salt_server salt]# salt-run jobs.list_jobs 最近操作文件的配置  
[root@salt_server salt]# salt-run jobs.lookup_jid 20140317113642863799  后面是返回值 配合第一个一起使用  

六、使用salt来查看客户端系统相关信息  
查看当前是否有任务运行  
[root@salt_server salt]# salt-run jobs.active
{}
[root@salt_server salt]# 

查看所有的主机名
[root@salt_server salt]# salt '*' cmd.run hostname
salt_client1:
    salt_client1
[root@salt_server salt]# 

查看系统
[root@salt_server salt]# salt '*' grains.item os
salt_client1:
  os: CentOS
[root@salt_server salt]# 

查看客户端IP地址
[root@salt_server salt]# salt 'salt_client1' network.interfaces
salt_client1:
    ----------
    eth0:
        ----------
        hwaddr:
            00:0c:29:f4:4f:0b
        inet:
            ----------
            - address:
                192.168.100.245
            - broadcast:
                192.168.100.255
            - label:
                eth0
            - netmask:
                255.255.255.0
        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
        up:
            True
[root@salt_server salt]# 

查看硬盘
[root@salt_server salt]# salt 'salt_client1' disk.usage
salt_client1:
    ----------
    /:
        ----------
        1K-blocks:
            10321208
        available:
            8462124
        capacity:
            14%
        filesystem:
            /dev/sda2
        used:
            1334796
    /boot:
        ----------
        1K-blocks:
            198337
        available:
            159996
        capacity:
            15%
        filesystem:
            /dev/sda1
        used:
            28101
    /dev/shm:
        ----------
        1K-blocks:
            506352
        available:
            506352
        capacity:
            0%
        filesystem:
            tmpfs
        used:
            0
    /home:
        ----------
        1K-blocks:
            10321208
        available:
            9642808
        capacity:
            2%
        filesystem:
            /dev/sda3
        used:
            154112
    /media:
        ----------
        1K-blocks:
            19134336
        available:
            4939136
        capacity:
            73%
        filesystem:
            192.168.100.249:/opt/
        used:
            13223232
    /mnt:
        ----------
        1K-blocks:
            19134336
        available:
            4939136
        capacity:
            73%
        filesystem:
            192.168.100.249:/opt/CentOS
        used:
            13223232
[root@salt_server salt]# 
[root@salt_server ~]# salt 'salt_client1' cmd.run 'df -h'
salt_client1:
    Filesystem            Size  Used Avail Use% Mounted on
    /dev/sda2             9.9G  1.3G  8.1G  14% /
    tmpfs                 495M     0  495M   0% /dev/shm
    /dev/sda1             194M   28M  157M  15% /boot
    /dev/sda3             9.9G  151M  9.2G   2% /home
    192.168.100.249:/opt/CentOS
                           19G   13G  4.8G  73% /mnt
    192.168.100.249:/opt/
                           19G   13G  4.8G  73% /media
[root@salt_server ~]# 


七、使用salt来管理系统软件包
安装包
[root@salt_server salt]# salt 'salt_client1' pkg.install telnet
salt_client1:
    ----------
    telnet:
        ----------
        new:
            0.17-47.el6
        old:
			
[root@salt_server salt]# 

查看安装的包,这里安装的包指的不是用salt安装的包 而是全部的包
[root@salt_server salt]# salt 'salt_client1' pkg.list_pkgs

删除刚安装的包
[root@salt_server salt]# salt 'salt_client1' pkg.remove telnet
salt_client1:
    ----------
    telnet:
        ----------
        new:
            sal	
        old:
            0.17-47.el6
[root@salt_server salt]# 
在客户端查看
[root@salt_client1 salt]# rpm -qa | grep telnet
telnet-0.17-47.el6.x86_64
[root@salt_client1 salt]# telnet
telnet> ^C
[root@salt_client1 salt]# rpm -qa | grep telnet
[root@salt_client1 salt]# 

查看repos内容:
[root@salt_server salt]# salt 'salt_client1' pkg.list_repos
salt_client1:
    ----------
    source:
        ----------
        baseurl:
            file:///mnt/CentOS6.3
        enabled:
            1
        file:
            /etc/yum.repos.d/source.repo
        gpgcheck:
            0
        name:
            source
    source1:
        ----------
        baseurl:
            file:///mnt/saltstack
        enabled:
            1
        file:
            /etc/yum.repos.d/source.repo
        gpgcheck:
            0
        name:
            source1
[root@salt_server salt]# 

如果想查看所有minion的链接情况,我们可以用如下命令来查看
[root@salt_server ~]# salt '*' test.ping
salt_client1:
    True
[root@salt_server ~]# salt-run manage.status
down:
up:
    - salt_client1
[root@salt_server ~]# 

删除客户端的一个用户:
[root@salt_server ~]# salt 'salt_client1' webutil.userdel /etc/passwd kcc
salt_client1:
    - Deleting password for user kcc
[root@salt_server ~]# 

Space-delimited arguments to the function:
[root@salt_server ~]# salt \* cmd.exec_code python 'import sys; print sys.version'
salt_client2:
    2.6.6 (r266:84292, Jun 18 2012, 14:18:47) 
    [GCC 4.4.6 20110731 (Red Hat 4.4.6-3)]
salt_client1:
    2.6.6 (r266:84292, Jun 18 2012, 14:18:47) 
    [GCC 4.4.6 20110731 (Red Hat 4.4.6-3)]
[root@salt_server ~]# 

See also