saltstack的安裝與常見模塊的使用

Salt 是一個配置管理系統,可以維護預約義狀態的遠程節點(好比,確保指定的報被安裝,指定的服務在運行)一個分佈式遠程執行系統,用來在遠程節點(能夠是單個節點,也能夠是任意規則挑選出來的節點)上執行命令和查詢數據 開發其的目的是爲遠程執行提供最好的解決方案,並使遠程執行變得更好,更快,更簡單 Saltstack(中國用戶組 www.saltstack.cn )基於 python 開發,c/s 架構,支持多平臺,比 puppet 輕量,在遠程執行命令時很是快捷,配置和使用比 puppet 容易,能實現 puppet 幾乎全部的功能。php

1、安裝與簡介

1.1 環境準備

準備兩臺安裝好 epel 源的 Linux 系統,這裏咱們系統使用的是 centos7,若是沒有的話,請進行安裝html

yum install -y epel-release

像批量管理工具都是 C/S 架構的,如下是用2臺設備的演示步驟:python

1)服務器 192.168.44.250 上安裝 salt-masterios

yum install salt-master -y

2)客戶端 192.168.44.132 上安裝 salt-minionnginx

yum install salt-minion -y

1.2 優點分析

1)salt 有 master 端和 minion 端,執行的信息比較穩定,不容易丟失信息,或者出現失聯主機的狀況;git

2)有封裝好的 http-api,咱們能夠直接啓動 salt-api 就能夠經過 http 協議進行調用。不須要本身進行第二次的封裝。web

1.3 祕鑰認證

Master 與 Minion 認證 minion 在第一次啓動時,會在 /etc/salt/pki/minion/(該路徑在 /etc/salt/minion 裏面設置)下自動生成 minion.pem(private key)和 minion.pub(public key),而後將 minion.pub 發送給master。spring

[root@V2 ~]# ll /etc/salt/pki/minion/
total 12
-rw-r--r-- 1 root root  451 Jun 29 16:24 minion_master.pub
-r-------- 1 root root 1675 Jun 29 16:24 minion.pem
-rw-r--r-- 1 root root  451 Jun 29 16:24 minion.pub

master 在接收到 minion 的 public key 後,經過 salt-key 命令 accept minion public key,這樣在 master 的 /etc/salt/pki/master/minions 下的將會存放以 minion id 命名的 public key,而後 master 就能對 minion 發送指令了。docker

[root@V1 ~]# ll /etc/salt/pki/master/minions
total 4
-rw-r--r-- 1 root root 451 Jun 29 16:50 client-zyy

 

2、具體配置

2.1 master服務端

saltstack 的配置文件屬於 YAML 格式,因此配置的時候必定注意語法shell

  • 每一個配置行前須要有空格
  • 每一個 ‘:’ 兩邊須要有空格
  • 數組中間加空格
  • 還有註釋掉的參數不能在 # 後邊加空格否則報錯

配置文件 vim /etc/salt/master,進行以下編輯

1)配置監聽 master地址

[root@V1 ~]# cat /etc/salt/master |grep -v ^$ |grep -v ^# |grep "interface"
interface : 192.168.44.250

2)添加自動驗證

[root@V1 ~]# cat /etc/salt/master |grep -v ^$ |grep -v ^# |grep "auto_accept"
auto_accept : True

salt-master 詳細配置可參照 salt-master配置文件詳解 。

2.2 minion客戶端

配置文件 vim /etc/salt/minion,進行以下編輯

一、添加master地址:master: 192.168.44.132

[root@V2 ~]# cat /etc/salt/minion |grep -v ^$ |grep -v ^# |grep "master"
master : 192.168.44.132
master_port : 4506

二、添加客戶端的id,用以在服務端區分客戶端機器

[root@V2 ~]# cat /etc/salt/minion |grep -v ^$ |grep -v ^# |grep "client-zyy"
id : client-zyy

 

2.3 啓動服務

服務端

systemctl start salt-master

客戶端

systemctl start salt-minion

 

2.4 日誌文件

服務端

/var/log/salt/master

客戶端

/var/log/salt/minion

 

2.5 監聽端口

分別服務端和客戶端查看默認監聽端口 4506

[root@V1 ~]# netstat -antlp|grep 4506
tcp 0 0 xxx.xxx.xxx.xxx:4506 0.0.0.0:* LISTEN 13458/python
tcp 0 0 xxx.xxx.xxx.xxx:4506 xxx.xxx.xxx.xxx:36196 ESTABLISHED 13458/python
tcp 0 0 xxx.xxx.xxx.xxx:56328 xxx.xxx.xxx.xxx:4506 TIME_WAIT -

說明服務已正常運行。

 

2.6 確認鏈接

在服務端查看客戶端是否已經鏈接成功

[root@V1 ~]# salt-key -L
Accepted Keys:
Denied Keys:
Unaccepted Keys:
Rejected Keys:

能夠看到沒有可用的客戶端,因而咱們應該在客戶端上進行查看 salt-minion 端口是否啓動,在這裏咱們看到是沒有啓動成功的,

[root@V2 ~]# systemctl status salt-minion
● salt-minion.service - The Salt Minion
Loaded: loaded (/usr/lib/systemd/system/salt-minion.service; disabled; vendor preset: disabled)
Active: active (running) since Fri 2018-06-29 16:27:31 CST; 3s ago
Main PID: 900 (salt-minion)
Tasks: 3
Memory: 25.2M
CGroup: /system.slice/salt-minion.service

├─900 /usr/bin/python /usr/bin/salt-minion

└─903 /usr/bin/python /usr/bin/salt-minion

Jun 29 16:27:31 V2 salt-minion[900]: [ERROR ] Error parsing configuration file: /etc/salt/minion - expected...tart>'
Jun 29 16:27:31 V2 salt-minion[900]: in "<string>", line 52, column 1:
Jun 29 16:27:31 V2 salt-minion[900]: master_port: 4506
Jun 29 16:27:31 V2 salt-minion[900]: ^
Jun 29 16:27:31 V2 salt-minion[900]: [ERROR ] Error parsing configuration file: /etc/salt/minion - expected...tart>'
Jun 29 16:27:31 V2 salt-minion[900]: in "<string>", line 52, column 1:
Jun 29 16:27:31 V2 salt-minion[900]: master_port: 4506
Jun 29 16:27:31 V2 salt-minion[900]: ^
Jun 29 16:27:32 V2 salt-minion[900]: [ERROR ] DNS lookup of 'salt' failed.
Jun 29 16:27:32 V2 salt-minion[900]: [ERROR ] Master hostname: 'salt' not found. Retrying in 30 seconds
Hint: Some lines were ellipsized, use -l to show in full.

從日誌中能夠看出是由於 minion 客戶端沒有解析 salt 主機名,因而咱們在 /etc/hosts 裏面添加以下

[root@V2 ~]# cat /etc/hosts
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.44.250 salt

而後從新啓動minion

[root@V2 ~]# systemctl restart salt-minion

再次在 master 服務端驗證有無客戶端的證書:salt-key -L

[root@V1 ~]# salt-key -L
Accepted Keys:
client-zyy
Denied Keys:
Unaccepted Keys:
Rejected Keys:

以上,基本環節已經完成,服務端已能夠執行客戶端的一些命令,示例以下:

[root@V1 ~]# salt '*' test.ping
client-zyy:
True

或者查看主機名命令

[root@V1 ~]# salt '*' cmd.run 'hostname'
client-zyy:
           V2

2.7 小示例

使用 ansible 安裝 minion,以後用 saltstack。

3、模塊介紹

3.1 Runner 模塊

runner 是個啥呢,runner 的寫法和 execution module 的寫法差很少。

不一樣的是,execution module 須要同步到 minions 上,而後在 minions 上面執行,

runner 則在 master 上直接用 salt-run 這個命令就能夠調用了,而不須要同步到 minions 上面

既然,runner 和 execution module 執行場景不一樣,那麼顯然他們操做的對象也不同。

一個 exection module 同步到一個 minion 上以後,幹好這一臺機器的活就 OK 了。

而 runner 在 master 執行,它乾的活,更加具備全局性,它可使用 master 上面的一切資源,能夠經過

salt 命令去間接的獲取 minions 上的資源。

runner 這東西就如同 "ls /home | grep lixc" ,"ls /home" 能夠理解爲 master 對 minions 的一系列操做,而後返回的結果經過管道交 "grep " 。而 runner 乾的活偏偏就是管道後面的活,對返回的結果作二次處理。

固然也能夠用 runner 去執行一系列的任務,下面咱們來試着自定義一個 runner。

1)查看 master 中配置文件的 runner 目錄

[root@V1 ~]# grep '#runner_dirs' /etc/salt/master 
#runner_dirs: []

先創建一個 runners 的存放文件夾,runner 的目錄能夠有多個,以列表的形式存在。咱們這裏把目錄放在 /srv/salt/_runners,以下:

[root@V1 ~]# grep '^runner_dirs' /etc/salt/master
runner_dirs: ['/srv/salt/_runners']

來看一下代碼:

[root@V1 /]# cat /srv/salt/_runners/lsdir.py   
#!/usr/bin/env python
import salt.client
import re 

def lsdir(directory):
   match = re.findall(r'^(/)',directory)

   if not match:
       directory = "/%s" % directory
   client = salt.client.LocalClient(__opts__['conf_file'])
   ret  = client.cmd('*', 'cmd.run',['/bin/ls /home'],timeout=1)

   for key,value in ret.items():
       value_list = value.split('\n')
       print "\033[31m%s\033[0m" % key
       for value in value_list:
           print "\033[32m%s\033[0m" % value

執行一下,咱們會獲得 minions 下 /home 路徑下的內容

[root@V1 /]# salt-run lsdir.lsdir home      
client-zyy
jenkins
memcached
test
web-
None

這時候咱們會發現,其實實現的功能就是 salt '*' cmd.run 'ls /home',只是咱們以自定義的形式展示了出來

[root@V1 /]# salt '*' cmd.run 'ls /home'
 client-zyy:
    jenkins
    memcached
    test
    web-
    None

這樣以後,咱們把 runner 模塊基本功能跑通了,runner 確定不止這麼點點用處了,到時候就得根據需求去用了。

 

3.2 Module 模塊

模塊是個啥呢?其實就是個文件。

salt模塊其實分爲兩類

一類模塊,叫 execution modules(執行模塊),這一類模塊是在命令行上面執行的,來個內置執行模塊的連接地址:FULL LIST OF BUILTIN EXECUTION MODULES

還有一類模塊,叫 state modules(狀態模塊),這一類模塊是在 state 裏面使用的,也來個內置 state模塊的連接:全部內置的STATE模塊列表

先說 execution modules 執行模塊是怎麼寫的吧。

執行模塊,默認的路徑在 /srv/salt/_modules 下

[root@V1 ~]# grep "#module_dirs" /etc/salt/master 
#module_dirs: <no default>
[root@V1 ~]# grep "^module_dirs" /etc/salt/master  
module_dirs : ['/srv/salt/_modules']

__salt__ 是個字典,是個啥字典呢,這個玩意,是屬於 salt 全局的一個字典,只要咱們定義的模塊被minion 加載後,就能夠用 __salt__ 了。

注意:這個 __salt__ 字典,不是在咱們模塊裏面本身定義的,而是 salt 全局的,咱們寫模塊的時候直接拿來用就 OK 了

OK,知道 __salt__ 是個啥以後,那個這東西里面都裝了些什麼玩意呢?

其實,__salt__ 這個字典裏面裝的 minion 上面全部的 modules 。__salt__ 的 key 是一個個的模塊名稱,value 則是模塊裏面的一個個函數。

下面我們看個例子,看看內建模塊是怎麼調用的。

咱們先自定義一個命名爲 foo.py 的模塊,很簡單,傳一個參數進去,而後返回一段話

[root@V1 /]# cat /srv/salt/_modules/foo.py 
#!/usr/bin/env python

def foo(name):
    return "I am {0}".format(name)

接着再自定義一個 命名爲 zhou.py 的模塊,目的是經過 __salt__ 調用咱們上面定義的那個 foo 模塊中的 foo 函數

[root@V1 /]# cat /srv/salt/_modules/zhou.py   
#!/usr/bin/env python

def zhou(name):
  return __salt__['foo.foo'](name)

而後咱們同步一下模塊

[root@V1 /]# salt '*' saltutil.sync_modules    
client-zyy:
    - modules.foo
    - modules.zhou

最後咱們能夠運行獲得結果

[root@V1 /]# salt '*' zhou.zhou zyy
client-zyy:
    I am zyy

從上面的演示過程當中,咱們能夠看出怎樣調用 __salt__ 裏面的模塊函數,且它的使用格式以下

__salt__['模塊.函數']('函數參數')

那麼咱們自定義的模塊,怎樣使用 grains,pillar,以及編寫文檔,還有自定義輸出格式呢?請繼續往下看

[root@V1 ~]# cat /srv/salt/_modules/zyy.py
#!/usr/bin/env python
__outputter__ = {
                'zyy': 'json'
                }
  '''指定輸出格式,__outputter__這個東西也是個字典,
  這個字典會綁定salt中的一個方法,
  當你以 "函數名:輸出格式" 這種形式加入__outputter__這個字典後,
  salt就會幫你把你指定的函數,按照指定的格式打印返回值。'''

def zyy(args):
    '''
    A function just one example
    CLI Example:
        salt '*' lisisi.lisisi args
    把這個函數的用法加到sys.doc裏面的
    '''
    grain = __grains__['chengge']
    # 經過salt默認的一個字典__grains__,你想去哪一個grain的時候,使用 __grains__['Id']

    pillar = __pillar__['group']
    # pillar 和 grains 一個樣,__pillar__['Id']

    result = __salt__['cmd.run'](args)
    # __salt__['模塊.函數']('函數參數')

    result = "{0},{1},{2}".format(grain,pillar,result)
    return  result

在實際中,咱們不該該在模塊中保留註釋,因此咱們會在去掉註釋以後

[root@V1 ~]# cat /srv/salt/_modules/zyy.py 
#!/usr/bin/env python

__outputter__ = {
                'zyy' : 'json'
                }

def zyy(args):
    '''
    A function just one example
    CLI Example:
        salt '*' zyy.zyy args
    '''
    result = __salt__['cmd.run'](args)
    result = "{0}".format(result)
    return  result

而後同步一下模塊到 minion 端

[root@V1 ~]# salt '*' saltutil.sync_modules
client-zyy:
    - modules.zyy

首先經過查看一下doc,看看這個函數是咋用的?

[root@V1 ~]# salt '*' -d zyy.zyy            
'zyy.zyy:'

    A function just one example
    CLI Example:
        salt '*' zyy.zyy args
    

[root@V1 ~]#

如今咱們就能夠執行了,傳個命令運行以下

[root@V1 ~]# salt '*' zyy.zyy 'ls /tmp' 
{
    "client-zyy": "Aegis-<Guid(5A2C30A2-A87D-490A-9281-6765EDAD7CBA)>\ngitaly-ruby064629261\nhsperfdata_root\nspring.log\nspring.log.1\nspring.log.2\nspring.log.3\nspring.log.4\nspring.log.5\nspring.log.6\nspring.log.7\nsystemd-private-ad4933dd5312402ebb22b63c1e18717e-mariadb.service-efkPrn\nsystemd-private-ad4933dd5312402ebb22b63c1e18717e-ntpd.service-ETSUmn\ntomcat-docbase.2235036041880545551.3336\ntomcat-docbase.232260128623980968.3336\ntomcat-docbase.4534970583944051382.2236\ntomcat-docbase.4539933346299648069.3336\ntomcat-docbase.5766346230451110185.9002\ntomcat-docbase.7852222648457097619.3336\ntomcat-docbase.938898103132580800.3335\ntomcat.1044739914805588690.3335\ntomcat.1700777959358680247.3336\ntomcat.3118119981664415691.3336\ntomcat.3182192429944317071.3336\ntomcat.3596014754434510879.9002\ntomcat.7288634415309080614.2236\ntomcat.8453255276092695862.3336"
}

關於執行模塊,官方文檔中還有這麼個功能,__virtual__, 這東西叫虛擬模塊,具體可參看 saltstack(七)modules(一) 瞭解一下。

咱們有三種方法能夠同步模塊

salt '*' state.highstate

salt '*' saltutil.sync_all

salt '*' saltutil.sync_modules

 

3.3 Grins 模塊

grains 實際上是一些靜態 data,對,是數據。這個數據是個啥結構呢?是 key: value 的結構

這些數據是屬於誰的?屬於 minion 的,也就是說 grains 是 minion 私有的數據,和別人不搭嘎的

當 minion 啓動的時候,這些數據會被加載到內存裏面,而後 grains 就保持不變了,即便你添加或者修改 grains,數據也不會改變,除非你重啓 minion。

固然萬事總留一線,凡事總有個例外嘛,咱們在 master 上面定義或者更新 grains,執行同步後,能夠修改 grains,就能夠不重啓 minion 而達到改變 grains 的目的。

其實還有一種方法來更新 grains 的,在master上執行 salt '*' state.highstate 或者在 minion 上執行,

salt-call state.highstate 也能夠更新 grains 。詳細這裏不贅述,具體可參考  saltstack(四)grains(一)和 saltstack(五)grains(二)

總的來講,grains 主要負責採集客戶端一些基本信息, 這個也徹底能夠自定義,能夠在客戶端自定義,而後自動彙報上來;也能夠從服務器端定義而後推下去,採集完後,再彙報上來。

1)打印全部 items 值

[root@V1 ~]# salt '*' grains.items
client-zyy:
    ----------
    SSDs:
    biosreleasedate:
        04/01/2014
    biosversion:
        rel-1.7.5-0-ge51488c-20140602_164612-nilsson.home.kraxel.org
    cpu_flags:
        - fpu
        - vme
        - de
        - pse
        - tsc
    ··· ···
    cpu_model:
        Intel(R) Xeon(R) CPU E5-2682 v4 @ 2.50GHz
    cpuarch:
        x86_64
    ··· ···
    id:
        client-zyy
    init:
        systemd
    ip4_interfaces:
        ----------
        docker0:
            - 192.168.0.1
        eth0:
            - xxx.xxx.xxx.xxx
        eth1:
            - xxx.xxx.xxx.xxx
        lo:
            - 127.0.0.1
        veth62589b9:
        veth6d70e12:
        veth8a157ff:
        veth911df84:
        vethaba1fd6:
        vethcd2bebc:
        vethed9f167:
    ip6_interfaces:
        ----------
        docker0:
        eth0:
        eth1:
        lo:
        veth62589b9:
        veth6d70e12:
        veth8a157ff:
        veth911df84:
        vethaba1fd6:
        vethcd2bebc:
        vethed9f167:

2)列出全部 items

[root@V1 ~]# salt '*' grains.ls
client-zyy:
    - SSDs
    - biosreleasedate
    - biosversion
    - cpu_flags
    - cpu_model
    - cpuarch
    - domain
    - fqdn
    - fqdn_ip4
    - fqdn_ip6
    - gpus
    - host
    - hwaddr_interfaces
    - id
    - init
    - ip4_interfaces
    - ip6_interfaces
    - ip_interfaces
    - ipv4
    - ipv6
    - kernel
    - kernelrelease
    ··· ···

3)查看單個 item 值

[root@V1 ~]# salt '*' grains.item shell
client-zyy:
    ----------
    shell:
        /bin/sh
[root@V1 ~]# salt '*' grains.item ipv4
client-zyy:
    ----------
    ipv4:
        - xxx.xxx.xxx.xxx
        - xxx.xxx.xxx.xxx
        - 127.0.0.1
        - 192.168.0.1

4)自定義 grains 數據

a. 在 minion 端實現

修改配置文件 /etc/salt/minion 中打開 default_include: minion.d/.conf

[root@V2 ~]# grep "^default_include" /etc/salt/minion   
default_include: minion.d/*.conf

在 /etc/salt/minion.d/ 目錄中添加須要增長的 items,文件類型與配置項 .conf 對應

[root@V2 ~]# cat /etc/salt/minion.d/tomcat.conf
grains:             #開頭需聲明是grains
  role: 
    - tomcat
    - db
  max_con: 123

而後重啓 minion,在 master 能夠查看到以下(本文實驗均只有一個 minion)

[root@V1 ~]# salt '*' grains.item  max_con  role
client-zyy:
    ----------
    max_con:
        123
    role:
        - tomcat
        - db

 

b. 在 master 端實現

在 /srv/salt/ 建立 _grains 目錄,通常是 py 文件,編寫 grains 文件,須要返回一個字典。

[root@V1 /]# cat /srv/salt/_grains/tomcat.py 
#!/usr/bin/env python

def tomcat_version():
        grains = {}
        grains['tomcat'] = '7.0.64'
        return grains

同步到 minion 端

[root@V1 /]# salt '*' saltutil.sync_modules
client-zyy:
[root@V1 /]# 
[root@V1 /]# salt '*' saltutil.sync_all
client-zyy:
    ----------
    beacons:
    grains:
        - grains.tomcat
    modules:
    output:
    renderers:
    returners:
    sdb:
    states:
    utils:

因而咱們能夠在 master 端查看到自定義的 grains 信息

[root@V1 /]# salt '*' grains.item tomcat
client-zyy:
    ----------
    tomcat:
        7.0.64

 

3.4 Pillar模塊

1)pillar是什麼?

pillar 是 salt 很是重要的一個組件,它用於給特定的 minion 定義任何你須要的數據, 這些數據能夠被salt 的其餘組件使用。pillar 在解析完成 後,是一個嵌套的 dict 結構;最上層的 key 是 minion ID,其 value 是該 minion 所擁有的 Pillar 數據;每個 value 也都是 key/value。這裏能夠看出 Pillar 的一個特色,Pillar 數據是與特定 minion 關聯的,也就是說每個 minion 都只能看到本身的數據, 因此 Pillar 能夠用來傳遞敏感數據 。

2)Pillar用在哪些地方?

a. 敏感數據
例如 ssh key,加密證書等,因爲Pillar使用獨立的加密 session,能夠確保這些敏感數據不被其餘minion看到。

b. 變量
能夠在 Pillar 中處理平臺差別性,好比針對不一樣的操做系統設置軟件包的名字,而後在 State 中引用。

c. 其餘任何數據
能夠在 Pillar 中添加任何須要用到的數據。好比定義用戶和UID的對應關係,mnion 的角色等。

d. 用在 Targetting 中
Pillar 能夠用來選擇 minion,使用 -I 選項。

3)怎樣定義 Pillar 數據?

a. master 配置文件中定義

默認狀況下,master 配置文件中的全部數據都添加到 Pillar 中,且對全部 minion 可用。若是要禁用這一默認值,能夠在 master 配置文件中添加以下數據,重啓服務後生效:

[root@V1 ~]# grep 'pillar_opts' /etc/salt/master  
# The pillar_opts option adds the master configuration file data to a dict in
#pillar_opts: False

b. 使用 SLS 文件定義 Pillar

Pillar 使用與 State 類似的 SLS 文件。Pillar 文件放在 master 配置文件中 pillar_roots 定義的目錄下。示例以下,重啓服務後生效:

[root@V1 ~]# egrep 'pillar_roots|base|/srv/pillar' /etc/salt/master 
pillar_roots:
  base:
    - /srv/salt/_pillar

c. 定義入口文件和數據

[root@V1 /]# cat /srv/salt/_pillar/top.sls 
base:
  '*':
    - data
[root@V1 /]# cat /srv/salt/_pillar/data.sls 
appname: website
flow:
  maxconn: 30000
  maxmen: 8G

4)如何知道 minion 擁有哪些 Pillar 數據?

在 master 上修改 Pillar 文件後,須要用如下命令刷新 minion 上的數據

[root@V1 ~]# salt '*' saltutil.refresh_pillar 
client-zyy:
    True

使用執行模塊 pillar。pillar 模塊有兩個 funtion:pillar.data 和 pillar.raw。示例以下

[root@V1 ~]# salt '*' pillar.data            
client-zyy:
    ----------

若是定義好的 pillar 不生效,建議重啓 master 端而且刷新一下試試。

[root@V1 ~]# salt '*' saltutil.refresh_pillar
client-zyy:
    True
[root@V1 ~]# salt '*' pillar.data            
client-zyy:
    ----------
    appname:
        website
    flow:
        ----------
        maxconn:
            30000
        maxmen:
            8G

5)Pillar中數據如何使用?

完成 pillar 配置後,咱們能夠在 state 和模板文件中引用,模板格式變量爲 {{ pillar變量 }}

{{ pillar['appname'] }}           # 表明值 website
{{ pillar['flow']['maxconn'] }}   # 表明值 30000

python API格式

pillar['appname']
pillar['flow']['maxconn']

a. 操做目標主機

使用 -I,大寫 i
選項表示使用 Pillar 來匹配 minion

[root@V1 ~]# salt -I 'appname:website' test.ping
client-zyy:
    True

b. 結合grains處理數據差別性

首先結合 grainx 的 id 信息來區分不一樣 id 的 maxcpu 和 maxmen;

修改上面的 data.sls ,語法爲 jinja2 ,查看jinja2中文文檔

[root@V1 /]# cat /srv/salt/_pillar/data.sls 
appname : website
ip : {{ grains['ipv4'][0] }}
flow:
  maxconn : 30000

刷新 pillar 數據,查看主機的 pillar 數據,看看是否有差別

[root@V1 _pillar]# salt \* saltutil.refresh_pillar
client-zyy:
    True
[root@V1 _pillar]# 
[root@V1 _pillar]# salt \* pillar.data ip         
client-zyy:
    ----------
    ip:
        xxx.xxx.xxx.xxx

 

3.5 cmd模塊

[root@PaulV1 /]# salt '*' cmd.run 'df -h'
client-zyy:
    Filesystem      Size  Used Avail Use% Mounted on
    /dev/vda1        99G   41G   53G  44% /
    devtmpfs        7.8G     0  7.8G   0% /dev
    tmpfs           7.8G   16K  7.8G   1% /dev/shm
    tmpfs           7.8G  1.8M  7.8G   1% /run
    tmpfs           7.8G     0  7.8G   0% /sys/fs/cgroup
    /dev/vdb1        99G   27G   67G  29% /var
    tmpfs           1.6G     0  1.6G   0% /run/user/0

 

3.6 ping模塊

[root@V1 /]# salt '*' test.ping -t 5            # 判斷 5s 內有無響應
client-zyy:
    True

 

3.7 cp 模塊

cp 模塊是小型文件服務器操做的主頁。 cp 模塊由 Salt 狀態系統 salt-cp 使用,可用於分發 Salt 文件服務器提供的文件。

詳細可參照 Salt文件服務器與salt-cp以及cp模塊

 

3.8 cron模塊

salt '*' cron.raw_cron root     (查看定時任務)
salt '*' cron.set_job root '*' '*' '*' '*' 1 /export/scripts/rm_log.sh 
salt '*' cron.rm_job root /export/scripts/rm_log.sh   (寫全沒效果)

3.9 dnsutil模塊

salt '*' dnsutil.hosts_append /etc/hosts 127.0.0.1 zzz.com
salt '*' dnsutil.hosts_remove /etc/hosts zzz.com

3.10 file模塊

salt '*' file.chown /etc/passwd root root
salt '*' file.copy /path/to/src /path/to/dst
salt '*' file.file_exists /etc/hosts
salt '*' file.directory_exists /etc/
salt '*' file.get_mod /etc/passwd
salt '*' file.set_mod /etc/passwd 0644
salt '*' file.mkdir /tmp/test
salt '*' file.sed /export/servers/nginx/conf/nginx.conf 'debug' 'warn'
salt '*' file.append /tmp/test.txt "welcome xiang"
salt '*' file.remove /tmp/1.txt

3.11 network模塊

salt '*' network.dig www.qq.com
salt '*' network.ping www.qq.com
salt '*' network.ip_addrs

3.12 pkg包管理模塊

# 管理yum, apt-get等
salt '*' pkg.install php
salt '*' pkg.remove php
salt '*' pkg.upgrade    (升級全部的軟件包)

3.13 service模塊

salt '*' service.enable nginx
salt '*' service.disable nginx
salt '*' service.restart nginx

 

 

參考資料

1. elasticsearch報錯expected <block end>, but found BlockMappingStart解決方法

2. salt-master配置文件詳解

3. saltstack(十一)runners

4. saltstack(七)modules(一)

5. saltstack(四)grains(一)

6. saltstack(五)grains(二)

7. salt實戰系列-grains和pillar學習 做者:擼大師

8. saltstack管理八之經常使用執行模塊

9. SALT文件服務器

10. Salt文件服務器與salt-cp以及cp模塊

相關文章
相關標籤/搜索