设为首页 收藏本站
查看: 1047|回复: 0

[经验分享] saltstack的状态文件

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2017-2-20 09:38:15 | 显示全部楼层 |阅读模式
saltstack状态文件设定:
编辑/etc/salt/master,修改其中关于“设置文件的目录”的设置:

wKiom1ioU9yjVAQbAAASdBuXfz0630.png
说明:注意语法格式,顶格/冒号/两个空格

1
2
3
4
5
6
7
8
9
10
state_top: top.sls
# The state system uses a "top" file to tell the minions what environment to
# use and what modules to use. The state_top file is defined relative to the
# root of the base environment as defined in "File Server settings" below.
#state_top: top.sls
[iyunv@master ~]# mkdir -p /etc/salt/states
[iyunv@master ~]# vim /etc/salt/states/top.sls
[iyunv@master ~]# sed -i '329s/#//' /etc/salt/master
state_top: top.sls
说明:将329行的注释取消



进入base环境下,并配置下top.sls

1
2
3
4
5
6
7
8
[iyunv@master ~]# cd /etc/salt/states/
[iyunv@master states]# mkdir -p init
[iyunv@master states]# mkdir -p prod
[iyunv@master states]# vim top.sls
[iyunv@master states]# cat top.sls
base:
  'node01.saltstack.com':
    -init.pkg



说明:base是指定一个名称,init为文件夹的名称,pkg为pkg.sls

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[iyunv@master states]# ll
总用量 12
drwxr-xr-x 2 root root 4096 2月  15 14:16 init
drwxr-xr-x 2 root root 4096 2月  15 14:16 prod
-rw-r--r-- 1 root root   46 2月  15 14:17 top.sls
[iyunv@master states]# cd init/
[iyunv@master init]# vim pkg.sls
[iyunv@master init]# cat pkg.sls
pkg.init:
  pkg.installed:
    - names:
      - lrzsz
      - mtr
      - nmap



案例1:使用salt初始化系统模块:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
[iyunv@master init]# salt '*' state.sls init.pkg
node01.saltstack.com:
----------
          ID: pkg.init
    Function: pkg.installed
        Name: mtr
      Result: True
     Comment: Package mtr is already installed.
     Started: 14:56:02.574416
    Duration: 11389.014 ms
     Changes:   
----------
          ID: pkg.init
    Function: pkg.installed
        Name: nmap
      Result: True
     Comment: Package nmap is already installed.
     Started: 14:56:13.963968
    Duration: 3.619 ms
     Changes:   
----------
          ID: pkg.init
    Function: pkg.installed
        Name: lrzsz
      Result: True
     Comment: Package lrzsz is already installed.
     Started: 14:56:13.967979
    Duration: 1.042 ms
     Changes:   
Summary
------------
Succeeded: 3
Failed:    0
------------
Total states run:     3



案例2:saltstack修改内核参数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
[iyunv@master ~]# cd /etc/salt/states/init/
[iyunv@master init]# tree
.
└── pkg.sls
0 directories, 1 file
[iyunv@master init]# mkdir -p files
[iyunv@master init]# cd files/
[iyunv@master init]# vim limit.sls
limit-conf-config:
  file.managed:
    - name: /etc/security/limits.conf
    - source: salt://init/files/limits.conf
    - user: root
    - group: root
    - mode: 644
[iyunv@master files]# cd /etc/security/
[iyunv@master security]# ls
access.conf       console.perms    limits.d        opasswd           time.conf
chroot.conf       console.perms.d  namespace.conf  pam_env.conf
console.apps      group.conf       namespace.d     pam_winbind.conf
console.handlers  limits.conf      namespace.init  sepermit.conf
[iyunv@master security]# cp limits.conf /etc/salt/states/init/files/
[iyunv@master files]# vim limits.conf
*               soft    core            0
*               hard    rss             10000
[iyunv@master states]# pwd
/etc/salt/states
注意:要将新的模块添加到top.sls中,不然会有其它报错
[iyunv@master states]# cat top.sls
base:
  '*':
    - init.pkg
    - init.limit
[iyunv@master init]# salt '*' state.highstate
node01.saltstack.com:
----------
          ID: pkg.init
    Function: pkg.installed
        Name: mtr
      Result: True
     Comment: Package mtr is already installed.
     Started: 17:42:55.479576
    Duration: 7120.831 ms
     Changes:   
----------
          ID: pkg.init
    Function: pkg.installed
        Name: nmap
      Result: True
     Comment: Package nmap is already installed.
     Started: 17:43:02.601307
    Duration: 2.278 ms
     Changes:   
----------
          ID: pkg.init
    Function: pkg.installed
        Name: lrzsz
      Result: True
     Comment: Package lrzsz is already installed.
     Started: 17:43:02.603841
    Duration: 0.952 ms
     Changes:   
----------
          ID: limit-conf-config
    Function: file.managed
        Name: /etc/security/limits.conf
      Result: True
     Comment: File /etc/security/limits.conf updated
     Started: 17:43:02.612678
    Duration: 19.256 ms
     Changes:   
              ----------
              diff:
                  ---  
                  +++  
                  @@ -39,8 +39,8 @@
                   #<domain>      <type>  <item>         <value>
                   #
                    
                  -#*               soft    core            0
                  -#*               hard    rss             10000
                  +*               soft    core            0
                  +*               hard    rss             10000
                   #@student        hard    nproc           20
                   #@faculty        soft    nproc           20
                   #@faculty        hard    nproc           50
Summary
------------
Succeeded: 4 (changed=1)
Failed:    0
------------
Total states run:     4
客户端测试:
[iyunv@node01 security]# egrep -v '#|^$' limits.conf
*               soft    core            0
*               hard    rss             10000




案例3:同步某个计划任务
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
最近发现很多服务器上没有配置ntp服务器指向,简单写个计划任务,然后通过状态文件下发
思路:
a)准备好需要下发的文件
b)编辑sls文件
c)修改top.sls,添加信息进去
[iyunv@master ~]# cat /var/spool/cron/root
*/5 * * * * /usr/sbin/ntpdate -u 202.120.2.101>/dev/null 2>&1
[iyunv@master ~]# cd /etc/salt/states/
[iyunv@master states]# ls
init  prod  top.sls
[iyunv@master states]# cd init/
[iyunv@master init]# ls
files  limit.sls  pkg.sls
[iyunv@master init]# cp limit.sls ntp-crontab.sls
[iyunv@master init]# ls
files  limit.sls  ntp-crontab.sls  pkg.sls
[iyunv@master init]# cd files/
[iyunv@master files]# cp /var/spool/cron/root .
[iyunv@master files]# pwd
/etc/salt/states/init/files
[iyunv@master files]# cat root
*/5 * * * * /usr/sbin/ntpdate -u 202.120.2.101>/dev/null 2>&1
[iyunv@master files]# mv root ntp-crontab.conf
[iyunv@master files]# cat ntp-crontab.conf
*/5 * * * * /usr/sbin/ntpdate -u 202.120.2.101>/dev/null 2>&1
[iyunv@master files]# cd ..
[iyunv@master init]# ls
files  limit.sls  ntp-crontab.sls  pkg.sls
[iyunv@master ~]# cat /etc/salt/states/init/ntp-crontab.sls
ntp-crontab-config:
  file.managed:
    - name: /var/spool/cron/root
    - source: salt://init/files/ntp-crontab.conf
    - user: root
    - group: root
    - mode: 644
计划任务更新执行结果:
[iyunv@master init]# salt '*' state.highstate
node01.saltstack.com:
----------
          ID: pkg.init
    Function: pkg.installed
        Name: mtr
      Result: True
     Comment: Package mtr is already installed.
     Started: 21:09:06.608808
    Duration: 4265.514 ms
     Changes:   
----------
          ID: pkg.init
    Function: pkg.installed
        Name: nmap
      Result: True
     Comment: Package nmap is already installed.
     Started: 21:09:10.874647
    Duration: 0.685 ms
     Changes:   
----------
          ID: pkg.init
    Function: pkg.installed
        Name: lrzsz
      Result: True
     Comment: Package lrzsz is already installed.
     Started: 21:09:10.875446
    Duration: 0.583 ms
     Changes:   
----------
          ID: limit-conf-config
    Function: file.managed
        Name: /etc/security/limits.conf
      Result: True
     Comment: File /etc/security/limits.conf is in the correct state
     Started: 21:09:10.879350
    Duration: 4.1 ms
     Changes:   
----------
          ID: ntp-crontab-config
    Function: file.managed
        Name: /var/spool/cron/root
      Result: True
     Comment: File /var/spool/cron/root updated
     Started: 21:09:10.883639
    Duration: 9.342 ms
     Changes:   
              ----------
              diff:
                  New file
              mode:
                  0644
Summary
------------
Succeeded: 5 (changed=1)
Failed:    0
------------
Total states run:     5
node02.saltstack.com:
----------
          ID: pkg.init
    Function: pkg.installed
        Name: mtr
      Result: True
     Comment: Package mtr is already installed.
     Started: 21:09:07.831431
    Duration: 4292.2 ms
     Changes:   
----------
          ID: pkg.init
    Function: pkg.installed
        Name: nmap
      Result: True
     Comment: Package nmap is already installed.
     Started: 21:09:12.123977
    Duration: 0.714 ms
     Changes:   
----------
          ID: pkg.init
    Function: pkg.installed
        Name: lrzsz
      Result: True
     Comment: Package lrzsz is already installed.
     Started: 21:09:12.124798
    Duration: 0.426 ms
     Changes:   
----------
          ID: limit-conf-config
    Function: file.managed
        Name: /etc/security/limits.conf
      Result: True
     Comment: File /etc/security/limits.conf is in the correct state
     Started: 21:09:12.128235
    Duration: 5.165 ms
     Changes:   
----------
          ID: ntp-crontab-config
    Function: file.managed
        Name: /var/spool/cron/root
      Result: True
     Comment: File /var/spool/cron/root updated
     Started: 21:09:12.133621
    Duration: 8.761 ms
     Changes:   
              ----------
              diff:
                  New file
              mode:
                  0644
Summary
------------
Succeeded: 5 (changed=1)
Failed:    0
------------
Total states run:     5
检查结果:
[iyunv@node01 spool]# cd /var/spool/cron/
[iyunv@node01 cron]# ls
root
[iyunv@node01 cron]# cat root
*/5 * * * * /usr/sbin/ntpdate -u 202.120.2.101>/dev/null 2>&1
[iyunv@node02 ~]# cat /var/spool/cron/root
*/5 * * * * /usr/sbin/ntpdate -u 202.120.2.101>/dev/null 2>&1
通过对比会发现,与master的下发文件一致



案例4:同步内网的hosts文件(适用于内网没有建立独立DNS的情况)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
[iyunv@master ~]# cd /etc/salt/states/init/
[iyunv@master init]# ll
总用量 16
drwxr-xr-x 2 root root 4096 2月  18 21:01 files
-rw-r--r-- 1 root root  168 2月  18 17:42 limit.sls
-rw-r--r-- 1 root root  169 2月  18 21:08 ntp-crontab.sls
-rw-r--r-- 1 root root   79 2月  15 14:55 pkg.sls
[iyunv@master init]# cd files/
[iyunv@master files]# vim hosts.conf
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
10.10.10.140    master  master.saltstack.com
10.10.10.141    node01  node01.saltstack.com
10.10.10.142    node02  node02.saltstack.com
10.10.10.143    node03  node03.saltstack.com
[iyunv@master init]# cat hosts.sls
hosts-config:
  file.managed:
    - name: /etc/hosts
    - source: salt://init/files/hosts.conf
    - user: root
    - group: root
    - mode: 644
说明:下发文件到/etc/hosts,源文件
[iyunv@master states]# cat /etc/salt/states/top.sls
base:
  '*':
    - init.pkg
    - init.limit
    - init.ntp-crontab
    - init.hosts
[iyunv@master states]# salt '*' state.highstate
----------前面的部分我直接省略了--------------
----------
          ID: hosts-config
    Function: file.managed
        Name: /etc/hosts
      Result: True
     Comment: File /etc/hosts updated
     Started: 21:31:43.644962
    Duration: 13.119 ms
     Changes:   
              ----------
              diff:
                  ---  
                  +++  
                  @@ -3,3 +3,4 @@
                   10.10.10.140 mastermaster.saltstack.com
                   10.10.10.141 node01node01.saltstack.com
                   10.10.10.142 node02node02.saltstack.com
                  +10.10.10.143  node03node03.saltstack.com
Summary
------------
Succeeded: 6 (changed=1)
Failed:    0
------------
Total states run:     6
客户端进行测试:
[iyunv@node01 cron]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
10.10.10.140 master master.saltstack.com
10.10.10.141 node01 node01.saltstack.com
10.10.10.142 node02 node02.saltstack.com
10.10.10.143 node03 node03.saltstack.com
[iyunv@node02 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
10.10.10.140 master master.saltstack.com
10.10.10.141 node01 node01.saltstack.com
10.10.10.142 node02 node02.saltstack.com
10.10.10.143 node03 node03.saltstack.com
如果此时我在master端修改hosts.conf文件
[iyunv@master init]# pwd
/etc/salt/states/init
[iyunv@master init]# cat files/hosts.conf
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
10.10.10.140 master master.saltstack.com
10.10.10.141 node01 node01.saltstack.com
10.10.10.142 node02 node02.saltstack.com
10.10.10.143 node03 node03.saltstack.com
10.10.10.144 openstack01 openstack01.saltstack.com
10.10.10.145 openstack02 openstack02.saltstack.com
[iyunv@master init]# salt '*' state.highstate
----------前面的部分我直接省略了--------------
----------
          ID: hosts-config
    Function: file.managed
        Name: /etc/hosts
      Result: True
     Comment: File /etc/hosts updated
     Started: 21:37:50.679328
    Duration: 78.269 ms
     Changes:   
              ----------
              diff:
                  ---  
                  +++  
                  @@ -4,3 +4,5 @@
                   10.10.10.141node01node01.saltstack.com
                   10.10.10.142node02node02.saltstack.com
                   10.10.10.143node03node03.saltstack.com
                  +10.10.10.144openstack01openstack01.saltstack.com
                  +10.10.10.145openstack02openstack02.saltstack.com
Summary
------------
Succeeded: 6 (changed=1)
Failed:    0
------------
Total states run:     6
客户端进行测试:
[iyunv@node01 cron]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
10.10.10.140 master master.saltstack.com
10.10.10.141 node01 node01.saltstack.com
10.10.10.142 node02 node02.saltstack.com
10.10.10.143 node03 node03.saltstack.com
10.10.10.144 openstack01 openstack01.saltstack.com
10.10.10.145 openstack02 openstack02.saltstack.com
[iyunv@node02 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
10.10.10.140 master master.saltstack.com
10.10.10.141 node01 node01.saltstack.com
10.10.10.142 node02 node02.saltstack.com
10.10.10.143 node03 node03.saltstack.com



关于salt批量配置hosts文件:http://www.ttlsa.com/linux/salt-modules-hosts/

这里我只写一个添加hosts文件的例子,更多内容可以参考上面的链接(干货很多)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[iyunv@master ~]# salt '*' hosts.set_host 10.10.10.145 openstack02.saltstack.com
node02.saltstack.com:
    True
node01.saltstack.com:
    True
[iyunv@master ~]# salt '*' hosts.set_host 10.10.10.143 openstack03.saltstack.com
node02.saltstack.com:
    True
node01.saltstack.com:
    True
[iyunv@master ~]# salt-ssh '*' cmd.run 'tail -2 /etc/hosts'
node02:
    10.10.10.144 openstack01 openstack01.saltstack.com
    10.10.10.145 openstack02.saltstack.com
node01:
    10.10.10.144 openstack01 openstack01.saltstack.com
    10.10.10.145 openstack02.saltstack.com






运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-344568-1-1.html 上篇帖子: 自动化运维工具-saltstack 下篇帖子: saltstack 系列(一)centos7 安装saltstack
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表