# ansible needs new magic vars instrument

**URL:** <https://forum.ansible.com/t/ansible-needs-new-magic-vars-instrument/13060>\
**Category:** Ansible Project\
**Tags:** ubuntu\
**Created:** [July 3, 2013, 4:07pm UTC](https://forum.ansible.com/t/ansible-needs-new-magic-vars-instrument/13060 "2013-07-03T16:07:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Drift\_Crow](https://avatars.discourse-cdn.com/v4/letter/d/e19b73/32.png) [@Drift\_Crow](https://forum.ansible.com/u/Drift_Crow)\
**Post date:** [July 3, 2013, 4:07pm UTC](https://forum.ansible.com/t/ansible-needs-new-magic-vars-instrument/13060/1 "2013-07-03T16:07:56Z")

</div>

ansible is very nice ,but when I write a common role for different operation ( mac,centos,ubuntu), I found it is difficult to reuse codes. thou i write code like:

> - action: homebrew name=$item state=present  
> with\_items: …  
> when: ansible\_os\_family == ‘Darwin’
> - action: apt pkg=$item  
> with\_items: …  
> when: ansible\_os\_family == ‘Debian’
> - action: yum …  
> with\_items: …  
> when: ansible\_os\_family == ‘RedHat’
> 
> …

the example maybe too simple, but when we want implement complex condition logic ,like (different version, different architecture ,different register var with and/or combining …),then the code will be all filled with when when when  
so i think it need a new vars instrument . and (imop) as a data structured tools , group\_vars host\_vars is too simple.  
the vars instrument such as:

> [ansible\_os\_familly==“Darwin”]  
> package: “homebrew”  
> state: “present”  
> base\_pkgs:
> 
> - ntp
> - git  
> …  
> [ansible\_os\_familly==“Darwin” && ansible\_os\_familly==“Debian”]

> [ansible\_os\_familly==“Debian” && “ansible\_distribution\_version”== “10.2” && “ansible\_machine”==“x86\_64”]

> [fact\_XX == “XX” && register XX == “XX”]

then write code will turn into write define data,the code above can write with action: ${package} pkg=$item state=${state} with\_item: ${base\_pkgs}  
it is declarative programming? 🙂 the instrument is not too difficult ,but it can implement self describe and complex logic,the code will be reusable

sorry for my poor english.

---

<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:** [July 3, 2013, 5:05pm UTC](https://forum.ansible.com/t/ansible-needs-new-magic-vars-instrument/13060/2 "2013-07-03T17:05:49Z")

</div>

Take a look at the ‘group\_by’ module, which allows you to easily do make up groups where common conditions are true.

I think it does exactly what you want without the repeated ‘when’ things, and makes a lot nicer output.

---

<div class="post-metadata">

**Author:** ![Drift\_Crow](https://avatars.discourse-cdn.com/v4/letter/d/e19b73/32.png) [@Drift\_Crow](https://forum.ansible.com/u/Drift_Crow)\
**Post date:** [July 4, 2013, 1:39am UTC](https://forum.ansible.com/t/ansible-needs-new-magic-vars-instrument/13060/3 "2013-07-04T01:39:27Z")

</div>

thanks for response .i have tried group\_by module,but it seems can’t used in roles and vars, just works for playbook’s task  
maybe you can enahce it ,such as:

vars/main.yml:  
group\_by: $ansible\_os\_family:

- Darwin:  
pack: “XXX”

---

<div class="post-metadata">

**Author:** ![James\_Martin2](https://avatars.discourse-cdn.com/v4/letter/j/b9e5f3/32.png) [@James\_Martin2](https://forum.ansible.com/u/James_Martin2)\
**Post date:** [July 4, 2013, 3:11am UTC](https://forum.ansible.com/t/ansible-needs-new-magic-vars-instrument/13060/4 "2013-07-04T03:11:25Z")

</div>

Take a look at [https://github.com/ansible/ansible-examples/tree/master/riak](https://github.com/ansible/ansible-examples/tree/master/riak).  
I think I'm doing something very similar to what you want. My role  
structure for the riak role is as follows:

riak  
├── common  
│ ├── files  
│ ├── handlers  
│ ├── tasks  
│ ├── templates  
│ └── vars  
├── redhat  
│ ├── handlers  
│ ├── tasks  
│ └── templates  
└── ubuntu  
&nbsp;&nbsp;&nbsp;&nbsp;├── files  
&nbsp;&nbsp;&nbsp;&nbsp;├── tasks  
&nbsp;&nbsp;&nbsp;&nbsp;└── templates  
each subrole has

I have a bootstrap role that calls group\_by module to group the the  
systems by OS type the playbook looks like this:

---

<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:** [July 4, 2013, 4:16am UTC](https://forum.ansible.com/t/ansible-needs-new-magic-vars-instrument/13060/5 "2013-07-04T04:16:58Z")

</div>

It is not true that you can’t use group\_by in roles.

However, group\_by is a task, and not a variable, so you need to read the documentation a bit more 🙂
