Provisioning a given number of cloud machines

Hello, I’m new to Ansible, I’m using DigitalOcean cloud, but my question concerns general Ansible scripting.

We have an application that requires, say, 40 servers during Saturday and Sunday and only 20 servers during week days.

I’m trying to compose a playbook that, given a desired quantity of machines, compares it with actual number of machines and:

— if desired < actual: performs include/block/role/whatever that sets up new machines until the desired quantity of machines is reached
— else: randomly shuts down machines in the cloud until the desired quantity is reached.

I found that it is surprisingly difficult to achive with Ansible. There are no -block with :until loop, -include with :until loop seems not to work with variables that are
set inside include. What I was able to do so far is calculating ‘delta’ variable and using ‘with_sequence’ which is not that I actually need, since execution inside the loop may fail to add/shut down a machine.

What whould you suggest as a ‘right Ansible way’ to do what we need, to achive “there are N servers in the cloud” state?

Regards,

Ivan

After a week of studying Ansible I came to the answer myself.

‘The right Ansible way’ seems to do all the ‘procedural/algorithmic’ part in Action Plugins (https://www.ansible.com/blog/how-to-extend-ansible-through-plugins) It is not hard to implement a plugin that returns a list of new machines’ names or a list of ‘victims’ to shut down.

Then you can run modules that create / shut down machines in simple with_items loops.

The moral is: Ansible playbooks are not for algorithmic things, if you need that, you may implement anything in plugins.

Regards,

Ivan