Optional shared files between roles

Ansible Best Practices described that each role contains a file directory that contains all the files required by this rule.

In my case, I have different roles that use the same files. But I cannot make a copy of these files in each role, since there will not be a single source of these files, and if editing happens with one of them, it will become tedious to make this change for each role.

The solution I made was to create another folder and link to it using an absolute or relative path. Is this the best way to do this?

My familiar directory is as follows

play.yml roles/ web/ tasks/ files/ common-1 common-2 other-multiple-files role-2/ tasks/ files/ common-1 common-2 other-multiple-files role-3/ tasks/ files/ common-2 role-4/ tasks/ files/ common-1 
+15
source share
5 answers

You have two reasonable approaches that you can try here to reduce recurrence.

You can have a separate shared-files directory, which will be located as a sibling in your role folders as follows:

 play.yml roles/ web/ tasks/ files/ other-multiple-files role-2/ tasks/ files/ other-multiple-files role-3/ tasks/ role-4/ tasks/ shared-files/ common-1 common-2 

Then you refer to this in tasks with the relative location of the file from which the role / files folder will be:

 - name: copy common-1 copy: src: ../../common-1 dest: /path/to/dest/common-1 - name: copy role specific file src: other-multiple-files dest: /path/to/dest/other-multiple-files 

Or alternatively, to use a relative folder path, you could symbolize things like this:

 play.yml roles/ web/ tasks/ files/ common-1 -> ../../common-1 common-2 -> ../../common-2 other-multiple-files role-2/ tasks/ files/ common-1 -> ../../common-1 common-2 -> ../../common-2 other-multiple-files role-3/ tasks/ files/ common-2 -> ../../common-2 role-4/ tasks/ files/ common-1 -> ../../common-1 shared-files/ common-1 common-2 

And you can refer to the file as if it were in the role / files directory directly:

 - name: copy common-1 copy: src: common-1 dest: /path/to/dest/common-1 - name: copy role specific file src: other-multiple-files dest: /path/to/dest/other-multiple-files 
+21
source

My solution was to create roles for common material and add them as dependencies.

For example, your game would look like this:

 play.yml roles/ common-1/ files/ common-1 common-2/ files/ common-2 web/ meta/ common-1 common-2 tasks/ files/ other-multiple-files role-2/ meta/ common-1 common-2 tasks/ files/ other-multiple-files role-3/ meta/ common-2 tasks/ role-4/ meta/ common-1 tasks/ 

therefore, roles/common-1 and roles/common-2 are roles that simply deploy the files, and all the roles that need them, they have a dependency in the meta/ folder.

+12
source

I use some of the given variables like TG40 and place the files along this path. So, I define a variable

 common_files_path: "{{ inventory_dir }}/resources/shared_files" 

... and then use it in the assignment

 - name: get local files command: cat {{ item }} register: some_files with_fileglob: "{{ common_files_path }}/*" delegate_to: localhost 
+2
source

Ansible actually supports project shared folders. No need to create custom folders. See the answer in this other question too. Answer: global template folder?

The tasks/ folder at the root level of ansible/ also accepted.

By the way, I could not find any official documentation on this kind of directory structure. https://docs.ansible.com/ansible/latest/user_guide/playbooks_best_practices.html#directory-layout

+1
source

Short answer (there should be additional points for short, concise answers) o question:

No

Better use git submodules . From:

http://random-notes-chris.blogspot.co.uk/2014/02/git-submodules-for-ansible-roles.html

 Every role lives in its own git repo. An example is Vincent Rischmann ansible-role-java. Every app (or project) has its own git repo for its Ansible configuration (inventory definition and playbooks etc.). The app repo has a roles sub directory. Actual roles (eg the java role) are then added as git submodules. git submodule add https://github.com/vrischmann/ansible-role-java.git roles/java 
-2
source

Source: https://habr.com/ru/post/1238248/


All Articles