# Delegate\_to, tags, with\_items | Good Practise while running a playbook against a group of hosts, where some tasks are for a specialized subset of hosts only

**URL:** <https://forum.ansible.com/t/delegate-to-tags-with-items-good-practise-while-running-a-playbook-against-a-group-of-hosts-where-some-tasks-are-for-a-specialized-subset-of-hosts-only/17593>\
**Category:** Ansible Project\
**Created:** [February 22, 2015, 4:24pm UTC](https://forum.ansible.com/t/delegate-to-tags-with-items-good-practise-while-running-a-playbook-against-a-group-of-hosts-where-some-tasks-are-for-a-specialized-subset-of-hosts-only/17593 "2015-02-22T16:24:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jakub\_Muszynski](https://avatars.discourse-cdn.com/v4/letter/j/c68b51/32.png) [@Jakub\_Muszynski](https://forum.ansible.com/u/Jakub_Muszynski)\
**Post date:** [February 22, 2015, 4:24pm UTC](https://forum.ansible.com/t/delegate-to-tags-with-items-good-practise-while-running-a-playbook-against-a-group-of-hosts-where-some-tasks-are-for-a-specialized-subset-of-hosts-only/17593/1 "2015-02-22T16:24:49Z")

</div>

**This is a question of good practise:**

I do have logic “Lines” on a way from my LB to Backend

> [LineA]  
> proxyA app\_type=proxy  
> frontendA app\_type=frontend  
> middlewareA app\_type=middleware

> [LineB]  
> proxyB app\_type=proxy  
> …

Since the apps are more complex than simple apache start stop. i did write custom modules with required logic to controll them:

> proxysrv: with actions [suspend, activate]  
> restart: with actions [start, stop, restart], and app\_type arguments [frontend, middleware]

The tasks have to be played in certain order (suspend proxy, restart frontends and middleware, activate proxy)  
Now I can have a play like:

> * * *
> 
> - hosts: LineA
> - tasks:
> - name:  
> proxysrv: action=suspend, when app\_type ==‘proxy’
> - name:  
> restart: action=restart, app\_type=frontend, when app\_type ==‘frontend’
> - name:  
> restart: action=restart, app\_type=middleware, when app\_type ==‘middleware’
> - name:  
> proxysrv: action=activate, when app\_type ==‘proxy’

I don’t like the “skipped” message during the run of a playbook, and the numbers in the final play statistics, that count the skipped hosts (as not changed).

**This is a question of good practise:**  
How would You create dependencies, that would apply specialized tasks on a subset of Your hosts group.

I guess that better way to solve the problem would be the use of **delegate\_to** (probably with “ **with\_items** ” or “ **tags** ”).  
I could register the ‘proxy\_hosts’, ‘frontend\_hosts’, ‘middleware\_hosts’ variables during an execution of the play and run tasks against them.  
I do not see yet use of tags here, the example would be nice.  
Any better idea?

What about roles. I would like to have a roles (proxy, restart role) and run them against subset of a hosts.

---

<div class="post-metadata">

**Author:** ![Jakub\_Muszynski](https://avatars.discourse-cdn.com/v4/letter/j/c68b51/32.png) [@Jakub\_Muszynski](https://forum.ansible.com/u/Jakub_Muszynski)\
**Post date:** [February 26, 2015, 12:05pm UTC](https://forum.ansible.com/t/delegate-to-tags-with-items-good-practise-while-running-a-playbook-against-a-group-of-hosts-where-some-tasks-are-for-a-specialized-subset-of-hosts-only/17593/2 "2015-02-26T12:05:35Z")

</div>

Up to now I think the best would be generate “temporary” host list (group\_by).  
You may intersect them later with other groups

However I do not like the idea of defining “hosts: xxxx” more than once in a playbook.

I did post the idea first in the topic:  
How to list all currently targeted hosts in an Ansible play

[http://stackoverflow.com/questions/28709501/how-to-list-all-currently-targeted-hosts-in-an-ansible-play](http://stackoverflow.com/questions/28709501/how-to-list-all-currently-targeted-hosts-in-an-ansible-play)

Playbook:

```auto
---
- hosts: all

  tasks:
    - name: Create a group of all hosts by app_type
      group_by: key={{app_type}}

    - debug: msg="groups={{groups}}"
      run_once: true

- hosts: web:&some_other_group

  tasks:
   - debug: msg="play_hosts={{play_hosts}}"
     run_once: true

```

would result in

```auto
TASK: [Create a group of all hosts by app_type] *******************************
changed: [web1] => {"changed": true, "groups": {"web": ["web1", "web2"], "load_balancer": ["web3"]}}

TASK: [debug msg="play_hosts={{play_hosts}}"] *********************************
ok: [web1] => {
    "msg": "play_hosts=['web1']"
}

```

inventory:

```auto
[proxy]
web1 app_type=web
web2 app_type=web
web3 app_type=load_balancer

[some_other_group]
web1
web3

```

W dniu niedziela, 22 lutego 2015 17:24:49 UTC+1 użytkownik sirkubax napisał:

---

<div class="post-metadata">

**Author:** ![Jakub\_Muszynski](https://avatars.discourse-cdn.com/v4/letter/j/c68b51/32.png) [@Jakub\_Muszynski](https://forum.ansible.com/u/Jakub_Muszynski)\
**Post date:** [February 26, 2015, 12:09pm UTC](https://forum.ansible.com/t/delegate-to-tags-with-items-good-practise-while-running-a-playbook-against-a-group-of-hosts-where-some-tasks-are-for-a-specialized-subset-of-hosts-only/17593/3 "2015-02-26T12:09:18Z")

</div>

Just to compare execution and inventory arragment:  
I’m trying to get the same result based on tags (run task on limited hosts-set from current hosts-group)

W dniu czwartek, 26 lutego 2015 13:05:35 UTC+1 użytkownik sirkubax napisał:
