1
votes

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?

1
Put ten host in your inventory group? The question is not really clear, to be honest. - β.εηοιτ.βε
I have about 200 hosts in my inventory file, and I was running my playbook 1 server at a time first. Now I want to batch the inventory hosts into 5, but within the batch I want to execute the playbook one host at a time - Yong zhu
Still unclear. If I have a gate letting only one person at a time pass, there is no difference if I have one, 5 or a million waiting. That's what you are explaining here, there is no "batch" if you still want them to run serially one at a time... - β.εηοιτ.βε
Here's what I want to do: I want to have the playbook execute on 5 hosts, then I need to validate the run, and then go for the next set of 5 hosts. But within the 5 hosts, I want to execute the playbook serially. I'm sorry I wasn't clear before. Hope this gives an idea - Yong zhu
And so, if you group them in hosts groups, per 5, you can achieve this. Otherwise how do you expect to specify the slice? Do you expect the playbook to ask or something? - β.εηοιτ.βε

1 Answers

3
votes

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).