其他分享
首页 > 其他分享> > SaltStact自动化运维工具02

SaltStact自动化运维工具02

作者:互联网

 Grains基础:
• Grains是saltstack最重要的组件之一
• 存储minion端的基本信息,这些信息一般都是静态的,如CPU、内核、操作系统等
• Grains存储在minion本地
• 管理员可以在minion端进行grains值的修改,如增加、删除等

Grains基础应用
应用一: 获取minion端所有grains信息
        # salt '随便一台主机名' grains.items  相当于python字典的items() 列出key value
        #salt '随便一台主机名' grains.ls  只列出key
        #salt '随便一台主机名' grains.get saltversion  获取某个key的value  
        • 通过grains.item获取minion端的fqdn信息
        # salt '随便一台主机名' grains.item fqdn

[root@linux-node1 ~]#  salt 'linux-node1.localdomain' grains.items 
linux-node1.localdomain:
    ----------
    SSDs:
    biosreleasedate:
        07/02/2015
    biosversion:
        6.00
    cpu_flags:
        - fpu
        - vme
        - de
        - pse
        - tsc
        - msr
        - pae
        - mce
        - cx8
        - apic
        - sep
        - mtrr
        - pge
        - mca
        - cmov
        - pat
        - pse36
        - clflush
        - dts
        - mmx
        - fxsr
        - sse
        - sse2
        - ss
        - ht
        - syscall
        - nx
        - pdpe1gb
        - rdtscp
        - lm
        - constant_tsc
        - arch_perfmon
        - pebs
        - bts
        - nopl
        - xtopology
        - tsc_reliable
        - nonstop_tsc
        - aperfmperf
        - eagerfpu
        - pni
        - pclmulqdq
        - vmx
        - ssse3
        - fma
        - cx16
        - pcid
        - sse4_1
        - sse4_2
        - x2apic
        - movbe
        - popcnt
        - tsc_deadline_timer
        - aes
        - xsave
        - avx
        - f16c
        - rdrand
        - hypervisor
        - lahf_lm
        - abm
        - epb
        - tpr_shadow
        - vnmi
        - ept
        - vpid
        - fsgsbase
        - tsc_adjust
        - bmi1
        - avx2
        - smep
        - bmi2
        - invpcid
        - xsaveopt
        - dtherm
        - arat
        - pln
        - pts
    cpu_model:
        Intel(R) Core(TM) i7-4710MQ CPU @ 2.50GHz
    cpuarch:
        x86_64
    disks:
        - sda
        - sr0
        - dm-0
        - dm-1
        - dm-2
    dns:
        ----------
        domain:
        ip4_nameservers:
            - 192.168.0.1
        ip6_nameservers:
        nameservers:
            - 192.168.0.1
        options:
        search:
            - localdomain
        sortlist:
    domain:
        localdomain
    fqdn:
        localhost.localdomain
    fqdn_ip4:
        - 127.0.0.1
    fqdn_ip6:
        - ::1
    fqdns:
    gid:
        0
    gpus:
        |_
          ----------
          model:
              SVGA II Adapter
          vendor:
              vmware
    groupname:
        root
    host:
        localhost
    hwaddr_interfaces:
        ----------
        ens33:
            00:0c:29:fd:fa:c7
        lo:
            00:00:00:00:00:00
        virbr0:
            52:54:00:17:39:b3
        virbr0-nic:
            52:54:00:17:39:b3
    id:
        linux-node1.localdomain
    init:
        systemd
    ip4_gw:
        192.168.0.1
    ip4_interfaces:
        ----------
        ens33:
            - 192.168.0.2
        lo:
            - 127.0.0.1
        virbr0:
            - 192.168.122.1
        virbr0-nic:
    ip6_gw:
        False
    ip6_interfaces:
        ----------
        ens33:
            - fe80::cd7:c134:744a:cf30
        lo:
            - ::1
        virbr0:
        virbr0-nic:
    ip_gw:
        True
    ip_interfaces:
        ----------
        ens33:
            - 192.168.0.2
            - fe80::cd7:c134:744a:cf30
        lo:
            - 127.0.0.1
            - ::1
        virbr0:
            - 192.168.122.1
        virbr0-nic:
    ipv4:
        - 127.0.0.1
        - 192.168.0.2
        - 192.168.122.1
    ipv6:
        - ::1
        - fe80::cd7:c134:744a:cf30
    kernel:
        Linux
    kernelrelease:
        3.10.0-957.el7.x86_64
    kernelversion:
        #1 SMP Thu Nov 8 23:39:32 UTC 2018
    locale_info:
        ----------
        defaultencoding:
            UTF-8
        defaultlanguage:
            zh_CN
        detectedencoding:
            UTF-8
    localhost:
        linux-node1.localdomain
    lsb_distrib_codename:
        CentOS Linux 7 (Core)
    lsb_distrib_id:
        CentOS Linux
    machine_id:
        37a27c09411541ca8f072be3ccc05527
    manufacturer:
        VMware, Inc.
    master:
        192.168.0.2
    mdadm:
    mem_total:
        3771
    nodename:
        linux-node1.localdomain
    num_cpus:
        4
    num_gpus:
        1
    os:
        CentOS
    os_family:
        RedHat
    osarch:
        x86_64
    oscodename:
        CentOS Linux 7 (Core)
    osfinger:
        CentOS Linux-7
    osfullname:
        CentOS Linux
    osmajorrelease:
        7
    osrelease:
        7.6.1810
    osrelease_info:
        - 7
        - 6
        - 1810
    path:
        /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
    pid:
        21258
    productname:
        VMware Virtual Platform
    ps:
        ps -efHww
    pythonexecutable:
        /usr/bin/python
    pythonpath:
        - /usr/bin
        - /usr/lib64/python27.zip
        - /usr/lib64/python2.7
        - /usr/lib64/python2.7/plat-linux2
        - /usr/lib64/python2.7/lib-tk
        - /usr/lib64/python2.7/lib-old
        - /usr/lib64/python2.7/lib-dynload
        - /usr/lib64/python2.7/site-packages
        - /usr/lib/python2.7/site-packages
    pythonversion:
        - 2
        - 7
        - 5
        - final
        - 0
    saltpath:
        /usr/lib/python2.7/site-packages/salt
    saltversion:
        2019.2.0
    saltversioninfo:
        - 2019
        - 2
        - 0
        - 0
    selinux:
        ----------
        enabled:
            True
        enforced:
            Enforcing
    serialnumber:
        VMware-56 4d 48 8f 93 62 a0 3f-01 fa 25 b5 f5 fd fa c7
    server_id:
        740310944
    shell:
        /bin/sh
    swap_total:
        3967
    systemd:
        ----------
        features:
            +PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 -SECCOMP +BLKID +ELFUTILS +KMOD +IDN
        version:
            219
    uid:
        0
    username:
        root
    uuid:
        8f484d56-6293-3fa0-01fa-25b5f5fdfac7
    virtual:
        VMware
    zfs_feature_flags:
        False
    zfs_support:
        False
    zmqversion:
        4.1.4
[root@linux-node1 ~]# 
View Code

 

 

应用二

root@linux-node1 ~]# vim /srv/salt/base/web/apache.sls
apache-install:
  pkg.installed:
    - name: httpd

apache-service:
  service.running:
    - name: httpd
    - enable: True

        
[root@linux-node1 ~]# vim /srv/salt/base/top.sls        
        
base:
  'os:CentOS':
    - match: grain
    - web.apache

[root@linux-node1 ~]# salt '*' state.highstate

 

应用三  通过minion的配置文件定义grains
[root@web1 ~]# vim /etc/salt/minion
    grains:
       web:
         apache
[root@web1 ~]# systemctl restart salt-minion.service
    
    [root@web2 ~]# vim  /etc/salt/minion
    grains:
       web:
         nginx
    [root@web2 ~]# systemctl restart salt-minion.service
    
    [root@sm ~]# salt '*' saltutil.sync_grains
    web1:
    db1:
    cache:
    web2:
    db2:


    [root@sm ~]# salt -G 'web:apache' test.ping
    web1:
        True
    [root@sm ~]# salt -G 'web:nginx' test.ping
    web2:
        True


    [root@sm ~]# salt 'web1' grains.item web
    web1:
        ----------
        web:
            apache


    [root@sm ~]# salt 'web2' grains.item web
    web2:
        ----------
        web:
            nginx

应用四 在minion的/etc/salt下创建一个grains文件自定义grains键值对

例子一

[root@linux-node2 ~]# vim /etc/salt/grains
haha: linux-node1
[root@linux-node2 ~]# systemctl restart salt-minion

 

[root@linux-node1 ~]# salt '*' grains.get haha
linux-node1.localdomain:
linux-node2.localdomain:
    linux-node1
例子二

[root@linux-node2 ~]# vim /etc/salt/grains
haha: linux-node1


[root@linux-node1 ~]# salt '*' saltutil.sync_grains 不需要重启就可以获取值 相对于例子一

[root@linux-node1 ~]# salt '*' grains.get haha
linux-node1.localdomain:
linux-node2.localdomain:
    linux-node1

 

标签:02,运维,SaltStact,salt,grains,usr,linux,node1,root
来源: https://www.cnblogs.com/linux985/p/10534138.html