Ansible和crontabs [英] Ansible and crontabs

查看:146
本文介绍了Ansible和crontabs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新手,很生气.我一直在写一些剧本,而且棒极了.但是,我坚持一个非常简单的管理任务,每个管理员都必须学习如何进行管理.场景很常见,服务器很多:ubuntu,centos,不同客户的服务器等

I am very newbie with ansible. I have been writing some playbooks and well, its awesome. However I am stuck with a very simple admin task that every admin has to learn how to manage. Scenario is very common, lot of servers: ubuntu, centos, servers of differents customers, etc

我的问题是我必须如何组织ansible结构来管理不同服务器的crontab?

My question is how I must organize my ansible structure to manage crontabs of the different servers?

我写了一些剧本,可以通过一些管理任务将它们应用到大多数服务器上:apt-get udpate,安装ntp服务器和同步,但是我找不到正确的方法来制作文件的ansible结构( main.yml,host_vars,文件,模板,角色等)来管理每个服务器的每个cron.

I have wrote playbooks which can be applied to the most of the servers with some admin tasks: apt-get udpate, install ntp servers and synchronize, but I don't find the right way to make a ansible structure of files (main.yml, host_vars, files,templates,roles,etc) to manage every single cron of every server.

您有什么例子或建议吗?

Do you have any example or any advice?

谢谢!

推荐答案

我知道我不是专家,但是如果某个新手与我有同样的问题,也许可以帮忙.

I know Im not an expert but If some ansible newbie has the same problem as me maybe this can help.

正如我几天前写的,您正在学习Ansible,您有很多服务器,甚至可以在几分钟内在一个非常新的ubuntu/centos框中安装nginx,php-fpm,mysql等.我在说什么梦servers以求的不是服务器,而是服务器.但是,哦,现在您必须在lemp服务器1和lemp服务器2中安装不同的cron任务.

As I wrote few days ago, you are learning ansible, you have a lot of servers, you even can install nginx, php-fpm, mysql, etc in a very new ubuntu/centos box in a few minutes. What I am saying? Not one but servers at the same time, the dream. But oh oh now you have to install different cron tasks in lemp server 1 and lemp server 2.

然后,您必须使用ansible变量.当您是新世界时,变量可能会有些棘手.

Then, you have to play with ansible variables. When you are newbie in the ansible world the variables can be a little tricky.

我并不是说这是更好的方法,但是至少,您不必为每个服务器维护单独的cron文件.

I am not saying this is the better approach but, at least, you don't have to maintain individual cron files for every single server.

首先,我检查Lemp服务器1的ansible_hostname

First, I check the ansible_hostname of lemp server 1

$ ansible lempserver1 -m setup|grep host
"ansible_hostname": "ns227962",

如您所见,ansible返回的主机名是ns227962.然后我有一个类似ansible的结构:

as you see, the hostname returned by ansible is ns227962. Then I have a ansible structure like:

-main.yml | _角色 | _cron | _tasks | _main.yml

-- main.yml |_ roles |_cron |_tasks |_main.yml

我主要是cron角色

---
- name: add line to crontab's root - backup
  cron: name="backup openvz vps" hour="5" minute="1" weekday="7"  job="/root/scripts/openvz_backup_vps > /dev/null"
  when: "ansible_hostname == 'ns227962'"

请注意,这是when语句和ansible_hostname可以解决的.

Notice that is the when statement and ansible_hostname will do the trick.

然后我应用main.yml就这样

Then I apply the main.yml and that's all

TASK: [cron | add line to crontab's root - backup] ***********
skipping: [lempserver2]
changed: [lempserver1]

成功了! :)

在其他情况下,如果您具有相同的文件配置,但是使用模板时有些部分略有不同,将会很有帮助.

In other situations when you have the same file config but there are parts that are slightly different using templates will be helpful.

现在对我来说还可以,现在我可以只用一个角色集中管理crons服务器.

Now for me its ok, now I can centralize the manage of the crons servers only with one role.

谢谢

这篇关于Ansible和crontabs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆