# Problem with variables structure (loop with\_items)

**URL:** <https://forum.ansible.com/t/problem-with-variables-structure-loop-with-items/17959>\
**Category:** Ansible Project\
**Created:** [April 11, 2015, 9:12pm UTC](https://forum.ansible.com/t/problem-with-variables-structure-loop-with-items/17959 "2015-04-11T21:12:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![wredetobias](https://avatars.discourse-cdn.com/v4/letter/w/ce7236/32.png) [@wredetobias](https://forum.ansible.com/u/wredetobias)\
**Post date:** [April 11, 2015, 9:12pm UTC](https://forum.ansible.com/t/problem-with-variables-structure-loop-with-items/17959/1 "2015-04-11T21:12:42Z")

</div>

Hi, I am trying to create a role for multiple installations of apache on one host.

**My group\_vars:**  
apache:

- { name: vhost1,  
dir: installation1,  
…lot of other variables…  
file:

- { src: /deploy/1.txt, dst: /tmp/1.txt }

- { src: /deploy/2.txt, dst: /tmp/2.txt }  
}

- { name: vhost2,  
dir: installation2,  
…lot of other variables…  
file:

- { src: /deploy/3.txt, dst: /tmp/3.txt }  
}

**Task with problem:**

- name: copy files  
copy: src={{ item.file.src }} dest={{ item.file.dst }}  
with\_items: apache

**Error:**  
ERROR: Syntax Error while loading YAML script, /roles/apache/vars/main.yml  
Note: The error may actually appear before this position: line 39, column 9

file:

- { src: /deploy/1.txt, dst: /tmp/1.txt }

**I try:**  
**My group\_vars:**  
apache:

- name: vhost1  
dir: installation1  
…lot of other variables…  
file:

- { src: /deploy/1.txt, dst: /tmp/1.txt }

- { src: /deploy/2.txt, dst: /tmp/2.txt }

- name: vhost2  
dir: installation2  
…lot of other variables…  
file:

- { src: /deploy/3.txt, dst: /tmp/3.txt }

**Task with problem:**

- name: copy files  
copy: src={{ item.file.src }} dest={{ item.file.dst }}  
with\_items: apache

**Error:**  
fatal: [test] =\> One or more undefined variables: ‘list object’ has no attribute ‘src’

**and also try:**  
**My group\_vars**  
apache:

- name: vhost1  
dir: installation1  
…lot of other variables…  
file:

- src: /deploy/1.txt  
dst: /tmp/1.txt

- src: /deploy/2.txt  
dst: /tmp/2.txt

- name: vhost2  
dir: installation2  
…lot of other variables…  
file:

- src: /deploy/3.txt  
dst: /tmp/3.txt

**Task with problem:**

- name: copy files  
copy: src={{ item.file.src }} dest={{ item.file.dst }}  
with\_items: apache

**Error:**  
fatal: [test] =\> One or more undefined variables: ‘list object’ has no attribute ‘src’

How can I solve my problem? Depending on my needs, I need to have in the variable “file” specification of one or more files to be copied.

---

<div class="post-metadata">

**Author:** ![kzabkowski](https://avatars.discourse-cdn.com/v4/letter/k/f17d59/32.png) [@kzabkowski](https://forum.ansible.com/u/kzabkowski)\
**Post date:** [April 15, 2015, 7:12pm UTC](https://forum.ansible.com/t/problem-with-variables-structure-loop-with-items/17959/2 "2015-04-15T19:12:26Z")

</div>

Check with\_subelements loop. This is solution to your problem.

Krzysztof Ząbkowski

[http://docs.ansible.com/playbooks\_loops.html#looping-over-subelements](http://docs.ansible.com/playbooks_loops.html#looping-over-subelements)

W dniu poniedziałek, 13 kwietnia 2015 14:12:21 UTC+2 użytkownik [wrede...@gmail.com](mailto:wrede...@gmail.com) napisał:
