Search code examples
ansibleansible-2.xansible-inventory

Ansible playbook batch for 10 hosts


I have an ansible playbook that is currently running for one server at a time (serial: 1) with one minute interval in between host runs.

I have about 200 hosts in my inventory file.
Here's what I want to do: I want to have the playbook execute on 10 hosts, then I need to validate the run, and then go for the next set of 10 hosts. But within the 10 hosts, I want to execute the playbook serially.

I now want to have a batch of 10 hosts while still running one server at a time.

How do I do this?


Solution

  • You could use a mechanism that is described under the section using group position in patterns of Ansible documentation.

    In short, this could allow you to slice an existing group in order to have only one subset of that group:

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

    Still, that would need you to edit your playbook after each batch validation in your use case, so it is not extremely convenient.

    On the other hand, you could construct your hosts based on a variable you would ask from localhost.

    Given the playbook:

    - 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 }}"
    

    And the inventory:

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

    Here are some recap:

    • 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  
      

    Mind that I am purposely starting the first slice at the position 1, because in my inventory (cf above), localhost is the host at the position 0, otherwise, the first slice would have only four element (because it would have excluded localhost with :!locahost).