is with_nested the right approach here?

Hi,

I’m writing a cookbook to deploy a service, of which multiple instances can run at the same time on the same host.
Each instance requires 3 config files: config_X.yml, config_X.env, log_X.conf, where X=instance number.
I have a template for each config file.

I want the cookbook to set things up for 3 instances.
Having read the doc, it seems I could go in this direction:

Define the 3 instances in an array variable called instances (each having an index member), and loop over it:

  • include: lib/copy_config_files.yml
    with_nested:
  • "{{ instances }} "
  • [ “config_X.yml”, “config_X.env”, “log_X.conf” ]

and in the lib/copy_config_files.yml, loop over the config files:

  • name: copy mode 1 config
    template: …
    when: mode == 1
  • name: copy mode 2 config
    template: …
    when: mode == 2
  • name: “Copy configs”
    template: src=templates/{{item[1]}} dest=/etc/{{item[1] | regex_replace(‘X’,{{item[0].index}})}} owner=root group=root mode=0744

First question: is this the right approach?

Second: what’s the best solution to the conditional template for mode1 and mode2 configs? With this cookbook it is unnecessarily generated multiple times. Is the solution to extract these to a seperate fil included one level higher?

thanks in advance for any tips!

Raphaël

Hello,

I tried this:

  • include: lib/copy_config_files.yml
    with_nested:
  • "{{ instances }} "
  • [ “config_X.yml”, “config_X.env”, “log_X.conf” ]

with the included cookbook:

  • name: “Copy configs”
    template: src=templates/{{item[1]}} dest=/etc/{{item[1] | regex_replace(‘X’,{{item[0].
    index}})}} owner=root group=root mode=0744

but it seems item is unknown in the include tasks. Any idea what I’m doing wrong here?

Thanks

Raphaël

cookbook is chef, ansible uses playbooks :wink:

I think there is an error in your dest, you use double curly braces
inside of double curly braces:

template:
  src: templates/{{item[1]}}
  dest: /etc/{{item[1] | regex_replace('X',{{item[0]. > index}})}}
  owner: root
  group: root
  mode: 0744

We had this recently, change the inner braces to single square ones:

dest: /etc/{{ item[1] | regex_replace('X', [ item[0]. > index ] ) }}

Untested!

Johannes