Silver
Silver

Reputation: 25

Sequence in batches of five hosts

I would like to sequence in batches of 5 inventory host in my playbook for to 5 actions in the same time in my crontab and 5 other inventory host an other time like in this exemple:

- name: Ajout de la découverte
  delegate_to: nedi.mousquetaires.com
  cron:
    name: "Découverte de la base de {{ inventory_hostname }}"
    minute: "{{ item.minute }}"
    hour: "{{ item.heure }}"
    job: 'php /var/scripts/nedi/discover.php "{{ item.host }}"'
    user: apache
  with_items: 
    - { host: '{{ inventory_hostname }}', minute: '00', heure: '00' }
    - { host: '{{ inventory_hostname }}', minute: '00', heure: '00' }
    - { host: '{{ inventory_hostname }}', minute: '00', heure: '00' }
    - { host: '{{ inventory_hostname }}', minute: '00', heure: '00' }
    - { host: '{{ inventory_hostname }}', minute: '00', heure: '00' }
  when: 
    - result_crontab.stdout is not search("discover.php {{ inventory_hostname }}")



- name: Ajout de la découverte
  delegate_to: nedi.mousquetaires.com
  cron:
    name: "Découverte de la base de {{ inventory_hostname }}"
    minute: "{{ item.minute }}"
    hour: "{{ item.heure }}"
    job: 'php /var/scripts/nedi/discover.php "{{ item.host }}"'
    user: apache
  with_items: 
    - { host: '{{ inventory_hostname }}', minute: '10', heure: '00' }
    - { host: '{{ inventory_hostname }}', minute: '10', heure: '00' }
    - { host: '{{ inventory_hostname }}', minute: '10', heure: '00' }
    - { host: '{{ inventory_hostname }}', minute: '10', heure: '00' }
    - { host: '{{ inventory_hostname }}', minute: '10', heure: '00' }
  when: 
    - result_crontab.stdout is not search("discover.php {{ inventory_hostname }}")

The problem is that i have many hosts (82 for the moment) and i would like to cut them in group of 5 each time,

the first group of 5= 0
the second group of 5= 5
the third group of 5= 10
etc...

And the same for the hour cron, to start to 00am and finish to 5am max ...

I have this crontab what i must build with ansible now :

10 0 * * * php /var/scripts/nedi/backup.php "garancieres|-051-" #sauvegarde garancieres

10 0 * * * php /var/scripts/nedi/backup.php "beziers|Beziers|-010-" #sauvegarde de beziers

10 0 * * * php /var/scripts/nedi/backup.php "mauchamps|-011-" #sauvegarde mauchamps

10 0 * * * php /var/scripts/nedi/backup.php "pezenas|-040-" #sauvegarde de pezenas

10 0 * * * php /var/scripts/nedi/backup.php "vimy|-074-" #sauvegarde vimy



10 0 * * * php /var/scripts/nedi/discover.php "-037-|levet" #decouverte de labuissiere

10 0 * * * php /var/scripts/nedi/discover.php "-056-|albon" #decouverte albon

10 0 * * * php /var/scripts/nedi/discover.php "-055-|amilly" #decouverte de amilly

10 0 * * * php /var/scripts/nedi/discover.php "-058-|anais" #decouverte anais

10 0 * * * php /var/scripts/nedi/discover.php "-021-|argentre" #decouverte de argentre



20 0 * * * php /var/scripts/nedi/backup.php "-037-|levet" #sauvegarde labuissiere

20 0 * * * php /var/scripts/nedi/backup.php "-056-|albon" #sauvegarde de albon

20 0 * * * php /var/scripts/nedi/backup.php "-055-|amilly" #sauvegarde amilly

20 0 * * * php /var/scripts/nedi/backup.php "-058-|anais" #sauvegarde de anais

20 0 * * * php /var/scripts/nedi/backup.php "-021-|argentre" #sauvegarde argentre



20 0 * * * php /var/scripts/nedi/discover.php "-072-|brignoles" #decouverte de brignoles

20 0 * * * php /var/scripts/nedi/discover.php "-031-|chaulnes" #decouverte chaulnes

20 0 * * * php /var/scripts/nedi/discover.php "-024-|gournay" #decouverte de gournay

20 0 * * * php /var/scripts/nedi/discover.php "-074-|henin-beaumont" #decouverte de henin-beaumont

20 0 * * * php /var/scripts/nedi/discover.php "charleroi" #decouverte charleroi



30 0 * * * php /var/scripts/nedi/backup.php "-072-|brignoles" #sauvegarde brignoles

30 0 * * * php /var/scripts/nedi/backup.php "-031-|chaulnes" #sauvegarde de chaulnes

30 0 * * * php /var/scripts/nedi/backup.php "-024-|gournay" #sauvegarde gournay

30 0 * * * php /var/scripts/nedi/backup.php "-074-|henin-beaumont" #sauvegarde de henin-beaumont

30 0 * * * php /var/scripts/nedi/backup.php "charleroi" #sauvegarde de charleroi

Note: I can edit my host file if it is needed :)

Upvotes: 1

Views: 95

Answers (1)

β.εηοιτ.βε
β.εηοιτ.βε

Reputation: 39169

You can most probably have that in one single task, and have it more dynamic.

I suppose that the group of host you are targeting for the moment is a group of ten hosts, but what should happen if you have eleven? Then twelve?

What you could do, though is to leverage the list of hosts in the play that the special variable ansible_play_hosts provides, and then use a list.index(item), to get its position in the list, and so, make a little bit of math to see when it should be launched.

In your math operation, it would be useful to know that // is used to

Divide two numbers and return the truncated integer result.

So, something like:

- cron:
    name: "Découverte de la base de {{ inventory_hostname }}"
    minute: >-
      {{ 
        ansible_play_hosts.index(inventory_hostname) 
          // (ansible_play_hosts | length // 2) 
          * 5 
      }}
    hour: "0"
    job: 'php /var/scripts/nedi/discover.php "{{ inventory_hostname }}"'
    user: apache
  delegate_to: nedi.mousquetaires.com

This even make it future proof, if you want to split them in more than two, change ansible_play_hosts | length // 2 by ansible_play_hosts | length // 3, and you're good to go.


Here is a really trimmed down example of what the minute parameter of the cron would render with that math:

- debug:
    msg: >-
      {{
        ansible_play_hosts.index(inventory_hostname)
          // (ansible_play_hosts | length // 2)
          * 5
      }}

Will give:

ok: [albon2] => 
  msg: '0'
ok: [alcanena] => 
  msg: '0'
ok: [albon] => 
  msg: '0'
ok: [amilly] => 
  msg: '0'
ok: [anais-db] => 
  msg: '0'
ok: [angouleme] => 
  msg: '5'
ok: [anais] => 
  msg: '5'
ok: [paris] => 
  msg: '5'
ok: [lyon] => 
  msg: '5'
ok: [marseille] => 
  msg: '5'

Upvotes: 1

Related Questions