将你的日历与 Ansible 集成，以避免与日程冲突
Ansible 日历

> 
> 通过将日历应用集成到 Ansible 中，确保你的自动化工作流计划不会与其他东西冲突。
> 
> 
> 


![](/data/attachment/album/202010/18/221110wk9kd9ewrde11zxx.jpg)


“随时”是执行自动化工作流的好时机吗？出于不同的原因，答案可能是否定的。


如果要避免同时进行更改，以最大限度地减少对关键业务流程的影响，并降低意外服务中断的风险，则在你的自动化运行的同时，其他任何人都不应该试图进行更改。


在某些情况下，可能存在一个正在进行的计划维护窗口。或者，可能有大型事件即将来临、一个关键的业务时间、或者假期（你或许不想在星期五晚上进行更改）。


![Street scene with a large calendar and people walking](/data/attachment/album/202010/18/221143fw24mm4e174z1wlp.jpg "Street scene with a large calendar and people walking")


无论出于什么原因，你都希望将此信息发送到你的自动化平台，以防止在特定时间段内执行周期性或临时任务。用变更管理的行话，我说的是当变更活动不应该发生时，指定封锁窗口。


### Ansible 中的日历集成


如何在 [Ansible](https://docs.ansible.com/ansible/latest/index.html) 中实现这个功能？虽然它本身没有日历功能，但 Ansible 的可扩展性将允许它与任何具有 API 的日历应用集成。


目标是这样的：在执行任何自动化或变更活动之前，你要执行一个 `pre-task` ，它会检查日历中是否已经安排了某些事情（目前或最近），并确认你没有在一个阻塞的时间段中。


想象一下，你有一个名为 `calendar` 的虚构模块，它可以连接到一个远程日历，比如 Google 日历，以确定你指定的时间是否已经以其他方式被标记为繁忙。你可以写一个类似这样的剧本：



```
- name: Check if timeslot is taken
  calendar:
    time: "{{ ansible_date_time.iso8601 }}"
  register: output

```

Ansible 实际会给出 `ansible_date_time`，将其传递给 `calendar` 模块，以验证时间的可用性，以便它可以注册响应 （`output`），用于后续任务。


如果你的日历是这样的：


![Google Calendar screenshot](/data/attachment/album/202010/18/221150jupznzhuu5fk5k9a.png "Google Calendar screenshot")


那么这个任务的输出就会指明这个时间段被占用的事实 （`busy: true`）：



```
ok: [localhost] =&gt; {
   "output": {
       "busy": true,
       "changed": false,
       "failed": false,
       "msg": "The timeslot 2020-09-02T17:53:43Z is busy: true"
   }
}

```

### 阻止任务运行


接下来，[Ansible Conditionals](https://docs.ansible.com/ansible/latest/user_guide/playbooks_conditionals.html) 将帮助阻止所有之后任务的执行。一个简单的例子，你可以在下一个任务上使用 `when` 语句来强制它只有当上一个输出中的 `busy` 字段不是 `true` 时，它才会运行：



```
tasks:
  - shell: echo "Run this only when not busy!"
    when: not output.busy

```

### 总结


在[上一篇文章](https://medium.com/swlh/python-and-ansible-to-automate-a-network-security-workflow-28b9a44660c6)中，我说过 Ansible 是一个将事物连接在一起的框架，将不同的组成部分相互连接，以协调端到端自动化工作流。


这篇文章探讨了 Ansible 剧本如何与日历应用集成以检查可用性。然而，我只做了一些表面工作！例如，你的任务也可以阻止日历中的一个时间段，这里的发挥空间很大。


在我的下一篇文章中，我将深入 `calendar` 模块是如何构建的，以及其他编程语言如何与 Ansible 一起使用。如果你和我一样是 [Go](https://golang.org/) 的粉丝，请继续关注！




---


*这篇文章最初发表在 Medium 上，名为 [Ansible and Google Calendar integration for change management](https://medium.com/swlh/ansible-and-google-calendar-integration-for-change-management-7c00553b3d5a)，采用 CC BY-SA 4.0 许可，经许可后转载。*




---


via: <https://opensource.com/article/20/10/calendar-ansible>


作者：[Nicolas Leiva](https://opensource.com/users/nicolas-leiva) 选题：[lujun9972](https://github.com/lujun9972) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
Is "anytime" a good time to execute your automation workflow? The answer is probably no, for different reasons.

If you want to avoid simultaneous changes to minimize the impact on critical business processes and reduce the risk of unintended service disruptions, then no one else should be attempting to make changes at the same time your automation is running.

In some scenarios, there could be an ongoing scheduled maintenance window. Or maybe there is a big event coming up, a critical business time, or a holiday—or maybe you prefer not to make changes on a Friday night.

![Street scene with a large calendar and people walking Street scene with a large calendar and people walking](https://opensource.com/sites/default/files/uploads/street-calendar.jpg)


Whatever the reason, you want to signal this information to your automation platform and prevent the execution of periodic or ad-hoc tasks during specific time slots. In change management jargon, I am talking about specifying blackout windows when change activity should not occur.

## Calendar integration in Ansible

How can you accomplish this in [Ansible](https://docs.ansible.com/ansible/latest/index.html)? While it has no calendar function per se, Ansible's extensibility will allow it to integrate with any calendar application that has an API.

The goal is this: Before you execute any automation or change activity, you execute a `pre-task`

that checks whether something is already scheduled in the calendar (now or soon enough) and confirms you are not in the middle of a blocked timeslot.

Imagine you have a fictitious module named `calendar`

, and it can connect to a remote calendar, like Google Calendar, to determine if the time you specify has otherwise been marked as busy. You could write a playbook that looks like this:

```
- name: Check if timeslot is taken
calendar:
time: "{{ ansible_date_time.iso8601 }}"
register: output
```


Ansible facts will give `ansible_date_time`

, which is passed to the `calendar`

module to verify the time availability so that it can register the response (`output`

) to use in subsequent tasks.

If your calendar looks like this:

![Google Calendar screenshot Google Calendar screenshot](https://opensource.com/sites/default/files/uploads/googlecalendarexample.png)

(Nicolas Leiva, CC BY-SA 4.0)

Then the output of this task would highlight the fact this timeslot is taken (`busy: true`

):

```
ok: [localhost] => {
"output": {
"busy": true,
"changed": false,
"failed": false,
"msg": "The timeslot 2020-09-02T17:53:43Z is busy: true"
}
}
```


## Prevent tasks from running

Next, [Ansible Conditionals](https://docs.ansible.com/ansible/latest/user_guide/playbooks_conditionals.html) will help prevent the execution of any further tasks. As a simple example, you could use a `when`

statement on the next task to enforce that it runs only when the field `busy`

in the previous output is not `true`

:

```
tasks:
- shell: echo "Run this only when not busy!"
when: not output.busy
```


## Conclusion

In a [previous article](https://medium.com/swlh/python-and-ansible-to-automate-a-network-security-workflow-28b9a44660c6), I said Ansible is a framework to wire things together, interconnecting different building blocks to orchestrate an end-to-end automation workflow.

This article looked at how playbooks can integrate or talk to a calendar application to check availability. However, I am just scratching the surface! For example, your tasks could also block a timeslot in the calendar… the sky is the limit.

In my next article, I will dig into how the `calendar`

module is built and how other programming languages can be used with Ansible. Stay tuned if you are a [Go](https://golang.org/) fan like me!

*This originally appeared on Medium as Ansible and Google Calendar integration for change management under a CC BY-SA 4.0 license and is republished with permission.*

## Comments are closed.