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

[经验分享] salt结合crontab自动同步配置文件

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-3-15 09:23:00 | 显示全部楼层 |阅读模式
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、欢迎大家加入本站运维交流群:群②: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-190984-1-1.html 上篇帖子: salt的使用 下篇帖子: saltstack初级篇 配置文件
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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