<p>Another approach that I found uses the <code>include_role</code> directive. So instead of the playbook being: </p>

<pre><code>---
- hosts: M2
  - roles:
    - role: apache2
</code></pre>

<p>It becomes:</p>

<pre><code>---
- hosts: M2
  - tasks:
    - include_role: apache2
</code></pre>

<p>Then I can add tasks directly after that apache2 task that configure apache2:</p>

<pre><code>---
- hosts: M2
  - tasks:
    - include_role: apache2

    - copy:
        # etc
</code></pre>

CC BY-SA 4.0
2019-04-09T15:27:00.707Z
2019-04-09T15:27:00.707Z
Ryan O.
<p>I have a role, let's call it apache2. The role installs apache2 and all of its dependencies.</p>

<p>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:</p>

<pre><code>---
- hosts: M1
    - role: apache2
</code></pre>

<p>M2.yml looks like:</p>

<pre><code>---
- hosts: M2
    - role: apache2
</code></pre>

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

<ol>
<li>Create a <code>post_tasks</code> section for each playbook that copies over the necessary files and configures the server.</li>
<li>Create a <code>copy_files</code> role that takes the files as a variable</li>
</ol>

<p>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.</p>

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

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

<ansible>
Ansible: Copying different files to different guests for use with the same role
Ryan O.
# Ansible: Copying different files to different guests for use with the same role

(Asked by: username_0 on 2019-04-09)

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:

Create a post_tasks section for each playbook that copies over the necessary files and configures the server.
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?

**Tags:** ansible


---

## 1 answers

### Answer by username_0 on 2019-04-09. Score: 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


---

