ansible/ansible — pull request #1463 by jhoekx (2012-10-27 17:57:59+00:00)
Title: Add a group_by action plugin.

Example usage:

```
---

- hosts: all
gather_facts: false
tasks:
- local_action: is_provisioned host=${inventory_hostname}
- local_action: pause seconds=1
- local_action: group_by is_provisioned, color

- hosts: unprovisioned
gather_facts: false
tasks:
- action: ping

- hosts: blue
gather_facts: false
tasks:
- action: ping
```

With `is_provisioned`:

```
#!/usr/bin/env python

def main():
module = AnsibleModule(
argument_spec = dict(
host=dict(required=True),
)
)

if module.params['host'] in ['firefly']:
ansible_facts={'is_provisioned':'unprovisioned', 'color':'blue'}
module.exit_json(ansible_facts=ansible_facts)
elif module.params['host'] in ['mole']:
ansible_facts={'is_provisioned':'unprovisioned', 'color':'red'}
module.exit_json(ansible_facts=ansible_facts)
else:
module.exit_json(ok="ok")

# this is magic, see lib/ansible/module_common.py
#<>
main()
```

Result:

```
PLAY [all] *********************

TASK: [is_provisioned host=${inventory_hostname}] *********************
ok: [firefly] => {"ansible_facts": {"color": "blue", "is_provisioned": "unprovisioned"}}
ok: [testsys] => {"ok": "ok"}
ok: [mole] => {"ansible_facts": {"color": "red", "is_provisioned": "unprovisioned"}}

TASK: [pause seconds=1] *********************
(^C-c = continue early, ^C-a = abort)
[testsys, firefly, mole]
Pausing for 1 seconds

TASK: [group_by is_provisioned, color] *********************
changed: [testsys] => {"changed": true,
"color": {"blue": ["firefly"], "red": ["mole"]},
"is_provisioned": {"unprovisioned": ["firefly", "mole"]}
}

PLAY [unprovisioned] *********************

TASK: [ping] *********************
ok: [firefly] => {"ping": "pong"}
ok: [mole] => {"ping": "pong"}

PLAY [blue] *********************

TASK: [ping] *********************
ok: [firefly] => {"ping": "pong"}
```

This is branched from 0.8, on trunk not even 'pause' was working.