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 }}"
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
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 }}"
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?
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
I didn't know about type, thank you
Red Hat
Learning Community
A collaborative learning environment, enabling open source skill development.