# Access role file different than main.yml

**URL:** <https://forum.ansible.com/t/access-role-file-different-than-main-yml/14775>\
**Category:** Ansible Project\
**Created:** [March 14, 2014, 5:25pm UTC](https://forum.ansible.com/t/access-role-file-different-than-main-yml/14775 "2014-03-14T17:25:27Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![James\_Goodhouse](https://avatars.discourse-cdn.com/v4/letter/j/54ee81/32.png) [@James\_Goodhouse](https://forum.ansible.com/u/James_Goodhouse)\
**Post date:** [March 14, 2014, 5:25pm UTC](https://forum.ansible.com/t/access-role-file-different-than-main-yml/14775/1 "2014-03-14T17:25:27Z")

</div>

When specifying a role to use in a playbook, is it possible to target a different file other than main.yml? I ask because I’m struggling to find the proper way to layout a playbook and role for my project.

Currently I have roles created for common tasks like nginx, mysql, varnish, etc. I also have a role created specifically for my project. Inside that role I handle things like installing project specific things like APC, memcache, etc. These are each handled by a separate file that is included in the main.yml. I also have a file for deployment that is also included in the main.yml. However, I don’t really want to have code deploy each time I provision a server.

My thought was to create a deploy.yml file in the root and somehow include the role for the project, but just the deploy portion.

How are people handling this with their projects?

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [March 14, 2014, 5:33pm UTC](https://forum.ansible.com/t/access-role-file-different-than-main-yml/14775/2 "2014-03-14T17:33:51Z")

</div>

So the task file roles/foo/tasks/main.yml is always loaded, however this pattern is common:

main.yml:

---

<div class="post-metadata">

**Author:** ![James\_Goodhouse](https://avatars.discourse-cdn.com/v4/letter/j/54ee81/32.png) [@James\_Goodhouse](https://forum.ansible.com/u/James_Goodhouse)\
**Post date:** [March 14, 2014, 7:30pm UTC](https://forum.ansible.com/t/access-role-file-different-than-main-yml/14775/3 "2014-03-14T19:30:44Z")

</div>

I’m aware that I can do that, and have been thinking I might try to implement some sort of variable to determine if I include my deploy.yml file. This solution would work in that code wouldn’t deploy unless I explicitly set that variable via the command line. The problem with that is that code deployment would be accompanied by all of the other tasks for provisioning the server, which is no bueno.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [March 14, 2014, 7:33pm UTC](https://forum.ansible.com/t/access-role-file-different-than-main-yml/14775/4 "2014-03-14T19:33:48Z")

</div>

“The problem with that is that code deployment would be accompanied by all of the other tasks for provisioning the server, which is no bueno.”

I don’t understand this part, can you elaborate?

---

<div class="post-metadata">

**Author:** ![James\_Goodhouse](https://avatars.discourse-cdn.com/v4/letter/j/54ee81/32.png) [@James\_Goodhouse](https://forum.ansible.com/u/James_Goodhouse)\
**Post date:** [March 14, 2014, 8:34pm UTC](https://forum.ansible.com/t/access-role-file-different-than-main-yml/14775/5 "2014-03-14T20:34:55Z")

</div>

I’d like to be able to run a role and, by default, exclude the code deployment portion. I’d also like to be able to deploy the code without running any other tasks.

I could include a line in the main.yml file for my project role that would only include the deploy.yml file when the deploy\_code variable is true. Something similar to below:

- include: deploy.yml  
when: “deploy\_code”

To deploy code, I would run a command like the following:

ansible-playbook site.yml --extra-vars “deploy\_code=true”

The problem with this is that my site.yml file has several other entries besides just my project role. The same is true for my project role’s main.yml – it contains other includes besides the deploy.yml. So if run the above command, yes my code will deploy, but every other task will also run, which I don’t want to have happen.

Does that make things clearer at all?

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [March 14, 2014, 9:23pm UTC](https://forum.ansible.com/t/access-role-file-different-than-main-yml/14775/6 "2014-03-14T21:23:16Z")

</div>

you want conditional includes?

instead of the current case in which includes apply their conditions to all  
included tasks.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [March 14, 2014, 9:34pm UTC](https://forum.ansible.com/t/access-role-file-different-than-main-yml/14775/7 "2014-03-14T21:34:22Z")

</div>

If you want this, use group\_by to create a dynamic group and then apply roles to the dynamic group.

---

<div class="post-metadata">

**Author:** ![James\_Goodhouse](https://avatars.discourse-cdn.com/v4/letter/j/54ee81/32.png) [@James\_Goodhouse](https://forum.ansible.com/u/James_Goodhouse)\
**Post date:** [March 17, 2014, 7:50pm UTC](https://forum.ansible.com/t/access-role-file-different-than-main-yml/14775/8 "2014-03-17T19:50:37Z")

</div>

I read a bit about this but am no 100% sure the direction to go. If possible could you give an example how this would work for deployment?

---

<div class="post-metadata">

**Author:** ![mhael](https://avatars.discourse-cdn.com/v4/letter/m/919ad9/32.png) [@mhael](https://forum.ansible.com/u/mhael)\
**Post date:** [March 18, 2014, 12:34am UTC](https://forum.ansible.com/t/access-role-file-different-than-main-yml/14775/9 "2014-03-18T00:34:10Z")

</div>

Hi James,

You may consider to use tags with include: [http://docs.ansible.com/playbooks\_tags.html](http://docs.ansible.com/playbooks_tags.html)

```auto
- include: deploy.yml tags=deployment

```

Then, to perform deployment only:

```auto
ansible-playbook example.yml --tags "deployment"

```

And to perform everything else except deployment:

```auto
ansible-playbook example.yml --skip-tags "deployment"

The downside is you have to explicitly disable deployment tag for default run. Probably not much of an issue if used in script.

```

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [March 21, 2014, 1:31pm UTC](https://forum.ansible.com/t/access-role-file-different-than-main-yml/14775/10 "2014-03-21T13:31:53Z")

</div>

Consider splitting your site.yml up into smaller files all included from site.yml, and just using those, or usings tags.
