How to use copy with with_items and a 'sub list' (not sure if that is the right terminology)

39 views
Skip to first unread message

morgan cox

unread,
May 24, 2022, 3:16:46 PM5/24/22
to Ansible Project
Hi.

Having an issue with a seemingly simple task, I just cannot work this out, pretty sure there is a simple way of doing this ...

I have these example vars for a role
--------
acs_deploy_instances:
 - name: 'test1'
   config_dir: '/opt/www/test1'
   java_version: 'java-1.8.0-openjdk-headless'
   files:
     - 'file1'
     - 'file2'
 - name: 'test2'
   config_dir: '/opt/www/test2'
   java_version: 'java-11-openjdk-headless'
   files:
    - 'file3'
    - 'file4'
--------

I am trying to copy local files inside the configs dir in the server profile  i.e

servers/server1/configs/acs/test1/file1
servers/server1/configs/acs/test1/file2

to the folder specified in acs_deploy_instances.config_dir

I can see the files correctly if I do

----
- debug:
   msg: "{{ item.files }}"
 with_items: "{{ acs_dmz_deploy_instances }}"
----

Output :

TASK [common/acs-dmz-deploy : debug] *******************************************
ok: [localhost] => (item={'name': 'test1', 'config_dir': '/opt/www/test1', 'java_version': 'java-1.8.0-openjdk-headless', 'files': ['file1'
, 'file2']}) => {                                                                                                                           
   "msg": [
       "file1",
       "file2"
   ]
}
ok: [localhost] => (item={'name': 'test2', 'config_dir': '/opt/www/test2', 'java_version': 'java-11-openjdk-headless', 'files': ['file3', '
file4']}) => {                                                                                                                              
   "msg": [
       "file3",
       "file4"
   ]
}

---

The issue is the fact the src files are in a 'sub list' - i.e file1/file2  so when i try to do this task to copy these files it will not work as the output contains multiple values (and square brackets)

Example task :

----
- name: Copy config files
 copy:
   src="../../../../configs/acs/{{ item.name }}/{{ item.files }}"
   dest="{{ item.config_dir }}/"
   owner="root"
   group="root"
   mode="0644"
 with_items: "{{ acs_dmz_deploy_instances }}"
----

I get the error

"msg": "Could not find or access '../../../../configs/acs/test1/['file1', 'file
2']'\nSearched in:\n\t/etc/ansible/roles/common/acs-dmz-deploy/files/../../../../configs/acs/test1/['file1', 'file2']\n\t/etc/ansible/roles
/common/acs-dmz-deploy/../../../../configs/acs/test1/['file1', 'file2']\n\t/etc/ansible/roles/common/acs-dmz-deploy/tasks/files/../../../..
/configs/acs/test1/['file1', 'file2']\n\t/etc/ansible/roles/common/acs-dmz-deploy/tasks/../../../../configs/acs/test1/['file1', 'file2']\n\
t/etc/ansible/files/../../../../configs/acs/test1/['file1', 'file2']\n\t/etc/ansible/../../../../configs/acs/test1/['file1', 'file2'] on th
e Ansible Controller.\nIf you are using a module and expect the file to exist on the remote, see the remote_src option"}                    
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: If you are using a module and expect the f
ile to exist on the remote, see the remote_src option


Can anyone suggest a way to copy the files in the 'sub list' to the 'config_dir' var ?

I've tried using loops, etc but nothing so far is working

morgan cox

unread,
May 25, 2022, 7:47:42 AM5/25/22
to Ansible Project
I worked it out ...

vars :-

-------
# Instances
acs_dmz_deploy_instances:
 - name: 'test1'
   config_dir: '/opt/www/test1'
   java_version: 'java-1.8.0-openjdk-headless'
   user: "tomcat"
   group: "tomcat"
   mode: "0644"
   acs_dmz_deploy_config_file_copy_only: true
   files:
     - { src: 'file1', dest: '/opt/www/test1'}
     - { src: 'file2', dest: '/opt/www/test1'}
 - name: 'test2'
   config_dir: '/opt/www/test2'
   java_version: 'java-11-openjdk-headless'
   user: tomcat9
   group: tomcat9
   mode: "0644"
   acs_dmz_deploy_config_file_copy_only: false
   files:
     - { src: 'file3', dest: '/opt/www/test2'}
-------

copy task :

------
- name: Copy config files
 copy:
   src="../../../../configs/acs-dmz/{{ item.0.name }}/{{ item.1.src }}"
   dest="{{ item.1.dest }}/"
   owner="{{ item.0.user }}"
   group="{{ item.0.group }}"
   mode="{{ item.0.mode }}"
 loop: "{{ acs_dmz_deploy_instances | subelements('files') }}"
 when: "item.0.acs_dmz_deploy_config_file_copy_only  == true"
------
Reply all
Reply to author
Forward
0 new messages