cancel
Showing results for 
Search instead for 
Did you mean: 
spurs
Flight Engineer
Flight Engineer
  • 435 Views

loop variable question

Jump to solution

Hello, 

Assume that vars.yml page has

 

[web_config_files]

- src: ~~

  dest: ~~

- src: ~~

  dest: ~~

 

if I want to copy the file from src to dest, why can't I use this syntax?

- name: Copy Config Files
          copy:
            src: "{{ web_config_files.src }}"
            dest: "{{ web_config_files.dest }}"
          

  

The correct answer is shown as

Should there be always item in the variable section?

 - name: Copy Config Files
          copy:
            src: "{{ item.src }}"
            dest: "{{ item.dest }}"
          loop: "{{ web_config_files }}"

 

Labels (1)
1 Solution

Accepted Solutions
ksaid
Mission Specialist
Mission Specialist
  • 235 Views

You need to check the property data type of the ansible.builtin.copy module. If the type is list, then you can use the list directory, eg.

"{{ web_config_files.src }}

But in your case (copy module) the type for src is "path" and same with dest. Then you have to use the loop function or use the other option that was mentioned by @shura 

One example where you can implement the list directory is in ansible.builtin.dnf where the name property is list data type.

It is always good to check the documentations for the data type

ansible-doc ansible.builtin.copy:

- src
  type: path

- dest
  type: path

------------------------------------------------

ansible-doc ansible.builtin.dnf

- name
  type: list

View solution in original post

4 Replies
shura
Flight Engineer
Flight Engineer
  • 402 Views

Hello @spurs 

Please, use correct structures name. According your example the syntax correct:

  - name: copy1
    copy:
       src: "{{ web_config_files[0].src }}"
       dest: "{{ web_config_files[0].dest }}"

  - name: copy2
    copy:
         src: "{{ web_config_files[1].src }}"
         dest: "{{ web_config_files[1].dest }}"

 

spurs
Flight Engineer
Flight Engineer
  • 184 Views

Thanks for the reply

so, instead of this

 src: "{{ web_config_files.src }}"
 dest: "{{ web_config_files.dest }}"

I can use the above code including [0], [1] (array)?

But, I noticed it's generated two different tasks which is inefficient. right? 

ksaid
Mission Specialist
Mission Specialist
  • 236 Views

You need to check the property data type of the ansible.builtin.copy module. If the type is list, then you can use the list directory, eg.

"{{ web_config_files.src }}

But in your case (copy module) the type for src is "path" and same with dest. Then you have to use the loop function or use the other option that was mentioned by @shura 

One example where you can implement the list directory is in ansible.builtin.dnf where the name property is list data type.

It is always good to check the documentations for the data type

ansible-doc ansible.builtin.copy:

- src
  type: path

- dest
  type: path

------------------------------------------------

ansible-doc ansible.builtin.dnf

- name
  type: list

spurs
Flight Engineer
Flight Engineer
  • 184 Views

I didn't know about type, thank you

Join the discussion
You must log in to join this conversation.