# Need advise on define inventory-specific variables

**URL:** <https://forum.ansible.com/t/need-advise-on-define-inventory-specific-variables/15616>\
**Category:** Ansible Project\
**Created:** [July 7, 2014, 4:11am UTC](https://forum.ansible.com/t/need-advise-on-define-inventory-specific-variables/15616 "2014-07-07T04:11:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chengbing\_Liu](https://avatars.discourse-cdn.com/v4/letter/c/b2d939/32.png) [@Chengbing\_Liu](https://forum.ansible.com/u/Chengbing_Liu)\
**Post date:** [July 7, 2014, 4:11am UTC](https://forum.ansible.com/t/need-advise-on-define-inventory-specific-variables/15616/1 "2014-07-07T04:11:29Z")

</div>

I’m using Ansible to deploy and maintain Hadoop clusters.

I used to define variables in group\_vars/all as shown below:

> hadoop:
> 
> # Variables for core-site.xml
> 
> nameservice: hadoop-namenode
> 
> # Variables for hdfs-site.xml
> 
> dfs\_blocksize: 256m
> 
> # Variables for mapred-site.xml
> 
> mapred\_job\_tracker: hadoop-jobtracker:8021

However, I have different inventory files, which makes it prone to mistakes.

I can define variables using [all:vars] in inventory files:

> [all:vars]  
> hadoop\_nameservice=hadoop-namenode

How can I define inventory-specific variables, while still preserving the structured definition as in group\_vars/all?

Thanks a lot!

---

<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 7, 2014, 7:29pm UTC](https://forum.ansible.com/t/need-advise-on-define-inventory-specific-variables/15616/2 "2014-07-07T19:29:08Z")

</div>

Perhaps I’m not understanding the question, but:

group\_vars/all  
group\_vars/groupname1

group\_vars/groupname2

etc

This can be used with multiple inventory files if group\_vars is a directory in the directory with both of them, which should be what you need.

Let us know if this doesn’t make sense and perhaps I can elaborate further.

Thanks!

---

<div class="post-metadata">

**Author:** ![Chengbing\_Liu](https://avatars.discourse-cdn.com/v4/letter/c/b2d939/32.png) [@Chengbing\_Liu](https://forum.ansible.com/u/Chengbing_Liu)\
**Post date:** [July 8, 2014, 9:41am UTC](https://forum.ansible.com/t/need-advise-on-define-inventory-specific-variables/15616/3 "2014-07-08T09:41:05Z")

</div>

Sorry, the question was not stated clearly.

I have different inventory files corresponding to different clusters, e.g.:

hosts\_cluster1

> [NameNode]

> host1

hosts\_cluster2

> [NameNode]

> host2

They have the same group names, therefore I decide to move the variable definitions from group\_vars to the all:vars section of inventory file.  
However, I cannot define structured variables like before in group\_vars.

Any advice?

在 2014年7月8日星期二UTC+8上午3时29分14秒，Michael DeHaan写道：

> Perhaps I’m not understanding the question, but:
> 
> group\_vars/all  
> group\_vars/groupname1
> 
> group\_vars/groupname2
> 
> etc
> 
> This can be used with multiple inventory files if group\_vars is a directory in the directory with both of them, which should be what you need.
> 
> Let us know if this doesn’t make sense and perhaps I can elaborate further.
> 
> Thanks!
> 
> > I’m using Ansible to deploy and maintain Hadoop clusters.
> > 
> > I used to define variables in group\_vars/all as shown below:
> > 
> > > hadoop:
> > > 
> > > # Variables for core-site.xml
> > > 
> > > nameservice: hadoop-namenode
> > > 
> > > # Variables for hdfs-site.xml
> > > 
> > > dfs\_blocksize: 256m
> > > 
> > > # Variables for mapred-site.xml
> > > 
> > > mapred\_job\_tracker: hadoop-jobtracker:8021
> > 
> > However, I have different inventory files, which makes it prone to mistakes.
> > 
> > I can define variables using [all:vars] in inventory files:
> > 
> > > [all:vars]  
> > > hadoop\_nameservice=hadoop-namenode
> > 
> > How can I define inventory-specific variables, while still preserving the structured definition as in group\_vars/all?
> > 
> > Thanks a lot!
> > 
> > –  
> > You received this message because you are subscribed to the Google Groups “Ansible Project” group.  
> > To unsubscribe from this group and stop receiving emails from it, send an email to [ansible-proje...@googlegroups.com](mailto:ansible-proje...@googlegroups.com).  
> > To post to this group, send email to [ansible...@googlegroups.com](mailto:ansible...@googlegroups.com).  
> > To view this discussion on the web visit [https://groups.google.com/d/msgid/ansible-project/a62a724b-d9db-4bb0-980d-91efff5c869b%40googlegroups.com](https://groups.google.com/d/msgid/ansible-project/a62a724b-d9db-4bb0-980d-91efff5c869b%40googlegroups.com?utm_medium=email&utm_source=footer).  
> > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

在 2014年7月8日星期二UTC+8上午3时29分14秒，Michael DeHaan写道：

> Perhaps I’m not understanding the question, but:
> 
> group\_vars/all  
> group\_vars/groupname1
> 
> group\_vars/groupname2
> 
> etc
> 
> This can be used with multiple inventory files if group\_vars is a directory in the directory with both of them, which should be what you need.
> 
> Let us know if this doesn’t make sense and perhaps I can elaborate further.
> 
> Thanks!
> 
> > I’m using Ansible to deploy and maintain Hadoop clusters.
> > 
> > I used to define variables in group\_vars/all as shown below:
> > 
> > > hadoop:
> > > 
> > > # Variables for core-site.xml
> > > 
> > > nameservice: hadoop-namenode
> > > 
> > > # Variables for hdfs-site.xml
> > > 
> > > dfs\_blocksize: 256m
> > > 
> > > # Variables for mapred-site.xml
> > > 
> > > mapred\_job\_tracker: hadoop-jobtracker:8021
> > 
> > However, I have different inventory files, which makes it prone to mistakes.
> > 
> > I can define variables using [all:vars] in inventory files:
> > 
> > > [all:vars]  
> > > hadoop\_nameservice=hadoop-namenode
> > 
> > How can I define inventory-specific variables, while still preserving the structured definition as in group\_vars/all?
> > 
> > Thanks a lot!
> > 
> > –  
> > You received this message because you are subscribed to the Google Groups “Ansible Project” group.  
> > To unsubscribe from this group and stop receiving emails from it, send an email to [ansible-proje...@googlegroups.com](mailto:ansible-proje...@googlegroups.com).  
> > To post to this group, send email to [ansible...@googlegroups.com](mailto:ansible...@googlegroups.com).  
> > To view this discussion on the web visit [https://groups.google.com/d/msgid/ansible-project/a62a724b-d9db-4bb0-980d-91efff5c869b%40googlegroups.com](https://groups.google.com/d/msgid/ansible-project/a62a724b-d9db-4bb0-980d-91efff5c869b%40googlegroups.com?utm_medium=email&utm_source=footer).  
> > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

在 2014年7月8日星期二UTC+8上午3时29分14秒，Michael DeHaan写道：

---

<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 8, 2014, 11:57am UTC](https://forum.ansible.com/t/need-advise-on-define-inventory-specific-variables/15616/4 "2014-07-08T11:57:14Z")

</div>

“They have the same group names, therefore I decide to move the variable definitions from group\_vars to the all:vars section of inventory file.  
However, I cannot define structured variables like before in group\_vars.”

I would consider different group names for each, and a parent group that contains them called NameNode or similar.

This way they could be selected by the same group but have different values for variables.
