# Advice on structuring playbooks and tasks

**URL:** <https://forum.ansible.com/t/advice-on-structuring-playbooks-and-tasks/12835>\
**Category:** Ansible Project\
**Created:** [May 20, 2013, 5:25pm UTC](https://forum.ansible.com/t/advice-on-structuring-playbooks-and-tasks/12835 "2013-05-20T17:25:35Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://forum.ansible.com/u/Brian_Candler)\
**Post date:** [May 20, 2013, 5:25pm UTC](https://forum.ansible.com/t/advice-on-structuring-playbooks-and-tasks/12835/1 "2013-05-20T17:25:35Z")

</div>

I am attempting to structure playbooks for reusability. Options seem to be:

- playbooks can include other playbooks
- playbooks can include tasks and handlers
- playbooks can include tasks and handlers via ‘roles’

Since every playbook has to be bound to specific hosts, I’m looking at the other two options.

To start with I’ve factored out tasks and handlers. There can be a lot of actions in one related set of tasks. Here’s an example of a tasks file:

# cat tasks/snmp/centos.yaml

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [May 20, 2013, 6:26pm UTC](https://forum.ansible.com/t/advice-on-structuring-playbooks-and-tasks/12835/2 "2013-05-20T18:26:27Z")

</div>

You should definitely consider using roles in 1.2 and take a look at some organization examples in things like:

[https://github.com/ansible/ansible-examples/blob/master/lamp\_haproxy/site.yml](https://github.com/ansible/ansible-examples/blob/master/lamp_haproxy/site.yml)

If you want to only runs parts of a playbook, yes, you have tags to do that. Though you seem to be more interested in the --interactive flag with --start-at-task for playbook control, so I recommend looking at those, but I like tags a lot.

We are not going to create something halfway between a playbook and a task file as that would be confusing, but hopefully between those two options you can get something you want.

Yes, tasks can reference other tasks, for instance in main.yml:

---

<div class="post-metadata">

**Author:** ![Matt\_Coddington](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Matt\_Coddington](https://forum.ansible.com/u/Matt_Coddington)\
**Post date:** [May 20, 2013, 6:39pm UTC](https://forum.ansible.com/t/advice-on-structuring-playbooks-and-tasks/12835/3 "2013-05-20T18:39:16Z")

</div>

Tagging pretty much everything is tedious, but it also gives you really  
fine-grained control over what you want to execute (if you need it, which  
unfortunately i do). And it's just a one-time effort... I tag almost  
everything and find myself using "--limit=\<hosts\> --tags=\<tags\>" for quick  
iteration and testing of new stuff all the time.

That said, you \_can\_ do exactly what you want above... I have includes like  
this in my playbooks and they work just fine:

- include: tasks/snmp/centos.yaml tags=snmp

Basically that implicitly adds the tag "snmp" to each item in centos.yaml.

matt

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [May 20, 2013, 6:43pm UTC](https://forum.ansible.com/t/advice-on-structuring-playbooks-and-tasks/12835/4 "2013-05-20T18:43:02Z")

</div>

Yep, this works on roles too!

roles:

- { role: foo, tags: [‘abc’, ‘def’], some\_variable: 12, some\_other\_variable: 3000 }

---

<div class="post-metadata">

**Author:** ![David\_Birdsong](https://avatars.discourse-cdn.com/v4/letter/d/bb73d2/32.png) [@David\_Birdsong](https://forum.ansible.com/u/David_Birdsong)\
**Post date:** [May 21, 2013, 5:23am UTC](https://forum.ansible.com/t/advice-on-structuring-playbooks-and-tasks/12835/5 "2013-05-21T05:23:42Z")

</div>

i’m having trouble getting this syntax for restricting tasks inside of roles to a specified tag.

i have in a playbook:

roles:

- { role: base, tags: [‘users’]}

and then base’s main.yml

[david@ops-1 provision{master}]$ cat roles/base/tasks/main.yml

---

<div class="post-metadata">

**Author:** ![GW1](https://avatars.discourse-cdn.com/v4/letter/g/c89c15/32.png) [@GW1](https://forum.ansible.com/u/GW1)\
**Post date:** [May 21, 2013, 7:10am UTC](https://forum.ansible.com/t/advice-on-structuring-playbooks-and-tasks/12835/6 "2013-05-21T07:10:46Z")

</div>

Hi,

Afaik the tags parameter for include statements and roles is only for tagging (you tag each and every task inside it with this tag), not for filtering (what seems you want to do). Filtering is done by specifying --tags in the command line.

Although it is weird that with --tags “users” base\_packages.yml gets skipped over. Shouldn’t it be also tagged with “users” because of the role statement?

Anyway, my approach is to use roles without tags and than the main.yml of each role contains only include statements tagged with all variations, eg: “tags=basic.ntp,base,ntp” (.,,) (the first tag contains a dot and simulates the missing “and” operation when you want to filter only the tasks that contain both tags). This way you can filter with --tags=basic for everything in this role, filter exact include statements inside roles with --tags=basic.ntp or just filter by all NTP related things through all roles --tags=ntp.

Greetings,  
gw

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [May 21, 2013, 12:49pm UTC](https://forum.ansible.com/t/advice-on-structuring-playbooks-and-tasks/12835/7 "2013-05-21T12:49:20Z")

</div>

> Although it is weird that with --tags "users" base\_packages.yml gets  
> skipped over. Shouldn't it be also tagged with "users" because of the role  
> statement?

Tags were specified later so they were overwritten.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [May 21, 2013, 12:49pm UTC](https://forum.ansible.com/t/advice-on-structuring-playbooks-and-tasks/12835/8 "2013-05-21T12:49:59Z")

</div>

GW is correct below, you must specify the tasks you want to run on the CLI.

Tags in the playbook are for applying tags, not restricting them, as that wouldn’t make sense.

---

<div class="post-metadata">

**Author:** ![David\_Birdsong](https://avatars.discourse-cdn.com/v4/letter/d/bb73d2/32.png) [@David\_Birdsong](https://forum.ansible.com/u/David_Birdsong)\
**Post date:** [May 21, 2013, 3:56pm UTC](https://forum.ansible.com/t/advice-on-structuring-playbooks-and-tasks/12835/9 "2013-05-21T15:56:10Z")

</div>

> GW is correct below, you must specify the tasks you want to run on the CLI.
> 
> Tags in the playbook are for applying tags, not restricting them, as that wouldn’t make sense.

Thanks all, that makes sense.

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://forum.ansible.com/u/Brian_Candler)\
**Post date:** [May 21, 2013, 7:49pm UTC](https://forum.ansible.com/t/advice-on-structuring-playbooks-and-tasks/12835/10 "2013-05-21T19:49:03Z")

</div>

Thank you everyone for the replies. I’ll try refactoring into roles and applying tags at the role level or at the include: tasks level.

ansible-playbook --step and --start-at-task are both new to me and also look useful. I don’t have them in ansible 1.0 so maybe this is an excuse to upgrade to 1.2 🙂

Regards,

Brian.
