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

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.

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

Playbook:

---
- 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

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:

[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ł:

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ł: