saltstack 实现系统初始化
1、整体结构如下
[root@zabbix init]# pwd /srv/salt/base/init [root@zabbix init]# ll total 20 -rw-r--r-- 1 root root 171 Jun 28 21:11 audit.sls -rw-r--r-- 1 root root 129 Mar 20 2017 dns.sls -rw-r--r-- 1 root root 70 Jun 28 21:22 env_init.sls drwxr-xr-x 2 root root 25 Jun 28 20:57 files -rw-r--r-- 1 root root 88 Jun 28 21:04 history.sls -rw-r--r-- 1 root root 227 Jun 28 21:19 sysctl.sls
2、状态文件内容如下
[root@zabbix init]# cat audit.sls /etc/bashrc: file.append: - text: - export PROMPT_COMMAND='{ msg=$(history 1 | { read x y; echo $y; });logger "[euid=$(whoami)]":$(who am i):[`pwd`]"$msg"; }' [root@zabbix init]# cat dns.sls /etc/resolv.conf: file.managed: - source: salt://init/files/resolv.conf - user: root - gourp: root - mode: 644[root@zabbix init]# cat history.sls /etc/profile: file.append: - text: - export HISTTIMEFORMAT="%F %T `whoami`" [root@zabbix init]# cat sysctl.sls net.ipv4.ip_local_port_range: sysctl.present: - value: 10000 65000 fs.file-max: sysctl.present: - value: 1000000 net.ipv4.ip_forward: sysctl.present: - value: 1 vm.swappiness: sysctl.present: - value: 0 [root@zabbix init]# cat env_init.sls include: - init.dns - init.history - init.audit - init.sysctl [root@zabbix init]# cat /srv/salt/base/top.sls base: '*': - init.env_init
3、执行结果
[root@zabbix init]# salt '*' state.highstate test=True #可以先测试一下
[root@zabbix init]# salt '*' state.highstate test=True
zabbix:
----------
ID: /etc/resolv.conf
Function: file.managed
Result: True
Comment: The file /etc/resolv.conf is in the correct state
Started: 21:30:07.452387
Duration: 38.22 ms
Changes:
----------
ID: /etc/profile
Function: file.append
Result: None
Comment: File /etc/profile is set to be updated
Started: 21:30:07.490775
Duration: 3.489 ms
Changes:
----------
diff:
---
+++
@@ -74,3 +74,4 @@
unset i
unset -f pathmunge
+export HISTTIMEFORMAT="%F %T `whoami`"
----------
ID: /etc/bashrc
Function: file.append
Result: None
Comment: File /etc/bashrc is set to be updated
Started: 21:30:07.494616
Duration: 3.249 ms
Changes:
----------
diff:
---
+++
@@ -90,3 +90,4 @@
unset -f pathmunge
fi
# vim:ts=4:sw=4
+export PROMPT_COMMAND='{ msg=$(history 1 | { read x y; echo $y; });logger "[euid=$(whoami)]":$(who am i):[`pwd`]"$msg"; }'
----------
ID: net.ipv4.ip_local_port_range
Function: sysctl.present
Result: None
Comment: Sysctl option net.ipv4.ip_local_port_range might be changed, we failed to check config file at /etc/sysctl.d/99-salt.conf. The file is either unreadable, or missing.
Started: 21:30:07.499427
Duration: 42.356 ms
Changes:
----------
ID: fs.file-max
Function: sysctl.present
Result: None
Comment: Sysctl option fs.file-max might be changed, we failed to check config file at /etc/sysctl.d/99-salt.conf. The file is either unreadable, or missing.
Started: 21:30:07.542193
Duration: 68.585 ms
Changes:
----------
ID: net.ipv4.ip_forward
Function: sysctl.present
Result: None
Comment: Sysctl option net.ipv4.ip_forward might be changed, we failed to check config file at /etc/sysctl.d/99-salt.conf. The file is either unreadable, or missing.
Started: 21:30:07.610971
Duration: 40.195 ms
Changes:
----------
ID: vm.swappiness
Function: sysctl.present
Result: None
Comment: Sysctl option vm.swappiness might be changed, we failed to check config file at /etc/sysctl.d/99-salt.conf. The file is either unreadable, or missing.
Started: 21:30:07.651402
Duration: 30.574 ms
Changes:
Summary for zabbix
------------
Succeeded: 7 (unchanged=6, changed=2)
Failed: 0
------------
Total states run: 7
Total run time: 226.668 ms
k8s-node1:
----------
ID: /etc/resolv.conf
Function: file.managed
Result: None
Comment: The file /etc/resolv.conf is set to be changed
Started: 21:30:07.717759
Duration: 68.049 ms
Changes:
----------
diff:
---
+++
@@ -0,0 +1 @@
+nameserver 114.114.114.114
----------
ID: /etc/profile
Function: file.append
Result: None
Comment: File /etc/profile is set to be updated
Started: 21:30:07.785995
Duration: 2.284 ms
Changes:
----------
diff:
---
+++
@@ -76,3 +76,4 @@
unset -f pathmunge
JAVA_HOME=/usr/local/jdk
export PATH=$PATH:/usr/local/jdk/bin
+export HISTTIMEFORMAT="%F %T `whoami`"
----------
ID: /etc/bashrc
Function: file.append
Result: None
Comment: File /etc/bashrc is set to be updated
Started: 21:30:07.788420
Duration: 3.71 ms
Changes:
----------
diff:
---
+++
@@ -90,3 +90,4 @@
unset -f pathmunge
fi
# vim:ts=4:sw=4
+export PROMPT_COMMAND='{ msg=$(history 1 | { read x y; echo $y; });logger "[euid=$(whoami)]":$(who am i):[`pwd`]"$msg"; }'
----------
ID: net.ipv4.ip_local_port_range
Function: sysctl.present
Result: None
Comment: Sysctl option net.ipv4.ip_local_port_range might be changed, we failed to check config file at /etc/sysctl.d/99-salt.conf. The file is either unreadable, or missing.
Started: 21:30:07.793759
Duration: 206.597 ms
Changes:
----------
ID: fs.file-max
Function: sysctl.present
Result: None
Comment: Sysctl option fs.file-max might be changed, we failed to check config file at /etc/sysctl.d/99-salt.conf. The file is either unreadable, or missing.
Started: 21:30:08.000637
Duration: 39.194 ms
Changes:
----------
ID: net.ipv4.ip_forward
Function: sysctl.present
Result: None
Comment: Sysctl option net.ipv4.ip_forward might be changed, we failed to check config file at /etc/sysctl.d/99-salt.conf. The file is either unreadable, or missing.
Started: 21:30:08.040010
Duration: 22.761 ms
Changes:
----------
ID: vm.swappiness
Function: sysctl.present
Result: None
Comment: Sysctl option vm.swappiness might be changed, we failed to check config file at /etc/sysctl.d/99-salt.conf. The file is either unreadable, or missing.
Started: 21:30:08.062950
Duration: 21.728 ms
Changes:
Summary for k8s-node1
------------
Succeeded: 7 (unchanged=7, changed=3)
Failed: 0
------------
Total states run: 7
Total run time: 364.323 ms
[root@zabbix init]#
[root@zabbix init]#
[root@zabbix init]#
[root@zabbix init]#
[root@zabbix init]#
[root@zabbix init]#
[root@zabbix init]#
[root@zabbix init]#
[root@zabbix init]#
[root@zabbix init]#
[root@zabbix init]#
[root@zabbix init]# salt '*' state.highstate
zabbix:
----------
ID: /etc/resolv.conf
Function: file.managed
Result: True
Comment: File /etc/resolv.conf is in the correct state
Started: 21:30:33.795289
Duration: 143.126 ms
Changes:
----------
ID: /etc/profile
Function: file.append
Result: True
Comment: Appended 1 lines
Started: 21:30:33.938875
Duration: 4.35 ms
Changes:
----------
diff:
---
+++
@@ -74,3 +74,4 @@
unset i
unset -f pathmunge
+export HISTTIMEFORMAT="%F %T `whoami`"
----------
ID: /etc/bashrc
Function: file.append
Result: True
Comment: Appended 1 lines
Started: 21:30:33.943370
Duration: 3.352 ms
Changes:
----------
diff:
---
+++
@@ -90,3 +90,4 @@
unset -f pathmunge
fi
# vim:ts=4:sw=4
+export PROMPT_COMMAND='{ msg=$(history 1 | { read x y; echo $y; });logger "[euid=$(whoami)]":$(who am i):[`pwd`]"$msg"; }'
----------
ID: net.ipv4.ip_local_port_range
Function: sysctl.present
Result: True
Comment: Updated sysctl value net.ipv4.ip_local_port_range = 10000 65000
Started: 21:30:33.951414
Duration: 44.024 ms
Changes:
----------
net.ipv4.ip_local_port_range:
10000 65000
----------
ID: fs.file-max
Function: sysctl.present
Result: True
Comment: Updated sysctl value fs.file-max = 1000000
Started: 21:30:33.995769
Duration: 9.811 ms
Changes:
----------
fs.file-max:
1000000
----------
ID: net.ipv4.ip_forward
Function: sysctl.present
Result: True
Comment: Updated sysctl value net.ipv4.ip_forward = 1
Started: 21:30:34.006046
Duration: 16.893 ms
Changes:
----------
net.ipv4.ip_forward:
1
----------
ID: vm.swappiness
Function: sysctl.present
Result: True
Comment: Updated sysctl value vm.swappiness = 0
Started: 21:30:34.023300
Duration: 10.572 ms
Changes:
----------
vm.swappiness:
0
Summary for zabbix
------------
Succeeded: 7 (changed=6)
Failed: 0
------------
Total states run: 7
Total run time: 232.128 ms
k8s-node1:
----------
ID: /etc/resolv.conf
Function: file.managed
Result: True
Comment: File /etc/resolv.conf updated
Started: 21:30:34.044920
Duration: 246.393 ms
Changes:
----------
diff:
---
+++
@@ -0,0 +1 @@
+nameserver 114.114.114.114
----------
ID: /etc/profile
Function: file.append
Result: True
Comment: Appended 1 lines
Started: 21:30:34.291544
Duration: 2.689 ms
Changes:
----------
diff:
---
+++
@@ -76,3 +76,4 @@
unset -f pathmunge
JAVA_HOME=/usr/local/jdk
export PATH=$PATH:/usr/local/jdk/bin
+export HISTTIMEFORMAT="%F %T `whoami`"
----------
ID: /etc/bashrc
Function: file.append
Result: True
Comment: Appended 1 lines
Started: 21:30:34.294458
Duration: 3.115 ms
Changes:
----------
diff:
---
+++
@@ -90,3 +90,4 @@
unset -f pathmunge
fi
# vim:ts=4:sw=4
+export PROMPT_COMMAND='{ msg=$(history 1 | { read x y; echo $y; });logger "[euid=$(whoami)]":$(who am i):[`pwd`]"$msg"; }'
----------
ID: net.ipv4.ip_local_port_range
Function: sysctl.present
Result: True
Comment: Updated sysctl value net.ipv4.ip_local_port_range = 10000 65000
Started: 21:30:34.299328
Duration: 23.361 ms
Changes:
----------
net.ipv4.ip_local_port_range:
10000 65000
----------
ID: fs.file-max
Function: sysctl.present
Result: True
Comment: Updated sysctl value fs.file-max = 1000000
Started: 21:30:34.322949
Duration: 21.344 ms
Changes:
----------
fs.file-max:
1000000
----------
ID: net.ipv4.ip_forward
Function: sysctl.present
Result: True
Comment: Updated sysctl value net.ipv4.ip_forward = 1
Started: 21:30:34.344490
Duration: 8.887 ms
Changes:
----------
net.ipv4.ip_forward:
1
----------
ID: vm.swappiness
Function: sysctl.present
Result: True
Comment: Updated sysctl value vm.swappiness = 0
Started: 21:30:34.353661
Duration: 8.257 ms
Changes:
----------
vm.swappiness:
0
Summary for k8s-node1
------------
Succeeded: 7 (changed=7)
Failed: 0
------------
Total states run: 7
Total run time: 314.046 ms
[root@zabbix init]#
4、验证结果