salt不同服务器同步不同配置文件,salt结合crontab自动同步配置文件

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

注:其实saltstack有自己schedule计划任务。只不过看的迷迷糊糊的,而且我赶脚其实就job,再说了,要是使用自动同步配置文件,前提是你都已经晓得了,并且都已经在测试环境中测试过,负责你懂得。。。而且自动同步,你根本看不到执行的状态。。所以你懂得。其实这些都是然并卵东东。。

一:对minion进行分组,为了不和master主配置文件一起搞混淆了,单独建立一个配置文件,在master的配置文件中,打开相应的注释如下

[email protected]:~# grep "^default" /etc/salt/master

default_include: master.d/*.conf

配置文件中的web是针对minion设置的一个组。组名为web

[email protected]:/etc/salt/master.d# cat nodegroup.conf

nodegroups:

web: [email protected]'

使用终端命令进行测试

[email protected]:/etc/salt/master.d# salt -N web test.ping

iZ23f6c5z8tZ:

True

二:使用salt安装nginx,在默认base环境下创建apt目录,因为我使用apt安装nginx的,方便测试....

[email protected]:/srv/salt/base# tree apt/

apt/

├── files

│   └── nginx.conf

└── install.sls

1 directory, 2 files

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

[email protected]:/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

[email protected]:/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服务状态

[email protected]:/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服务

[email protected]:/srv/salt/base/apt/files# salt -N web service.stop nginx

iZ23f6c5z8tZ:

True

[email protected]:/srv/salt/base/apt/files# salt -N web cmd.run 'service nginx status'

iZ23f6c5z8tZ:

* nginx is not running

在nginx.conf文件中添加内容

[email protected]:/srv/salt/base/apt/files# tail -n 1 nginx.conf

#This is a test salt

隔1分钟后查看web组的minion端nginx配置文件,以及查看服务状态

[email protected]:/srv/salt/base/apt/files# salt -N web cmd.run 'tail -n 1 /etc/nginx/nginx.conf'

iZ23f6c5z8tZ:

#This is a test salt

[email protected]:/srv/salt/base/apt/files# salt -N web cmd.run 'service nginx status'

iZ23f6c5z8tZ:

* nginx is running

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值