使用saltstack工具安裝一個服務

grains工具

grains是在minion啓動時收集到的一些信息,如操作系統類型、網卡ip、內核版本、cpu架構等信息
salt 'select hostname' grains.ls   列出所有grains項目名字
sakt 'select hostname' grains.items  列出所有的grains項目與值
grains的信息並不是動態的,不會實時變更,只有在minion啓動或重啓時纔會被收集到
可以根據grains收集到的信息,做配置管理工作。grains也支持自定義信息

查看grains記錄的項目

[root@nfs3 ~]# salt 'localzabbix.com' grains.ls
localzabbix.com:
    - SSDs
    - biosreleasedate
    - biosversion
    - cpu_flags
    - cpu_model
    - cpuarch
    - disks
    - dns
    - domain
    - fqdn
-------------省略

查詢出grains項目的值
這裏列出部分grains獲取的信息

[root@nfs3 ~]# salt 'localzabbix.com' grains.items
localzabbix.com:
    ----------
    SSDs:
    biosreleasedate:
        05/19/2017
    init:
        systemd
    ip4_gw:
        192.168.1.1
    ip4_interfaces:
        ----------
        ens33:
            - 192.168.1.223
        ens34:
            - 192.168.2.33
        lo:
            - 127.0.0.1
    ip6_gw:
    os:
        CentOS
    os_family:
        RedHat
    osarch:
        x86_64
    oscodename:
        CentOS Linux 7 (Core)
    osfinger:
        CentOS Linux-7
    osfullname:
        CentOS Linux
    osmajorrelease:
        7
    osrelease:
        7.3.1611

自定義grains值
在客戶端修改自定義值,並重啓客戶端上的minion服務,自定義信息要以空格分隔

[root@nfs2 ~]# cat /etc/salt/grains 
role: nginx
env: tes
[root@nfs2 ~]# systemctl restart salt-minion

在服務端執行salt ‘select hostname’ grains.itme role env來查詢客戶端的自定義grains信息

[root@nfs3 ~]# salt 'localzabbix.com' grains.item role env
localzabbix.com:
    ----------
    env:
        tes
    role:
        nginx

在服務端指定自定義信息來對客戶端進行操作,-G指定一個定義過grains信息的組
執行salt -G role:nginx cmd.run 'netstat -ntlp' 的格式,對標籤爲role:nginx的執行端口查詢操作
查詢過程如下

[root@nfs3 ~]# salt -G role:nginx cmd.run 'netstat -ntlp'
localzabbix.com:
    Active Internet connections (only servers)
    Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name    
    tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1623/master         
    tcp 0 0 192.168.1.223:27017 0.0.0.0:* LISTEN 1648/mongod         
    tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 1648/mongod         
    tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1344/sshd           
    tcp6 0 0 ::1:25 :::* LISTEN 1623/master         
    tcp6 0 0 :::3306 :::* LISTEN 1581/mysqld         
    tcp6 0 0 :::22 :::* LISTEN 1344/sshd

pillar管理工具

pillar和grains不一樣的地方在於pillar是在master上配置定義的,並且是針對minion定義的信息。像一些比較重要的數據(密碼)可以存在pillar裏,也可以在pillar中定義變量
自定義pillar配置文件
修改/etc/salt/master文件,去掉pillar_roots配置下的兩行註釋

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

------------省略的配置文件內容
pillar_roots:
  base:
    - /srv/pillar

定義以配置文件對象的方式
重啓加載master配置後,再創建master需要使用到的/srv/pillar目錄。在這個目錄下創建配置文件
創建top的主配置文件,用於控制子配置文件
top配置文件可以對多個主機名進行並列配置,對每個主機配置不同的匹配對象
注意子配置文件需要加上sls後綴結尾的命名方式

[root@nfs3 /]# mkdir /srv/pillar
[root@nfs3 /]# cd /srv/pillar
[root@nfs3 pillar]# cat top.sls 
base:
  'localzabbix.com':
    - test1
  'nfs3':
    - test2

字配置文件指定值

[root@nfs3 pillar]# cat test1.sls
conf: /etc/123.conf
[root@nfs3 pillar]# cat test2.sls
dir: /data/123.conf

重新加載pillar的配置,然後使用pillar.itme查詢匹配對象

[root@nfs3 pillar]# salt '*' saltutil.refresh_pillar
localzabbix.com:
    True
nfs3:
    True
[root@nfs3 pillar]# salt '*' pillar.items conf dir
localzabbix.com:
    ----------
    conf:
        /etc/123.conf
    dir:
nfs3:
    ----------
    conf:
    dir:
        /data/123.conf

對指定對象的主機配置文件執行命令
在master(192.168.1.115)上執行其他主機ip查詢操作進行測試

[root@nfs3 pillar]# salt -I 'conf:/etc/123.conf' cmd.run 'ifconfig ens33'
localzabbix.com:
    ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
            inet 192.168.1.223 netmask 255.255.255.0 broadcast 192.168.1.255
            inet6 fe80::20c:29ff:fedc:8559 prefixlen 64 scopeid 0x20<link>
            ether 00:0c:29:dc:85:59 txqueuelen 1000 (Ethernet)
            RX packets 87399 bytes 5707598 (5.4 MiB)
            RX errors 0 dropped 0 overruns 0 frame 0
            TX packets 3065 bytes 477805 (466.6 KiB)
            TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

安裝配置文件httpd

修改master服務端的主配置文件,找到file_roots行。打開file_roots的配置

[root@nfs3 ~]# vim /etc/salt/master
​
------------省略的配置文件內容
file_roots:
  base:
    - /srv/salt
[root@nfs3 salt]# systemctl restart salt-master

在/srv/salt中創建配置文件
打開master主配置文件後重啓生效, 然後再修改top.sls配置文件
在top.sls文件中配置指定一個服務安裝配置文件
在服務安裝的配置文件中指定安裝的軟件包和啓動服務的名稱
top.sls配置內容

[root@nfs3 /]# mkdir /srv/salt
[root@nfs3 /]# cd /srv/salt/
[root@nfs3 salt]# cat top.sls 
base:
  '*':
    - httpd

針對top.sls文件中的配置內容,創建httpd.sls服務配置文件
-name:  指定安裝的服務,如果只有一個服務要指定,就可以寫成-name: httpd 可以不用寫成再換行的格式
httpd-service是id的名字,自定義的服務名稱,pkg.installed爲安裝軟件包的安裝函數,下面則是指定安裝包的名稱,service.running也是一個函數,用於保障軟件包安裝完成後,來正常的啓動服務,enable是True表示設置爲開機啓動

[root@nfs3 salt]# cat httpd.sls 
httpd-service:
  pkg.installed:
    - names:
      - httpd
      - httpd-devel
  service.running:
    - name: httpd
    - enable: True

使用pillar配置執行安裝
在服務端爲客戶機上安裝httpd服務來測試

[root@nfs3 salt]# salt 'localzabbix.com' state.highstate
localzabbix.com:
----------
          ID: httpd-service
    Function: pkg.installed
        Name: httpd
      Result: True
     Comment: The following packages were installed/updated: httpd
     Started: 17:49:53.926557
    Duration: 63447.741 ms
     Changes:   
--------------------------省略
----------
          ID: httpd-service
    Function: service.running
        Name: httpd
      Result: True
     Comment: Service httpd has been enabled, and is running
     Started: 17:51:25.168861
    Duration: 707.817 ms
     Changes:   
              ----------
              httpd:
                  True
​
Summary for localzabbix.com
------------
Succeeded: 3 (changed=3)
Failed: 0
------------
Total states run: 3
Total run time: 88.751 s

在服務端爲客戶端安裝軟件
客戶端上產生的安裝進程

[root@nfs2 ~]# ps -aux |grep yum
root 4695 33.5 6.1 290296 61856 ? D 17:50 0:00 /usr/bin/python /usr/bin/yum -y install httpd
root 4697 0.0 0.0 112708 916 pts/0 R+ 17:50 0:00 grep --color=auto yum

客戶端服務安裝結果和啓動進程

[root@nfs2 ~]# ps -aux |grep httpd
root 4950 0.0 0.5 221976 4992 ? Ss 17:51 0:00 /usr/sbin/httpd -DFOREGROUND
apache 4951 0.0 0.2 221976 2952 ? S 17:51 0:00 /usr/sbin/httpd -DFOREGROUND
apache 4952 0.0 0.2 221976 2952 ? S 17:51 0:00 /usr/sbin/httpd -DFOREGROUND
apache 4953 0.0 0.2 221976 2952 ? S 17:51 0:00 /usr/sbin/httpd -DFOREGROUND
apache 4954 0.0 0.2 221976 2952 ? S 17:51 0:00 /usr/sbin/httpd -DFOREGROUND
apache 4955 0.0 0.2 221976 2952 ? S 17:51 0:00 /usr/sbin/httpd -DFOREGROUND
root 4999 0.0 0.0 112704 964 pts/0 R+ 18:10 0:00 grep --color=auto httpd

配置管理文件

首先打開master中的file_roots配置,指定總控制的配置文件top.sls位置
然後在/srv/salt/目錄下創建管理的top.sls配置文件

[root@nfs3 salt]# cat top.sls 
base:
  '*':
    - httpd
    - test1

再創建top.sls的下級管理文件,這個文件用於向客戶端來發送配置文件
name 中指定的是發送給客戶端文件保存的/目錄/文件名
source 指定的是服務端用來發送到所有的客戶端的一個模版配置文件所保存位置,所有客戶端都可以使用這個文件來加載服務,這個模版文件保存位置是file_roots中指定的base的相對路徑,如這裏解釋爲在/srv/salt/test這個目錄下
user/group   指定下發的文件的所屬主所屬組
mode  指定下發文件的權限
配置文件中需要注意是否拼寫錯誤

[root@nfs3 salt]# vim test1.sls
​
file_test:
  file.managed:
    - name: /tmp/mico.com
    - source: salt://test/1.txt
    - user: root
    - group: root
    - mode: 600

拷貝一個文件到相對路徑test目錄下,執行state.highstate下發文件進行測試

使用saltstack工具安裝一個服務

[root@nfs3 test]# cp /etc/inittab 1.txt
[root@nfs3 test]# ls
1.txt
[root@nfs3 test]# salt 'localzabbix.com' state.highstate
localzabbix.com:
----------
          ID: file_test
    Function: file.managed
        Name: /tmp/mico.com
      Result: True
     Comment: File /tmp/mico.com updated
     Started: 18:43:40.918619
    Duration: 193.675 ms
     Changes:   
              ----------
              diff:
                  New file
​
Summary for localzabbix.com
------------
Succeeded: 1 (changed=1)
Failed: 0
------------
Total states run: 1
Total run time: 193.675 ms

查看客戶端下是否有這個文件存在
查看文件權限和文件的內容
使用saltstack工具安裝一個服務

[root@nfs2 ~]# ll /tmp/mico.com 
-rw------- 1 root root 511 Dec 4 18:43 /tmp/mico.com
[root@nfs2 ~]# cat /tmp/mico.com 
# inittab is no longer used when using systemd.
#
# ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
#
# Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target
#
# systemd uses 'targets' instead of runlevels. By default, there are two main targets:
#
# multi-user.target: analogous to runlevel 3
# graphical.target: analogous to runlevel 5
# To view current default target, run:
# systemctl get-default
#
# To set a default target, run:
# systemctl set-default TARGET.target

總結:如果想要批量修改服務配置文件,可先修改一個模版配置文件,然後通過saltstack批量推送到minion端上去

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章