码迷,mamicode.com
首页 > 其他好文 > 详细

SaltStack 之 文件批量管理(hosts文件)

时间:2018-06-09 20:26:19      阅读:958      评论:0      收藏:0      [点我收藏+]

标签:saltstack

SaltStack 之 文件批量管理(hosts文件)

前期工作:

1.创建Salt所需的文件夹
[root@SlatStack-Master ~]# mkdir /srv/{salt,pillar}
[root@SlatStack-Master ~]# ll /srv/
total 8
drwxr-xr-x 2 root root 4096 Jun  9 18:56 pillar
drwxr-xr-x 2 root root 4096 Jun  9 18:56 salt

备注:
此文件夹的所在目录与/etc/salt/master 识识相关,配置固定文件存放位置

一、在/srv/salt/目录下创建host_file.sls文件(建议必须带后续)

[root@SlatStack-Master srv]# mkdir /srv/salt/files/             #创建存放替换的文件的目录
[root@SlatStack-Master salt]# vi hosts_file.sls
hosts_test:                                    #文件标识可随意(没空格)
     file.managed:                             #file:模块,managed:命令(两个空格)
         - name: /etc/hosts                   #目标从机的文件或目录(四个空格)
         - source: salt://files/hosts        #master主机的源文件存放位置,相对路径,并且在/srv目录必须要创建files目录,hosts为要准备用来替换的文件
         - user: root                               #所属主
         - group: root                            #所属组
         - mode: 644                             #权限

二、定义好/srv/salt/files/hosts的文件内容

[root@SlatStack-Master srv]# cat /srv/salt/files/hosts 
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
GZ15_ZABBIX     10.1.251.202
GZIFC_ZABBIX    10.3.151.25
GZXB_ZABBIX     10.2.151.16
SHJM_ZABBIX     10.32.151.15
SZPA_ZABBIX     10.16.151.20
BackupServer    10.3.151.193

三、通过master主机开始同步到所有从机的/etc/hosts文件中

[root@SlatStack-Master srv]# salt ‘*‘ state.sls hosts_file

备注:
    *:代表所有主机
    state:代表是一个模块
    sls:代表一个功能
    hosts_file:代表刚刚创建定义的文件,如前面功能那里没有sls,此时就要写全

四、命令返回结果:

    BackupServer:
      ID: hosts_test
Function: file.managed
    Name: /etc/hosts
  Result: True
 Comment: File /etc/hosts updated
 Started: 20:13:46.704409
Duration: 17.643 ms
 Changes:   
          ----------
          diff:
              ---  
              +++  
              @@ -1,3 +1,8 @@
               127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
               ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
              -###############20180609################
              +GZ15_ZABBIX  10.1.251.202
              +GZIFC_ZABBIX 10.3.151.25
              +GZXB_ZABBIX  10.2.151.16
              +SHJM_ZABBIX  10.32.151.15
              +SZPA_ZABBIX  10.16.151.20
              +BackupServer 10.3.151.193

Summary
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1
GZIFC_ZABBIX:
----------
      ID: hosts_test
Function: file.managed
    Name: /etc/hosts
  Result: True
 Comment: File /etc/hosts updated
 Started: 20:13:46.636542
Duration: 35.524 ms
 Changes:   
          ----------
          diff:
              ---  
              +++  
              @@ -1,2 +1,8 @@
               127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
               ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
              +GZ15_ZABBIX  10.1.251.202
              +GZIFC_ZABBIX 10.3.151.25
              +GZXB_ZABBIX  10.2.151.16
              +SHJM_ZABBIX  10.32.151.15
              +SZPA_ZABBIX  10.16.151.20
              +BackupServer 10.3.151.193

Summary
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1
GZXB_ZABBIX:
----------
      ID: hosts_test
Function: file.managed
    Name: /etc/hosts
  Result: True
 Comment: File /etc/hosts updated
 Started: 04:17:40.587127
Duration: 81.509 ms
 Changes:   
          ----------
          diff:
              ---  
              +++  
              @@ -1,2 +1,8 @@
               127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
               ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
              +GZ15_ZABBIX  10.1.251.202
              +GZIFC_ZABBIX 10.3.151.25
              +GZXB_ZABBIX  10.2.151.16
              +SHJM_ZABBIX  10.32.151.15
              +SZPA_ZABBIX  10.16.151.20
              +BackupServer 10.3.151.193

Summary
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1
SZPA_ZABBIX:
----------
      ID: hosts_test
Function: file.managed
    Name: /etc/hosts
  Result: True
 Comment: File /etc/hosts updated
 Started: 20:13:47.012765
Duration: 156.962 ms
 Changes:   
          ----------
          diff:
              ---  
              +++  
              @@ -1,2 +1,8 @@
               127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
               ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
              +GZ15_ZABBIX  10.1.251.202
              +GZIFC_ZABBIX 10.3.151.25
              +GZXB_ZABBIX  10.2.151.16
              +SHJM_ZABBIX  10.32.151.15
              +SZPA_ZABBIX  10.16.151.20
              +BackupServer 10.3.151.193

Summary
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1
SHJM_ZABBIX:
----------
      ID: hosts_test
Function: file.managed
    Name: /etc/hosts
  Result: True
 Comment: File /etc/hosts updated
 Started: 20:16:37.675228
Duration: 148.334 ms
 Changes:   
          ----------
          diff:
              ---  
              +++  
              @@ -1,2 +1,8 @@
              -127.0.0.1   localhost SH-Network localhost4 localhost4.localdomain4
              +127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
               ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
              +GZ15_ZABBIX  10.1.251.202
              +GZIFC_ZABBIX 10.3.151.25
              +GZXB_ZABBIX  10.2.151.16
              +SHJM_ZABBIX  10.32.151.15
              +SZPA_ZABBIX  10.16.151.20
              +BackupServer 10.3.151.193

Summary
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1
GZ15_ZABBIX:
----------
      ID: hosts_test
Function: file.managed
    Name: /etc/hosts
  Result: True
 Comment: File /etc/hosts updated
 Started: 20:16:11.194538
Duration: 280.379 ms
 Changes:   
          ----------
          diff:
              ---  
              +++  
              @@ -1,2 +1,8 @@
               127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
               ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
              +GZ15_ZABBIX  10.1.251.202
              +GZIFC_ZABBIX 10.3.151.25
              +GZXB_ZABBIX  10.2.151.16
              +SHJM_ZABBIX  10.32.151.15
              +SZPA_ZABBIX  10.16.151.20
              +BackupServer 10.3.151.193

Summary
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1

五、查看各主机是否生效

[root@SlatStack-Master srv]# salt ‘*‘ cmd.run ‘cat /etc/hosts‘
BackupServer:
        127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
        ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
        GZ15_ZABBIX 10.1.251.202
        GZIFC_ZABBIX        10.3.151.25
        GZXB_ZABBIX 10.2.151.16
        SHJM_ZABBIX 10.32.151.15
        SZPA_ZABBIX 10.16.151.20
        BackupServer        10.3.151.193
GZIFC_ZABBIX:
        127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
        ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
        GZ15_ZABBIX 10.1.251.202
        GZIFC_ZABBIX        10.3.151.25
        GZXB_ZABBIX 10.2.151.16
        SHJM_ZABBIX 10.32.151.15
        SZPA_ZABBIX 10.16.151.20
        BackupServer        10.3.151.193
SZPA_ZABBIX:
        127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
        ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
        GZ15_ZABBIX 10.1.251.202
        GZIFC_ZABBIX        10.3.151.25
        GZXB_ZABBIX 10.2.151.16
        SHJM_ZABBIX 10.32.151.15
        SZPA_ZABBIX 10.16.151.20
        BackupServer        10.3.151.193
GZ15_ZABBIX:
        127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
        ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
        GZ15_ZABBIX 10.1.251.202
        GZIFC_ZABBIX        10.3.151.25
        GZXB_ZABBIX 10.2.151.16
        SHJM_ZABBIX 10.32.151.15
        SZPA_ZABBIX 10.16.151.20
        BackupServer        10.3.151.193
GZXB_ZABBIX:
        127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
        ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
        GZ15_ZABBIX 10.1.251.202
        GZIFC_ZABBIX        10.3.151.25
        GZXB_ZABBIX 10.2.151.16
        SHJM_ZABBIX 10.32.151.15
        SZPA_ZABBIX 10.16.151.20
        BackupServer        10.3.151.193
SHJM_ZABBIX:
        127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
        ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
        GZ15_ZABBIX 10.1.251.202
        GZIFC_ZABBIX        10.3.151.25
        GZXB_ZABBIX 10.2.151.16
        SHJM_ZABBIX 10.32.151.15
        SZPA_ZABBIX 10.16.151.20
        BackupServer        10.3.151.193

SaltStack 之 文件批量管理(hosts文件)

标签:saltstack

原文地址:http://blog.51cto.com/12965094/2126777

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!