配置管理是基于远程执行的,实现的是模版引擎的功能
top file的作用:1 指定环境 2 指定Minion 3 指定需要加载的模块
top file必须放在/srv/salt/base下面:
[root@linux-node1 salt]# ll /srv/salt/base/
总用量 8
-rw-r--r-- 1 root root 172 11月 16 21:58 apache.sls
-rw-r--r-- 1 root root 53 11月 16 22:43 top.sls
[root@linux-node1 salt]# vim /etc/salt/master
419 file_roots:
420 base:
421 - /srv/salt/base
422 prod:
423 - /srv/salt/prod
424 test:
425 - /srv/salt/test
根据/etc/salt/master文件中的配置来创建对应的目录
[root@linux-node1 salt]# mkdir /srv/salt/{test,base,prod}
然后重新启动salt-master
[root@linux-node1 salt]# /etc/init.d/salt-master restart
Stopping salt-master daemon: [确定]
Starting salt-master daemon: [确定]
应用场景1:修改minion上面的dns文件
在基础环境下创建一个files目录,放范例文件
[root@linux-node1 ~]# mkdir /srv/salt/base/files
[root@linux-node1 ~]# vim /srv/salt/base/dns.sls
/etc/resolv.conf:
file.managed:
- source: salt://files/resolv.conf
- user: root
- group: root
- mode: 644
[root@linux-node1 base]# cp /etc/resolv.conf ./files/ 将机器上面的/etc/resolv.conf文件拷贝到/srv/salt/base/files/目录下,将第一行删除,如下:
执行状态:
[root@linux-node1 ~]# salt '*' state.sls dns =====================>直接执行状态,如下是返回结果
linux-node1.example.com:
----------
ID: /etc/resolv.conf =====================>文件ID
Function: file.managed =====================>状态和模块
Result: True =====================>返回结果
Comment: File /etc/resolv.conf updated =====================>输出状态显示
Started: 13:05:20.253533 =====================>开始时间
Duration: 71.313 ms =====================>执行时间
Changes: =====================>如下是文件改变的地方
----------
diff:
---
+++
@@ -1,2 +1,1 @@
-# Generated by NetworkManager
nameserver 10.0.0.2
Summary
------------
Succeeded: 1 (changed=1)
Failed: 0
------------
Total states run: 1
linux-node2.example.com:
----------
ID: /etc/resolv.conf
Function: file.managed
Result: True
Comment: File /etc/resolv.conf updated
Started: 13:05:20.500400
Duration: 80.807 ms
Changes:
----------
diff:
---
+++
@@ -1,2 +1,1 @@
-# Generated by NetworkManager
nameserver 10.0.0.2
Summary
------------
Succeeded: 1 (changed=1)
Failed: 0
------------
Total states run: 1
[root@linux-node1 base]# cat files/resolv.conf
nameserver 10.0.0.2
[root@linux-node1 ~]# cat /etc/resolv.conf
nameserver 10.0.0.2
[root@linux-node2 ~]# cat /etc/resolv.conf
nameserver 10.0.0.2
结论:linux-node1和linux-node2上面的/etc/resolv.conf文件都修改成功,代表状态执行成功。
场景2: 用高级状态修改文件:
[root@linux-node1 ~]# vim /srv/salt/base/files/resolv.conf
#hehe
nameserver 10.0.0.2
[root@linux-node1 ~]# cat /srv/salt/base/top.sls
base: ===========================>指定的是哪个环境
'*': ===========================>指定哪台minion
- dns ===========================>指定哪个状态模块
执行结果展示:
[root@linux-node1 ~]# salt '*' state.highstate
linux-node2.example.com:
----------
ID: /etc/resolv.conf
Function: file.managed
Result: True
Comment: File /etc/resolv.conf updated
Started: 13:19:01.723665
Duration: 48.675 ms
Changes:
----------
diff:
---
+++
@@ -1,1 +1,3 @@
+#hehe ========>此为添加的地方
nameserver 10.0.0.2
+
Summary
------------
Succeeded: 1 (changed=1)
Failed: 0
------------
Total states run: 1
linux-node1.example.com:
----------
ID: /etc/resolv.conf
Function: file.managed
Result: True
Comment: File /etc/resolv.conf updated
Started: 13:19:01.766432
Duration: 33.202 ms
Changes:
----------
diff:
---
+++
@@ -1,1 +1,3 @@
+#hehe ========>此为添加的地方
nameserver 10.0.0.2
+
Summary
------------
Succeeded: 1 (changed=1)
Failed: 0
------------
Total states run: 1
如上所示,文件修改成功
老男孩网址:http://www.etiantian.org
qq:406564728