Use Array/list file in with_items

337 views
Skip to first unread message

Amoyensis

unread,
May 27, 2015, 7:02:28 AM5/27/15
to ansible...@googlegroups.com

Hi all,

I am new to Ansible and I am trying use the template module to create different config files.

Here is what I got:
├── group_vars
│   └── web
└── web
    ├── tasks
    │   └── main.yml
    └── templates
        └── virtualhost.conf

group_vars/web:

domain:
- alpha
- bravo
- charly

/web/tasks/main.yml:

- name: 7. Add Template Module
  template: src=virtualhost.conf dest=/etc/apache2/sites-available/{{domain}}.conf
  with_items: domain

I want, that the task creates a file for each array field in the domain list. So, what I expect are three files, named alpha.conf beta.conf and charly.conf. But I just got one file, called ['alpha', 'bravo', 'charly'].conf

Job message:

src=virtualhost.conf dest=/etc/apache2/sites-available/['alpha', 'bravo', 'charly'].conf

When I use 

with_items: 

 - alpha

 - bravo 

 - charly

in the task itself, it works. But I want to declare the array in a separate file (groupvar).

Can you tell my how to fix this?


Kind regards,

Amoyensis

Brian Coca

unread,
May 27, 2015, 7:03:25 AM5/27/15
to ansible...@googlegroups.com
each item is stored in a special 'item' variable when you have a with_
loop, so you just need:

dest=/etc/apache2/sites-available/{{item}}.conf
> --
> You received this message because you are subscribed to the Google Groups
> "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ansible-proje...@googlegroups.com.
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/ffb18b94-2281-45f8-b44f-1e2e402a448f%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
Brian Coca
Reply all
Reply to author
Forward
0 new messages