【问题标题】:Ansible playbook batch for 10 hosts适用于 10 个主机的 Ansible playbook 批处理
【发布时间】:2020-07-07 16:40:06
【问题描述】:

我有一个 ansible playbook,当前一次为一台服务器运行 (serial: 1),主机运行之间的间隔为一分钟。

我的清单文件中有大约 200 台主机。
这就是我想做的事情:我想让剧本在 10 台主机上执行,然后我需要验证运行,然后再使用下一组 10 台主机。但是在 10 个主机中,我想串行执行剧本。

我现在想拥有一批 10 台主机,同时仍一次运行一个服务器。

我该怎么做?

【问题讨论】:

  • 将十台主机放入您的库存组?老实说,这个问题并不是很清楚。
  • 我的清单文件中有大约 200 台主机,我首先一次运行我的 playbook 1 服务器。现在我想将清单主机批处理为 5 个,但在批处理中我想一次执行一个主机
  • 仍不清楚。如果我有一扇门一次只让一个人通过,那么我有一个、5 个或一百万个等待没有区别。这就是您在此处解释的内容,如果您仍希望它们一次连续运行一个,则没有“批处理”...
  • 这就是我想要做的:我想让 playbook 在 5 台主机上执行,然后我需要验证运行,然后再选择下一组 5 台主机。但在 5 台主机中,我想连续执行剧本。很抱歉我之前没有说清楚。希望这能提供一个想法
  • 因此,如果您将它们分组到主机组中,每 5 个,您就可以实现这一点。否则你希望如何指定切片?你希望剧本会问什么吗?

标签: ansible ansible-2.x ansible-inventory


【解决方案1】:

您可以使用 Ansible 文档的 using group position in patterns 部分中描述的机制。

简而言之,这可以让您对现有组进行切片,以便仅拥有该组的一个子集:

hosts: all[0:4]
hosts: all[5:9]
hosts: all[10:14]

不过,这需要您在用例中的每次批量验证后编辑您的剧本,因此不是非常方便。

另一方面,您可以根据从 localhost 询问的变量构造您的 hosts

鉴于剧本:

- hosts: localhost
  gather_facts: no
  vars_prompt:
    - name: from
      prompt: "Where should we start?"
      default: 1
      private: false

  tasks:
    - set_fact:
        hosts: "all[{{ from }}:{{ from | int + 4 }}]:!localhost"

- hosts: "{{ hostvars['localhost']['hosts'] }}"
  gather_facts: no
  tasks:
    - debug:
        msg: "{{ inventory_hostname }}"

还有库存:

all:
  hosts:
    localhost:
    host1:
    host2:
    host3:
    host4:
    host5:
    host6:
    host7:
    host8:
    host9:
    host10:
    host11:

这里有一些回顾:

  • Where should we start? [1]: 
    
    PLAY [localhost] *****************************************************************************************************************
    
    TASK [set_fact] ******************************************************************************************************************
    ok: [localhost]
    
    PLAY [all[1:5]:!localhost] *******************************************************************************************************
    
    TASK [debug] *********************************************************************************************************************
    ok: [host1] => {
        "msg": "host1"
    }
    ok: [host2] => {
        "msg": "host2"
    }
    ok: [host3] => {
        "msg": "host3"
    }
    ok: [host4] => {
        "msg": "host4"
    }
    ok: [host5] => {
        "msg": "host5"
    }
    
    PLAY RECAP ***********************************************************************************************************************
    host1                      : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
    host2                      : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
    host3                      : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
    host4                      : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  
    host5                      : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0    
    localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  
    
  • Where should we start? [1]: 6
    
    PLAY [localhost] *****************************************************************************************************************
    
    TASK [set_fact] ******************************************************************************************************************
    ok: [localhost]
    
    PLAY [all[6:10]:!localhost] *******************************************************************************************************
    
    TASK [debug] *********************************************************************************************************************
    ok: [host6] => {
        "msg": "host6"
    }
    ok: [host7] => {
        "msg": "host7"
    }
    ok: [host8] => {
        "msg": "host8"
    }
    ok: [host9] => {
        "msg": "host9"
    }
    ok: [host10] => {
        "msg": "host10"
    }
    
    PLAY RECAP ***********************************************************************************************************************
    host10                     : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
    host6                      : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
    host7                      : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
    host8                      : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
    host9                      : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
    localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 
    
  • Where should we start? [1]: 11
    
    PLAY [localhost] *****************************************************************************************************************
    
    TASK [set_fact] ******************************************************************************************************************
    ok: [localhost]
    
    PLAY [all[11:15]:!localhost] *****************************************************************************************************
    
    TASK [debug] *********************************************************************************************************************
    ok: [host11] => {
        "msg": "host11"
    }
    
    PLAY RECAP ***********************************************************************************************************************
    host11                     : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0     
    localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  
    

请注意我特意在1 位置开始第一个切片,因为在我的库存(参见上文)中,localhost 是定位0,否则,第一个切片将只有四个元素(因为它将排除localhost:!locahost)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-17
    • 2017-09-08
    • 2021-11-04
    • 1970-01-01
    • 2020-11-13
    相关资源
    最近更新 更多