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. On Mon, Sep 22, 2014 at 10:02 AM, Michael Peters <[email protected]> wrote: > 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('-.*', > '') }} > > On Mon, Sep 22, 2014 at 9:07 AM, James Martin <[email protected]> wrote: > > 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 > > > > - James > > > > > > On Sunday, September 21, 2014 11:27:51 PM UTC-4, Matt Wells wrote: > >> > >> 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!! > > > > -- > > 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 [email protected]. > > To post to this group, send email to [email protected]. > > To view this discussion on the web visit > > > https://groups.google.com/d/msgid/ansible-project/ff817e01-47d8-4118-96f3-5cb3901db354%40googlegroups.com > . > > > > For more options, visit https://groups.google.com/d/optout. > > -- > 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 [email protected]. > To post to this group, send email to [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/ansible-project/CAJQqANd5gA8UoHvZ3fRG_JZhV8pkNbu%3D2g%2B4ibbgvzXcrGY80A%40mail.gmail.com > . > For more options, visit https://groups.google.com/d/optout. > -- 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 [email protected]. To post to this group, send email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CA%2BnsWgwqosHPnDdRwiE9Hm3KabQOdJ2TdbgTNiXG64eaDn7X9w%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
