Problem with variables structure (loop with_items)

53 views
Skip to first unread message

wrede...@gmail.com

unread,
Apr 13, 2015, 8:12:21 AM4/13/15
to ansible...@googlegroups.com
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.

kzabk...@gmail.com

unread,
Apr 15, 2015, 3:12:26 PM4/15/15
to ansible...@googlegroups.com
Check with_subelements loop. This is solution to your problem.

Krzysztof Ząbkowski

http://docs.ansible.com/playbooks_loops.html#looping-over-subelements
Reply all
Reply to author
Forward
0 new messages