# what would be the best way to set variable based on group

**URL:** <https://forum.ansible.com/t/what-would-be-the-best-way-to-set-variable-based-on-group/15648>\
**Category:** Ansible Project\
**Created:** [July 10, 2014, 10:39pm UTC](https://forum.ansible.com/t/what-would-be-the-best-way-to-set-variable-based-on-group/15648 "2014-07-10T22:39:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Steve\_Lee](https://avatars.discourse-cdn.com/v4/letter/s/e56c9b/32.png) [@Steve\_Lee](https://forum.ansible.com/u/Steve_Lee)\
**Post date:** [July 10, 2014, 10:39pm UTC](https://forum.ansible.com/t/what-would-be-the-best-way-to-set-variable-based-on-group/15648/1 "2014-07-10T22:39:20Z")

</div>

```auto
Is it possible to have condition statement on variable? What would be the best way to assign Variable based on inventory group? Below is the basic idea. 

```

```auto
vars:
  epic: true
  when: inventory_hostname == groups.database or inventory_hostname == groups.apache
  
vars:
   epic: false
   when: inventory_hostname != groups.database or inventory_hostname != groups.apache

```

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [July 11, 2014, 6:57pm UTC](https://forum.ansible.com/t/what-would-be-the-best-way-to-set-variable-based-on-group/15648/2 "2014-07-11T18:57:06Z")

</div>

Put them in a separate file and use the include\_vars module:

[http://docs.ansible.com/include\_vars\_module.html](http://docs.ansible.com/include_vars_module.html)

You can use when: statements with that.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [July 11, 2014, 10:33pm UTC](https://forum.ansible.com/t/what-would-be-the-best-way-to-set-variable-based-on-group/15648/3 "2014-07-11T22:33:19Z")

</div>

group\_vars/ directories are the best way to do this in many cases.

Create a parent group that contains ‘database’ and ‘apache’ and apply the variables there.
