# Parsing ansible\_hostname

**URL:** <https://forum.ansible.com/t/parsing-ansible-hostname/16294>\
**Category:** Ansible Project\
**Created:** [September 22, 2014, 3:27am UTC](https://forum.ansible.com/t/parsing-ansible-hostname/16294 "2014-09-22T03:27:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![phanoko](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/phanoko/32/517_2.png) [@phanoko](https://forum.ansible.com/u/phanoko)\
**Post date:** [September 22, 2014, 3:27am UTC](https://forum.ansible.com/t/parsing-ansible-hostname/16294/1 "2014-09-22T03:27:51Z")

</div>

Hi all, I hope somebody can offer me a little guidance. I’ve read a decent amount on jinja2 and as I’m still learning python; so with that I hoped for a little guidance.  
Essentially I want to apply a role based off an item in the hostname.

So {{ ansible\_hostname }} = las01-123-984 where 123 is the host type. That’s my role that’s setup. It sets up the box as an apache system with new firewall and all that.  
I’ve just been unable to get the proper parser to pull the 123 from the hostname.

Any guidance would be fantastic, thank you all!!

---

<div class="post-metadata">

**Author:** ![James\_Martin1](https://avatars.discourse-cdn.com/v4/letter/j/0ea827/32.png) [@James\_Martin1](https://forum.ansible.com/u/James_Martin1)\
**Post date:** [September 22, 2014, 1:07pm UTC](https://forum.ansible.com/t/parsing-ansible-hostname/16294/2 "2014-09-22T13:07:23Z")

</div>

I believe you’d want to use an inventory pattern here:

- hosts: _-apache-_  
roles:
- apache

See this link for more information: [http://docs.ansible.com/intro\_patterns.html](http://docs.ansible.com/intro_patterns.html)

- James

---

<div class="post-metadata">

**Author:** ![Michael\_Peters](https://avatars.discourse-cdn.com/v4/letter/m/c2a13f/32.png) [@Michael\_Peters](https://forum.ansible.com/u/Michael_Peters)\
**Post date:** [September 22, 2014, 2:02pm UTC](https://forum.ansible.com/t/parsing-ansible-hostname/16294/3 "2014-09-22T14:02:53Z")

</div>

The regex\_replace filter is probably what you're looking for. Assuming  
there are only ever 3 segments separated by a hyphen, and you want the  
2nd, something like this would work:

{{ ansible\_hostname | regex\_replace('^[^-]+-', '') | regex\_replace('-.\*', '') }}

---

<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:** [September 22, 2014, 5:24pm UTC](https://forum.ansible.com/t/parsing-ansible-hostname/16294/4 "2014-09-22T17:24:14Z")

</div>

I’d vote for James Martin’s suggestion for selecting hosts based on the pattern.

The other vote is good enough for getting out the string and putting it into a conditional, but using the pattern construct is more readable and also produces cleaner output.

---

<div class="post-metadata">

**Author:** ![phanoko](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/phanoko/32/517_2.png) [@phanoko](https://forum.ansible.com/u/phanoko)\
**Post date:** [September 29, 2014, 3:36am UTC](https://forum.ansible.com/t/parsing-ansible-hostname/16294/5 "2014-09-29T03:36:15Z")

</div>

Sorry for the delay.

That has worked fantastic! Thank you so much for your help.
