我如何在 Ansibe 的 50% 机器上发布代码 [英] How can i release code on 50% machine in Ansibe

查看:16
本文介绍了我如何在 Ansibe 的 50% 机器上发布代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在 aws 中运行了 100 台机器,标签为 Name=ad_server那么我如何才能仅在 50% 的机器上运行发布代码.示例:

we have running 100 Machines in aws with tag Name=ad_server so how can i run release code only 50% machines. Example:

 - hosts: tag_Name_ad_server
   sudo: yes
   remote_user: ubuntu

   tasks:
     - name: whatever

那我该怎么做..

推荐答案

选项 1:串行 + 暂停

这将使用批量的标准功能进行滚动更新,并需要一些用户交互.
我们添加带有提示的 pause 作为第一个任务,以等待在每个批次开始时按下 ENTER.
因此,在第一个 50% 批次上按 ENTER,然后在要求开始下半场时中止执行.

Option 1: serial + pause

This will use standard feature of batches for rolling updates and require some user interaction.
We add pause with prompt as a first task to wait for ENTER to be pressed at the start of every batch.
So press ENTER on the first 50%-batch and then abort execution when asked to start the second half.

- hosts: tag_Name_ad_server
  user: ubuntu
  serial: "50%"
  tasks:
    - pause: prompt="Press ENTER to run this batch"
    - shell: echo task1
    - shell: echo task2

每次运行 playbook 时 serial 将始终从清单中选择相同的服务器.在这种情况下,列表中的前 50%.

With every playbook run serial will always choose the same servers from inventory. In this case first 50% from the list.

循环遍历 tag_Name_ad_server 组中的主机并形成一个新的 50_percent 组.
然后在这个新组中执行您的实际任务.
无需用户交互.

Loop through hosts in tag_Name_ad_server group and form a new 50_percent group.
Then execute your actual tasks within this new group.
No user interaction required.

- hosts: tag_Name_ad_server
  gather_facts: no
  tasks:
    - group_by: key=50_percent
      when: 100 | random > 50

- hosts: 50_percent
  user: ubuntu
  tasks:
    - shell: echo task1
    - shell: echo task2

随着 playbook run random 将从列表中随机选择 50% 的服务器.

With ever playbook run random will choose 50% random servers from the list.

如果你只有一个任务要运行,你可以使用 max_fail_percentage: -1 在第一批的第一个任务之后停止执行.
这将生成 50% 的批次,在该批次的每个主机上执行第一个任务并检查失败的服务器计数,因为它总是高于 -1 剧本将停止执行.

If you have only one task to run, you can use max_fail_percentage: -1 to stop execution after first task of first batch.
This will make a 50%-batches, execute first task on every host of this batch and check failed servers count, as it always will be above -1 playbook will stop execution.

- hosts: tag_Name_ad_server
  user: ubuntu
  serial: "50%"
  max_fail_percentage: -1
  tasks:
    - shell: echo task1
    - shell: echo task2 # this task will never be executed with max_fail_percentage == -1

这篇关于我如何在 Ansibe 的 50% 机器上发布代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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