首页 > 代码库 > Saltstack之系统初始化

Saltstack之系统初始化

我使用了我自己制作的saltstack-minion的docker镜像,进行saltstack自动实现系统初始化。

PS:处理过程中发现docker的ntpd 是无法更新时间的。需要映射宿主的时间,使用docker -v参数。

docker获取方法:

docker pull docker.io/516249940/saltsatack-minion

系统初始化过程:每一个sls文件测试一遍 >> 然后写入到初始化目录的sls >> 最后加入到top.sls

#vim /etc/salt/master   
file_roots:
  base:
    - /srv/salt/base
  prod:
    - /srv/salt/prod
#分别创建yum.sls
#vim /srv/salt/base/init/yum.sls
[root@salt-master init]# cat yum.sls
yum-list-init:
  pkg.installed:
    - names:
      - gcc
      - gcc-c++
      - man
      - vim-enhanced
      - wget
      - telnet
      - lsof
      - sysstat
      - openssh-clients
      - lrzsz
      - tree
      - hdparm
[root@salt-master init]# cat dns.sls 
/etc/resolv.conf:

  file.managed:

    - source: salt://init/config/resolv.conf

    - user: root

    - group: root

    - mode: 644
    - backup: ‘*‘
[root@salt-master init]# cat cron.sls 
ntpdate-init:
  pkg.installed:
    - name: ntpdate

crontab-init:
  pkg.installed:
    - name: crontabs
  cmd.run:
    - name: /etc/init.d/crond restart
   

set-crontab:
  cron.present:
    - name: /usr/bin/ntpdate times.aliyun.com >> /dev/null 2>&1
    - user: root
    - minute: ‘*5‘
    - require:
      - pkg: ntpdate-init
      - pkg: crontab-init
#再创建一个/srv/salt/base/init/env_init.sls
[root@salt-master init]# cat env_init.sls 
include:
  - init.dns
  - init.cron
  - init.yum
最后创建top.sls
[root@salt-master base]# cat top.sls 
base:
  ‘*minion*‘:                  #匹配id含有minion的主机
    - init.env_init
[root@salt-master base]# salt-key 
Accepted Keys:
docker-minion01
docker-minion02
hddcluster1
hddcluster2
hddcluster3
hddcluster4
salt-master
salt-minion01
Denied Keys:
Unaccepted Keys:
Rejected Keys:
分别执行下面命令测试上面的编写的文件
salt ‘docker-minion0*‘ state.sls init.yum test=true
salt ‘docker-minion0*‘ state.sls init.dns test=true
salt ‘docker-minion0*‘ state.sls init.cron test=true
全部通过测试
最后测试top.sls
[root@salt-master base]# salt ‘docker-minion0*‘   state.highstate test=True
docker-minion02:
----------
          ID: /etc/resolv.conf
    Function: file.managed
      Result: True
     Comment: The file /etc/resolv.conf is in the correct state
     Started: 11:00:15.272191
    Duration: 16.812 ms
     Changes:   
----------
          ID: ntpdate-init
    Function: pkg.installed
        Name: ntpdate
      Result: True
     Comment: Package ntpdate is already installed
     Started: 11:00:16.425816
    Duration: 504.677 ms
     Changes:   
----------
          ID: crontab-init
    Function: pkg.installed
        Name: crontabs
      Result: True
     Comment: Package crontabs is already installed
     Started: 11:00:16.930610
    Duration: 0.357 ms
     Changes:   
----------
          ID: crontab-init
    Function: cmd.run
        Name: /etc/init.d/crond restart
      Result: None
     Comment: Command "/etc/init.d/crond restart" would have been executed
     Started: 11:00:16.931733
    Duration: 0.254 ms
     Changes:   
----------
          ID: set-crontab
    Function: cron.present
        Name: /usr/bin/ntpdate times.aliyun.com >> /dev/null 2>&1
      Result: True
     Comment: Cron /usr/bin/ntpdate times.aliyun.com >> /dev/null 2>&1 already present
     Started: 11:00:16.932840
    Duration: 253.346 ms
     Changes:   
----------
          ID: yum-list-init
    Function: pkg.installed
        Name: gcc
      Result: True
     Comment: Package gcc is already installed
     Started: 11:00:17.186312
    Duration: 0.422 ms
     Changes:   
----------
          ID: yum-list-init
    Function: pkg.installed
        Name: lrzsz
      Result: True
     Comment: Package lrzsz is already installed
     Started: 11:00:17.186803
    Duration: 0.285 ms
     Changes:   
----------
          ID: yum-list-init
    Function: pkg.installed
        Name: lsof
      Result: True
     Comment: Package lsof is already installed
     Started: 11:00:17.187151
    Duration: 0.287 ms
     Changes:   
----------
          ID: yum-list-init
    Function: pkg.installed
        Name: tree
      Result: True
     Comment: Package tree is already installed
     Started: 11:00:17.187501
    Duration: 0.277 ms
     Changes:   
----------
          ID: yum-list-init
    Function: pkg.installed
        Name: openssh-clients
      Result: True
     Comment: Package openssh-clients is already installed
     Started: 11:00:17.187840
    Duration: 0.275 ms
     Changes:   
----------
          ID: yum-list-init
    Function: pkg.installed
        Name: telnet
      Result: True
     Comment: Package telnet is already installed
     Started: 11:00:17.188176
    Duration: 0.288 ms
     Changes:   
----------
          ID: yum-list-init
    Function: pkg.installed
        Name: hdparm
      Result: True
     Comment: Package hdparm is already installed
     Started: 11:00:17.188528
    Duration: 0.274 ms
     Changes:   
----------
          ID: yum-list-init
    Function: pkg.installed
        Name: sysstat
      Result: True
     Comment: Package sysstat is already installed
     Started: 11:00:17.188865
    Duration: 0.272 ms
     Changes:   
----------
          ID: yum-list-init
    Function: pkg.installed
        Name: wget
      Result: True
     Comment: Package wget is already installed
     Started: 11:00:17.189199
    Duration: 0.28 ms
     Changes:   
----------
          ID: yum-list-init
    Function: pkg.installed
        Name: man
      Result: True
     Comment: Package man is already installed
     Started: 11:00:17.189542
    Duration: 0.272 ms
     Changes:   
----------
          ID: yum-list-init
    Function: pkg.installed
        Name: gcc-c++
      Result: True
     Comment: Package gcc-c++ is already installed
     Started: 11:00:17.189876
    Duration: 0.272 ms
     Changes:   
----------
          ID: yum-list-init
    Function: pkg.installed
        Name: vim-enhanced
      Result: True
     Comment: Package vim-enhanced is already installed
     Started: 11:00:17.190211
    Duration: 0.291 ms
     Changes:   

Summary for docker-minion02
-------------
Succeeded: 17 (unchanged=1)
Failed:     0
-------------
Total states run:     17
Total run time:  778.941 ms
docker-minion01:
----------
          ID: /etc/resolv.conf
    Function: file.managed
      Result: True
     Comment: The file /etc/resolv.conf is in the correct state
     Started: 11:00:15.249261
    Duration: 34.726 ms
     Changes:   
----------
          ID: ntpdate-init
    Function: pkg.installed
        Name: ntpdate
      Result: True
     Comment: Package ntpdate is already installed
     Started: 11:00:16.442680
    Duration: 501.913 ms
     Changes:   
----------
          ID: crontab-init
    Function: pkg.installed
        Name: crontabs
      Result: True
     Comment: Package crontabs is already installed
     Started: 11:00:16.944708
    Duration: 0.357 ms
     Changes:   
----------
          ID: crontab-init
    Function: cmd.run
        Name: /etc/init.d/crond restart
      Result: None
     Comment: Command "/etc/init.d/crond restart" would have been executed
     Started: 11:00:16.945815
    Duration: 0.255 ms
     Changes:   
----------
          ID: set-crontab
    Function: cron.present
        Name: /usr/bin/ntpdate times.aliyun.com >> /dev/null 2>&1
      Result: True
     Comment: Cron /usr/bin/ntpdate times.aliyun.com >> /dev/null 2>&1 already present
     Started: 11:00:16.946927
    Duration: 258.464 ms
     Changes:   
----------
          ID: yum-list-init
    Function: pkg.installed
        Name: gcc
      Result: True
     Comment: Package gcc is already installed
     Started: 11:00:17.205522
    Duration: 0.496 ms
     Changes:   
----------
          ID: yum-list-init
    Function: pkg.installed
        Name: lrzsz
      Result: True
     Comment: Package lrzsz is already installed
     Started: 11:00:17.206094
    Duration: 0.317 ms
     Changes:   
----------
          ID: yum-list-init
    Function: pkg.installed
        Name: lsof
      Result: True
     Comment: Package lsof is already installed
     Started: 11:00:17.206480
    Duration: 0.283 ms
     Changes:   
----------
          ID: yum-list-init
    Function: pkg.installed
        Name: tree
      Result: True
     Comment: Package tree is already installed
     Started: 11:00:17.206848
    Duration: 0.281 ms
     Changes:   
----------
          ID: yum-list-init
    Function: pkg.installed
        Name: openssh-clients
      Result: True
     Comment: Package openssh-clients is already installed
     Started: 11:00:17.207192
    Duration: 0.313 ms
     Changes:   
----------
          ID: yum-list-init
    Function: pkg.installed
        Name: telnet
      Result: True
     Comment: Package telnet is already installed
     Started: 11:00:17.207570
    Duration: 0.273 ms
     Changes:   
----------
          ID: yum-list-init
    Function: pkg.installed
        Name: hdparm
      Result: True
     Comment: Package hdparm is already installed
     Started: 11:00:17.207906
    Duration: 0.275 ms
     Changes:   
----------
          ID: yum-list-init
    Function: pkg.installed
        Name: sysstat
      Result: True
     Comment: Package sysstat is already installed
     Started: 11:00:17.208242
    Duration: 0.279 ms
     Changes:   
----------
          ID: yum-list-init
    Function: pkg.installed
        Name: wget
      Result: True
     Comment: Package wget is already installed
     Started: 11:00:17.208584
    Duration: 0.272 ms
     Changes:   
----------
          ID: yum-list-init
    Function: pkg.installed
        Name: man
      Result: True
     Comment: Package man is already installed
     Started: 11:00:17.208917
    Duration: 0.271 ms
     Changes:   
----------
          ID: yum-list-init
    Function: pkg.installed
        Name: gcc-c++
      Result: True
     Comment: Package gcc-c++ is already installed
     Started: 11:00:17.209248
    Duration: 0.279 ms
     Changes:   
----------
          ID: yum-list-init
    Function: pkg.installed
        Name: vim-enhanced
      Result: True
     Comment: Package vim-enhanced is already installed
     Started: 11:00:17.209589
    Duration: 0.278 ms
     Changes:   

Summary for docker-minion01
-------------
Succeeded: 17 (unchanged=1)
Failed:     0
-------------
Total states run:     17
Total run time:  799.332 ms
[root@salt-master base]#
最后一步初始化系统:
salt ‘docker-minion0*‘   state.highstate


写着写着发觉自己的写作水平真差劲········

本文出自 “禅剑一如” 博客,请务必保留此出处http://yanconggod.blog.51cto.com/1351649/1883139

Saltstack之系统初始化