Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible shared files between roles

Tags:

ansible

Ansible Best Practices described that every role contains file directory that have all files needed by this rule.

In my case I have different roles that share the same files. But I cannot make a copy of these files in each role as there will be no one source of these files and if edit happens to one of them it will become tedious to make this change for every role.

A solution I made is to create another folder and reference it using absolute or relative path. Is this the best way of doing it?

My ansible directory look like this

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
like image 238
Nasr Avatar asked Dec 15 '15 10:12

Nasr


2 Answers

You've got two reasonably decent approaches you can try here to reduce repetition.

You could have a separate shared-files directory that sits as a sibling to your role folders like this:

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

You would then reference this in the tasks with a relative file location from where the role/files folder would 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 path to the folder, you could symlink things across 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 then reference the file as if it was 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
like image 125
ydaetskcoR Avatar answered Nov 09 '22 14:11

ydaetskcoR


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

For example, your playbook 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/

so, roles/common-1 and roles/common-2 are roles that just deploy the files, and all the roles that need those, they have it as a dependency in the meta/ folder.

like image 17
user2599522 Avatar answered Nov 09 '22 12:11

user2599522