How to share files between roles
We use AAP to deploy roles. The roles are in Git. I now have 2 roles that need to deploy the same files and templates, and of course I don't want to keep 2 versions in Git. How could I solve that?
We use AAP to deploy roles. The roles are in Git. I now have 2 roles that need to deploy the same files and templates, and of course I don't want to keep 2 versions in Git. How could I solve that?
vegetaaaaaaa, Symlinks are the simplest solution (just checked, it works)
<span style="color:#323232;">$ tree test-role* </span><span style="color:#323232;">test-role1 </span><span style="color:#323232;">├── tasks </span><span style="color:#323232;">│ └── main.yml </span><span style="color:#323232;">└── templates </span><span style="color:#323232;"> └── template1.j2 </span><span style="color:#323232;">test-role2 </span><span style="color:#323232;">├── tasks </span><span style="color:#323232;">│ └── main.yml </span><span style="color:#323232;">└── templates </span><span style="color:#323232;"> └── template1.j2 -> ../../test-role1/templates/template1.j2 </span><span style="color:#323232;"> </span><span style="color:#323232;">$ cat test-role1/tasks/main.yml </span><span style="color:#323232;">- name: copy template </span><span style="color:#323232;"> tags: test-role1 </span><span style="color:#323232;"> become: yes </span><span style="color:#323232;"> template: </span><span style="color:#323232;"> src: template1.j2 </span><span style="color:#323232;"> dest: /root/template1 </span><span style="color:#323232;"> </span><span style="color:#323232;">$ cat test-role2/tasks/main.yml </span><span style="color:#323232;">- name: copy template </span><span style="color:#323232;"> tags: test-role2 </span><span style="color:#323232;"> become: yes </span><span style="color:#323232;"> template: </span><span style="color:#323232;"> src: template1.j2 </span><span style="color:#323232;"> dest: /root/template1 </span><span style="color:#323232;"> </span><span style="color:#323232;">$ ansible-playbook playbook.yml --diff --check --tags test-role2 </span><span style="color:#323232;">... </span><span style="color:#323232;">TASK [test-role2 : copy template] ***************** </span><span style="color:#323232;">--- before </span><span style="color:#323232;">+++ after: /home/deploy/.ansible/tmp/ansible-local-143602q8imzfz/tmp8g6dn_h1/template1.j2 </span><span style="color:#323232;">@@ -0,0 +1 @@ </span><span style="color:#323232;">+this is a template </span><span style="color:#323232;"> </span>
melvinkitnick, As an alternative to what @mea_rah proposed, you can also push your files in a git repository that you can clone/pull on your remote hosts (or locally, then copied / templated out, if needed) from a separate task, which can be in or out (via include_tasks or import_tasks modules) of your roles. Or if all target hosts have access to a shared storage, you can directly copy files from there, depending on your needs.
I still think having your files and deploy task on a separate role used as a dependency for your other roles is the way to go, though I’d need more context to give you a more appropriate answer.
mea_rah, Is it possible that the files and templates could (should?) be part of separate role that you perhaps pull in as dependency?
The other pattern I saw out there was that the file/template wasn’t part of the roles, but was instead provided by playbook that is using the roles. So file was in repository with playbook and template/file path was passed to role as parameter.
Add comment