SaltStack 之 文件批量管理(hosts文件)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SaltStack 之 文件批量管理(hosts文件)相关的知识,希望对你有一定的参考价值。
SaltStack 之 文件批量管理(hosts文件)
前期工作:
1.创建Salt所需的文件夹
[[email protected] ~]# mkdir /srv/{salt,pillar}
[[email protected] ~]# 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文件(建议必须带后续)
[[email protected] srv]# mkdir /srv/salt/files/ #创建存放替换的文件的目录
[[email protected] 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的文件内容
[[email protected] 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文件中
[[email protected] 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
五、查看各主机是否生效
[[email protected] 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文件)的主要内容,如果未能解决你的问题,请参考以下文章