is with_nested the right approach here?

17 views
Skip to first unread message

raphael...@tessares.net

unread,
Jan 23, 2017, 10:23:27 AM1/23/17
to Ansible Project
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



DISCLAIMER.
This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you have received this email in error please notify the system manager. This message contains confidential information and is intended only for the individual named. If you are not the named addressee you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. If you are not the intended recipient you are notified that disclosing, copying, distributing or taking any action in reliance on the contents of this information is strictly prohibited.

raphael...@tessares.net

unread,
Jan 24, 2017, 3:40:40 AM1/24/17
to Ansible Project

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

Johannes Kastl

unread,
Jan 24, 2017, 5:43:29 AM1/24/17
to ansible...@googlegroups.com
On 24.01.17 09:40 raphael...@tessares.net wrote:

> with the included cookbook:

cookbook is chef, ansible uses playbooks ;-)

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

signature.asc
Reply all
Reply to author
Forward
0 new messages