0

I am trying to deploy multiple instances of a service on a single host. The service has a config file, and in this config file, I'd like to include the index of the instance that it belongs to. For example:

# main.yml
- name: Configure each Instance
  template: src=config dest=/usr/service/etc/config@{{item}}-{{count}}
  with_items:
    - "A"
    - "B"
    - "C"


# templates/config
id={{count}}

So ideally, I will end up with:

config@A-1
config@B-2
config@C-3

But I'm stuck on how I can get the "count" variable in there. I've looked at with_indexed_items, but I'm not sure if that is the right way. I also looked at using a list of lists like this:

with_items:
 - ["A", "1"]
 - ["B", "2"]
 - ["C", "3"]

But that doesn't feel like the most "Ansible" way to do this.

user3270760
  • 163
  • 2
  • 2
  • 8

1 Answers1

0

I solved this using the with_sequence loop and abstracting the A, B, C value into the vars file:

- name: Configure each Instance
  template: src=config dest=/usr/service/etc/config@{{value}}-{{item}}
  with_sequence: count={{num_instances}}

Then my template looks like this:

# templates/config
id={{item}}
user3270760
  • 163
  • 2
  • 2
  • 8