1

I have a role, let's call it apache2. The role installs apache2 and all of its dependencies.

Now I have two machines, let's call them M1 and M2. Both M1 and M2 need to have apache2 installed. However, each of them needs their own set of files installed into the web root. So M1.yml looks like:

---
- hosts: M1
    - role: apache2

M2.yml looks like:

---
- hosts: M2
    - role: apache2

Obviously I can't just put the files into the role's files directory, as they are different for each. Options of which I am aware:

  1. Create a post_tasks section for each playbook that copies over the necessary files and configures the server.
  2. Create a copy_files role that takes the files as a variable

The reason I don't like #1 is because I might have a bunch of similar roles (apache2, nginx, ftp, etc.) each of which requires installation and files to be copied over. I'd like to keep each "together", meaning that the "install apache2" and "copy apache2 files to guest" tasks are both specified near each other in the file.

The reason I don't like #2 is because it seems heavyweight to basically just wrap a task as a role.

Is there another option I'm not thinking of? And of the available options, which is considered best practice?

1 Answer 1

1

Another approach that I found uses the include_role directive. So instead of the playbook being:

---
- hosts: M2
  - roles:
    - role: apache2

It becomes:

---
- hosts: M2
  - tasks:
    - include_role: apache2

Then I can add tasks directly after that apache2 task that configure apache2:

---
- hosts: M2
  - tasks:
    - include_role: apache2

    - copy:
        # etc

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.