适用于 10 台主机的 Ansible 剧本批处理



我有一个 ansible 剧本,它目前正在一次为一台服务器运行(serial: 1(,主机运行之间的间隔为一分钟。

我的清单文件中有大约 200 个主机.
以下是我要做的:我想让 playbook 在 10 台主机上执行,然后我需要验证运行,然后进行下一组 10 台主机。但是在 10 个主机中,我想连续执行剧本。

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

我该怎么做?

您可以使用 Ansible 文档模式中使用组位置一节中描述的机制。

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

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

尽管如此,这需要您在用例中的每个批处理验证后编辑您的剧本,因此它不是非常方便。

另一方面,您可以根据从本地主机请求的变量来构造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(。

最新更新