一、为不同的环境设置不同的文件目录
1.1 修改配置文件 /etc/salt/master
[root@node1 salt]# vim /etc/salt/master
file_roots: base: - /srv/salt/base dev: - /srv/salt/dev prod: - /srv/salt/prod
1.2 创建对应的文件夹
[root@node1 salt]# mkdir -p /srv/salt/{base,dev,prod}
1.3 重启服务
[root@node1 salt]# systemctl restart salt-master
二、文件管理
2.1准备入口文件
更改 base 环境的 dns 配置,首先在 base 文件夹下创建入口文件 top.sls
[root@node1 salt]# cd /srv/salt/base/
[root@node1 base]# vim top.sls
base:
'web1':
- dns_file
2.2 配置分发文件说明
[root@node1 base]# vim dns_file.sls
resolv_conf: file.managed: - name: /etc/resolv.conf - source: salt://files/resolv.conf - user: root - group: root - mode: 644
2.3 执行文件分发操作
[root@node1 base]# mkdir files
[root@node1 base]# vim files/resolv.conf
nameserver 8.8.8.8
2.4 测试
[root@node1 base]# salt '*' state.highstate saltenv=base test
web2: ---------- ID: states Function: no.None Result: False Comment: No Top file or master_tops data matches found. Changes: Summary for web2 ------------ Succeeded: 0 Failed: 1 ------------ Total states run: 1 Total run time: 0.000 ms web3: ---------- ID: states Function: no.None Result: False Comment: No Top file or master_tops data matches found. Changes: ………… web1: ---------- ID: resolv_conf Function: file.managed Name: /etc/resolv.conf Result: None Comment: The file /etc/resolv.conf is set to be changed Started: 17:33:30.626072 Duration: 48.745 ms Changes: ---------- diff: --- +++ @@ -1,3 +1 @@ -# Generated by NetworkManager -search localdomain -nameserver 172.25.254.2 +nameserver 8.8.8.8 Summary for web1 ------------ Succeeded: 1 (unchanged=1, changed=1) Failed: 0 ------------ Total states run: 1 Total run time: 48.745 ms
2.5 运行
[root@node1 base]# salt '*' state.highstate saltenv=base False
2.6 检测
[root@node1 base]# salt 'web1' cmd.run 'cat /etc/resolv.conf'
2.7 修改所有节点 的 resolv.conf 配置
给issue 文件追加内容
[root@node1 base]# vim top.sls
base: 'L@web1,web2,web3,db1,db2': - dns- issue - rpm_pkgs
[root@node1 base]# vim dns_file.sls
resolv_conf: file.managed: - name: /etc/resolv.conf - source: salt://files/resolv.conf - user: root - group: root - mode: 644 - template: jinja - defaults: DNS_IP: 192.168.113.254
[root@node1 base]# mkdir -p /srv/salt/base/files
[root@node1 base]# vim files/resolv.conf
nameserver 8.8.4.4
[root@node1 base]# vim issue.sls
issue_file: file.append: - name: /etc/issue - text: - Red Hat Enterprise Linux 7 - Tedu Ltd Server
[root@node1 base]# vim rpm_pkgs.sls
rpms: file.directory: - name: /opt/rpm_pkgs - user: root - group: root - dir_mode: 755 - file_mode: 644
[root@node1 base]# salt '*' state.highstate saltenv=base test=True
web1: ---------- ID: resolv_conf Function: file.managed Name: /etc/resolv.conf Result: None Comment: The file /etc/resolv.conf is set to be changed Started: 20:09:06.189301 Duration: 893.458 ms Changes: ---------- diff: --- +++ @@ -1,3 +1 @@ -# Generated by NetworkManager -search localdomain -nameserver 172.25.254.2 +nameserver 8.8.4.4 ---------- ID: issue_file Function: file.append Name: /etc/issue Result: None Comment: File /etc/issue is set to be updated Started: 20:09:07.082933 Duration: 2.67 ms Changes: ---------- diff: --- +++ @@ -1,3 +1,5 @@ \S Kernel \r on an \m +Red Hat Enterprise Linux 7 +Tedu Ltd Server ---------- ID: rpms Function: file.directory Name: /opt/rpm_pkgs Result: None Comment: The following files will be changed: /opt/rpm_pkgs: directory - new Started: 20:09:07.085724 Duration: 0.432 ms Changes: Summary for web1 ------------ Succeeded: 3 (unchanged=3, changed=2) Failed: 0 ------------ Total states run: 3 Total run time: 896.560 ms web2: ---------- ID: resolv_conf Function: file.managed Name: /etc/resolv.conf Result: None Comment: The file /etc/resolv.conf is set to be changed Started: 20:09:06.520386 Duration: 1121.741 ms Changes: ---------- diff: --- +++ @@ -1,3 +1 @@ -# Generated by NetworkManager -search localdomain -nameserver 172.25.254.2 +nameserver 8.8.4.4 ---------- ID: issue_file Function: file.append Name: /etc/issue Result: None Comment: File /etc/issue is set to be updated Started: 20:09:07.642292 Duration: 2.611 ms Changes: ---------- diff: --- +++ @@ -1,3 +1,5 @@ \S Kernel \r on an \m +Red Hat Enterprise Linux 7 +Tedu Ltd Server ---------- ID: rpms Function: file.directory Name: /opt/rpm_pkgs Result: None Comment: The following files will be changed: /opt/rpm_pkgs: directory - new Started: 20:09:07.645019 Duration: 0.426 ms Changes: Summary for web2 ------------ Succeeded: 3 (unchanged=3, changed=2) Failed: 0 ------------ Total states run: 3 Total run time: 1.125 s db1: ---------- ID: resolv_conf Function: file.managed Name: /etc/resolv.conf Result: None Comment: The file /etc/resolv.conf is set to be changed Started: 20:09:06.055239 Duration: 1069.927 ms Changes: ---------- diff: --- +++ @@ -1,4 +1 @@ -# Generated by NetworkManager -search localdomain -nameserver 192.168.2.1 -nameserver 172.25.254.2 +nameserver 8.8.4.4 ---------- ID: issue_file Function: file.append Name: /etc/issue Result: None Comment: File /etc/issue is set to be updated Started: 20:09:07.125304 Duration: 2.857 ms Changes: ---------- diff: --- +++ @@ -1,3 +1,5 @@ \S Kernel \r on an \m +Red Hat Enterprise Linux 7 +Tedu Ltd Server ---------- ID: rpms Function: file.directory Name: /opt/rpm_pkgs Result: None Comment: The following files will be changed: /opt/rpm_pkgs: directory - new Started: 20:09:07.128287 Duration: 0.441 ms Changes: Summary for db1 ------------ Succeeded: 3 (unchanged=3, changed=2) Failed: 0 ------------ Total states run: 3 Total run time: 1.073 s web3: ---------- ID: resolv_conf Function: file.managed Name: /etc/resolv.conf Result: None Comment: The file /etc/resolv.conf is set to be changed Started: 20:09:06.368308 Duration: 915.69 ms Changes: ---------- diff: --- +++ @@ -1,3 +1 @@ -# Generated by NetworkManager -search localdomain -nameserver 172.25.254.2 +nameserver 8.8.4.4 ---------- ID: issue_file Function: file.append Name: /etc/issue Result: None Comment: File /etc/issue is set to be updated Started: 20:09:07.284136 Duration: 3.062 ms Changes: ---------- diff: --- +++ @@ -1,3 +1,5 @@ \S Kernel \r on an \m +Red Hat Enterprise Linux 7 +Tedu Ltd Server ---------- ID: rpms Function: file.directory Name: /opt/rpm_pkgs Result: None Comment: The following files will be changed: /opt/rpm_pkgs: directory - new Started: 20:09:07.287324 Duration: 0.45 ms Changes: Summary for web3 ------------ Succeeded: 3 (unchanged=3, changed=2) Failed: 0 ------------ Total states run: 3 Total run time: 919.202 ms db2: ---------- ID: resolv_conf Function: file.managed Name: /etc/resolv.conf Result: None Comment: The file /etc/resolv.conf is set to be changed Started: 20:09:06.578547 Duration: 493.823 ms Changes: ---------- diff: --- +++ @@ -1,3 +1 @@ -# Generated by NetworkManager -search localdomain -nameserver 172.25.254.2 +nameserver 8.8.4.4 ---------- ID: issue_file Function: file.append Name: /etc/issue Result: None Comment: File /etc/issue is set to be updated Started: 20:09:07.072509 Duration: 2.762 ms Changes: ---------- diff: --- +++ @@ -1,3 +1,5 @@ \S Kernel \r on an \m +Red Hat Enterprise Linux 7 +Tedu Ltd Server ---------- ID: rpms Function: file.directory Name: /opt/rpm_pkgs Result: None Comment: The following files will be changed: /opt/rpm_pkgs: directory - new Started: 20:09:07.075408 Duration: 0.423 ms Changes: Summary for db2 ------------ Succeeded: 3 (unchanged=3, changed=2) Failed: 0 ------------ Total states run: 3 Total run time: 497.008 ms