如何配置“全有或全无"? Ansible剧本? [英] How can I configure an "all or nothing" Ansible playbook?

查看:92
本文介绍了如何配置“全有或全无"? Ansible剧本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆需要频繁修补的服务器.我计划使用Ansible协调修补过程.这里的要点是,它必须是全有或全无"修补程序.所有服务器均已打补丁或未打补丁.

I have a bunch of servers that need will be need frequent patching. I am planning on using Ansible to coordinate the patching process. The keep point here is that it must be an "all or nothing" patching. Either all servers are patched or none.

我正在为剧本考虑的任务如下: 1-转到所有服务器并拍摄lvm快照 2-IIF任务1适用于所有服务器,请应用更改 3-如果其中一台主机由于任何原因发生故障,请在ALL NODES上回滚快照.

The tasks I was considering for my playbook would be something like: 1 - Go to all servers and take an lvm snapshot 2 - IIF task 1 works on all servers, apply the changes 3 - If one of the hosts fails for any reason, roll back the snapshot on ALL NODES.

问题在于我是Ansible的新手,无法在剧本上表达这一点.我写了这个简单的测试手册:

The problem is that I am new to Ansible and I can't express this on a playbook. I have written this simple testing playbook:

---
- hosts: all
  strategy: linear

  tasks:
  - block:
      - debug: msg='Testing on {{ inventory_hostname }}...'
      - command: /home/amirsamary/activity.sh
        changed_when: false
    rescue:
      - debug: msg='Rollback of {{ inventory_hostname }}...'
  - debug: msg='I continued running tasks on {{ inventory_hostname }}...'

我的库存中有两个主机.在第一个节点上,activity.sh返回true,在第二个节点上,activity.sh返回false.因此,node2将始终失败.问题在于,救援任务将仅针对发生故障的主机运行,而不会针对所有主机运行(正如人们期望的那样),而剧本则继续运行其他任务.

I have two hosts on my inventory. On the first node, activity.sh returns true and on the second node, activity.sh returns false. So, node2 will always fail. The problem is that the rescue tasks will only run for the failed host and not for all of them (as one would expect anyway) and the playbook keeps running the other tasks.

我听说过很多有关Ansible在数千台服务器上协调复杂任务的能力.但是我似乎找不到找到一种安全地实施全有或全无"策略的方法.我想念什么?

I have heard a lot about how good Ansible was to orchestrate complex tasks on thousands of servers. But I can't seem to find a way of safely implement an "all or nothing strategy" with it. What am I missing?

推荐答案

我敢肯定有很多方法可以实现这一点,这是其中一种:

I bet there are many ways to implement this, here is one of them:

---
- hosts: all
  strategy: linear

  tasks:
    - debug: msg='Testing on {{ inventory_hostname }}...'
    - command: /home/amirsamary/activity.sh
      register: cmd_result
      ignore_errors: true
    - debug: msg='Rollback of {{ inventory_hostname }}...'
      when: play_hosts | map('extract', hostvars, 'cmd_result') | selectattr('failed','defined') | list | count > 0

这里做什么?

  • 我们将脚本执行的结果注册到cmd_result中,并忽略错误(如果有的话)
  • 采用线性策略,我们将在所有主机上完成command任务,然后再执行下一个任务
  • 所以我们为每个主机都注册了cmd_result
  • 要检查是否需要回滚,请提取当前播放中所有主机的cmd_result事实,选择定义为failed的主机,将其转换为列表并计数:如果有,请回滚.
  • we register result of script execution into cmd_result and ignore errors, if any
  • with linear strategy, we will have command task completed on all hosts before next task being executed
  • so we have cmd_result registered for every host
  • to check if we need to rollback we extract cmd_result facts for all hosts in the current play, select those with failed defined, convert them to list and count them: if there is any, rollback.

因此,如果其中的任何一个主机cmd_result失败,则将对所有主机执行回滚任务.

So rollback task will be executed for all hosts if there is failed cmd_result for any of them.

您可能要在回滚任务之后添加此任务:

You may want to add this task after rollback task:

- fail: msg='Patch command failed!'
  when: cmd_result | failed

这样,您将完成回滚任务,并将问题主机标记为失败.

This way you will have your rollback tasks done and also mark problem hosts as failed.

这篇关于如何配置“全有或全无"? Ansible剧本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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