Dennis Wisnia
Dennis Wisnia

Reputation: 81

Ansible Variables only X times per host

I am actually writing an ansible playbook, for the automatically installation of Docker Container with several same instances on an host with minor differences.

I have an Variable File with "Customers" and every customer has an own docker container, my idea is: I have a bunch of hosts, and ansible deploys 5 instances per host with 5 different containers. Of course, I can define an new var with the hostname, but it seems a bit smarter, that ansible check: "okay, already 5 instances, next host".

Any Idea?

Upvotes: 0

Views: 62

Answers (1)

Konstantin Suvorov
Konstantin Suvorov

Reputation: 68239

Possible solution:

---
- hosts: all
  gather_facts: no
  vars:
    containers: ['cont1', 'cont2', 'cont3', 'cont4', 'cont5', 'cont6', 'cont7', 'cont8']
    per_host: 3
  tasks:
    - debug: msg='Deploy {{ item }}'
      with_items: '{{ containers[ play_hosts.index(inventory_hostname)*per_host : (play_hosts.index(inventory_hostname)+1)*per_host] }}'

This will take a section of containers list with per_host size based and host index as offset.
So in this example you will receive no more than 3 debugs per host in your inventory.

Upvotes: 1

Related Questions