42ew222 发表于 2016-3-15 09:23:00

salt结合crontab自动同步配置文件

saltstack自动同步配置文件,其实我是结合crontab来搞得,就是把minion端的机器加入到已个组中,然后针对这个组进行job执行state.sls来做的。。


注:其实saltstack有自己schedule计划任务。只不过看的迷迷糊糊的,而且我赶脚其实就job,再说了,要是使用自动同步配置文件,前提是你都已经晓得了,并且都已经在测试环境中测试过,负责你懂得。。。而且自动同步,你根本看不到执行的状态。。所以你懂得。其实这些都是然并卵东东。。
一:对minion进行分组,为了不和master主配置文件一起搞混淆了,单独建立一个配置文件,在master的配置文件中,打开相应的注释如下

root@iZ23f6c5z8tZ:~# grep "^default" /etc/salt/master
default_include: master.d/*.conf
配置文件中的web是针对minion设置的一个组。组名为web
root@iZ23f6c5z8tZ:/etc/salt/master.d# cat nodegroup.conf
nodegroups:
web: 'S@121.40.139.41'

使用终端命令进行测试
root@iZ23f6c5z8tZ:/etc/salt/master.d# salt -N web test.ping
iZ23f6c5z8tZ:
    True
二:使用salt安装nginx,在默认base环境下创建apt目录,因为我使用apt安装nginx的,方便测试....
root@iZ23f6c5z8tZ:/srv/salt/base# tree apt/
apt/
├── files
│   └── nginx.conf
└── install.sls
1 directory, 2 files

安装nginx的sls文件的内容如下:

root@iZ23f6c5z8tZ:/srv/salt/base/apt# cat install.sls
apt-nginx-install:
cmd.run:
    - name: apt-get install nginx -y
    - unless: test -d /etc/nginx
apt-nginx-config:
file.managed:
    - name: /etc/nginx/nginx.conf
    - source: salt://apt/files/nginx.conf
    - user: root
    - group: root
    - mode: 644
apt-nginx-service:
service.running:
    - name: nginx
    - reload: True
    - require:
      - cmd: apt-nginx-install
      - file: apt-nginx-config
    - watch:
      - file: apt-nginx-config
三:安装nginx
root@iZ23f6c5z8tZ:/srv/salt/base/apt# salt -N web state.sls apt.install
iZ23f6c5z8tZ:
----------
    State: - cmd
    Name:      apt-get install nginx -y
    Function:run
      Result:    True
      Comment:   unless execution succeeded
      Changes:   
----------
    State: - file
    Name:      /etc/nginx/nginx.conf
    Function:managed
      Result:    True
      Comment:   File /etc/nginx/nginx.conf is in the correct state
      Changes:   
----------
    State: - service
    Name:      nginx
    Function:running
      Result:    True
      Comment:   Started Service nginx
      Changes:   nginx: True


Summary
------------
Succeeded: 3
Failed:    0
------------
Total:   3
查看web组的minion端nginx服务状态
root@iZ23f6c5z8tZ:/srv/salt/base/apt/files# salt -N web cmd.run 'service nginx status'
iZ23f6c5z8tZ:
   * nginx is running
四:设置计划任务,以及修改nginx配置文件,查看是否更改nginx配置文件以及加载服务,方便测试我设置每隔1分钟执行,
#web group test salt
*/1 * * * * salt -N web state.sls apt.install

修改nginx.conf配置以及停止nginx服务,查看是否更改配置以及重载服务
停止web组的minion端nginx服务
root@iZ23f6c5z8tZ:/srv/salt/base/apt/files# salt -N web service.stop nginx
iZ23f6c5z8tZ:
    True

root@iZ23f6c5z8tZ:/srv/salt/base/apt/files# salt -N web cmd.run 'service nginx status'
iZ23f6c5z8tZ:
   * nginx is not running
在nginx.conf文件中添加内容
root@iZ23f6c5z8tZ:/srv/salt/base/apt/files# tail -n 1 nginx.conf
#This is a test salt

隔1分钟后查看web组的minion端nginx配置文件,以及查看服务状态
root@iZ23f6c5z8tZ:/srv/salt/base/apt/files# salt -N web cmd.run 'tail -n 1 /etc/nginx/nginx.conf'
iZ23f6c5z8tZ:
    #This is a test salt

root@iZ23f6c5z8tZ:/srv/salt/base/apt/files# salt -N web cmd.run 'service nginx status'
iZ23f6c5z8tZ:
   * nginx is running



页: [1]
查看完整版本: salt结合crontab自动同步配置文件