# simple pattern in /etc/ansible/hosts file so as to be DRY?

**URL:** <https://forum.ansible.com/t/simple-pattern-in-etc-ansible-hosts-file-so-as-to-be-dry/11635>\
**Category:** Ansible Project\
**Created:** [June 30, 2012, 9:08pm UTC](https://forum.ansible.com/t/simple-pattern-in-etc-ansible-hosts-file-so-as-to-be-dry/11635 "2012-06-30T21:08:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zack\_Perry](https://avatars.discourse-cdn.com/v4/letter/z/54ee81/32.png) [@Zack\_Perry](https://forum.ansible.com/u/Zack_Perry)\
**Post date:** [June 30, 2012, 9:08pm UTC](https://forum.ansible.com/t/simple-pattern-in-etc-ansible-hosts-file-so-as-to-be-dry/11635/1 "2012-06-30T21:08:17Z")

</div>

I have been using ansible for a while now, and really like it.

Unless I have missed it In [http://ansible.github.com/patterns.html#hosts-and-groups](http://ansible.github.com/patterns.html#hosts-and-groups), we need to enumerate all hosts in a group, even the names of such hosts are very similar, e.g. host0, host1, host2, host3 etc and thus can be more succinctly specified as host[1-3] instead.

I know that adding such additional parsing of a plain ini style file adds complexity to the core codes. But, IMHO if such a capability is not implemented yet, it might be worth considering?

Just a suggestion.

Regards,

Zack

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [June 30, 2012, 9:35pm UTC](https://forum.ansible.com/t/simple-pattern-in-etc-ansible-hosts-file-so-as-to-be-dry/11635/2 "2012-06-30T21:35:14Z")

</div>

> I know that adding such additional parsing of a plain ini style file adds complexity to the core codes. But, IMHO if such a capability is not implemented yet, it might be worth considering?

You may want to consider code-generating the file or using an external inventory script if you need something immediate, but I would take patches to allow this sort of expansion of host records.

However, if you do this, it should be done consistently for both the INI and YAML style parsers.

---

<div class="post-metadata">

**Author:** ![Zack\_Perry](https://avatars.discourse-cdn.com/v4/letter/z/54ee81/32.png) [@Zack\_Perry](https://forum.ansible.com/u/Zack_Perry)\
**Post date:** [June 30, 2012, 11:07pm UTC](https://forum.ansible.com/t/simple-pattern-in-etc-ansible-hosts-file-so-as-to-be-dry/11635/3 "2012-06-30T23:07:54Z")

</div>

Hi Michael,

> but I would take patches to allow this sort of  
> expansion of host records.
> 
> However, if you do this, it should be done consistently for  
> both the INI and YAML style parsers.

OK. I see what you mean. in ansible/lib/ansible/inventory/\_\_init\_\_.py I saw the following:

&nbsp;&nbsp;&nbsp;&nbsp;71 data = file(host\_list).read()  
&nbsp;&nbsp;&nbsp;&nbsp;72 if not data.startswith("---"):  
&nbsp;&nbsp;&nbsp;&nbsp;73 self.parser = InventoryParser(filename=host\_list)  
&nbsp;&nbsp;&nbsp;&nbsp;74 self.groups = self.parser.groups.values()  
&nbsp;&nbsp;&nbsp;&nbsp;75 else:  
&nbsp;&nbsp;&nbsp;&nbsp;76 self.parser = InventoryParserYaml(filename=host\_list)  
&nbsp;&nbsp;&nbsp;&nbsp;77 self.groups = self.parser.groups.values()

I will see what I can come up with.

Regards,

Zack

---

<div class="post-metadata">

**Author:** ![Zack\_Perry](https://avatars.discourse-cdn.com/v4/letter/z/54ee81/32.png) [@Zack\_Perry](https://forum.ansible.com/u/Zack_Perry)\
**Post date:** [June 30, 2012, 11:25pm UTC](https://forum.ansible.com/t/simple-pattern-in-etc-ansible-hosts-file-so-as-to-be-dry/11635/4 "2012-06-30T23:25:57Z")

</div>

Hi Michael,

Just a quick follow-up to my prior email.

> However, if you do this, it should be done consistently for  
> both the INI and YAML style parsers.

Different sites tend to use different host naming conventions. Our own convention here is usually the following:

\* All hosts are categorized into different and non-overlapping categories (usually according the major functions of a set of nodes)  
\* For all hosts in a category, they share a common base name (e.g. nn =\> name nodes, tn =\> tracker nodes, dn =\> data nodes), and then followed by digits (0 .. N). A full example: nn01

This is quite simple to deal with. But if I work on a patch, I would like to make it more generally useful. Perhaps we should solicit others inputs in this regard?

Regards,

Zack

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [July 1, 2012, 12:31am UTC](https://forum.ansible.com/t/simple-pattern-in-etc-ansible-hosts-file-so-as-to-be-dry/11635/5 "2012-07-01T00:31:18Z")

</div>

--Michael
