Ansible 和 crontabs [英] Ansible and crontabs

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

问题描述

我是 ansible 的新手.我一直在写一些剧本,嗯,它很棒.然而,我坚持一个非常简单的管理任务,每个管理员都必须学习如何管理.场景很常见,很多服务器: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 结构来管理不同服务器的 crontabs?

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?

谢谢!!!

推荐答案

我知道我不是专家,但如果一些 ansible 新手和我有同样的问题,也许这会有所帮助.

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等.我在说什么?不是一个,而是同时服务器,梦想.但是哦哦现在你必须在 lemp server 1 和 lemp server 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 变量.当你是 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|_任务|_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天全站免登陆