Mikhail Janowski
Mikhail Janowski

Reputation: 4669

How to get a hostname that contains a certain string from a group in Ansible

I'm using Ansible and need to lookup my database server to use in a config file.

I have a group of all database servers: groups.rds and I know that the server I'm looking for contains a certain string, eg. "development".

What is the cleanest way to find that hostname?

I'm looking for something like this: groups.rds.contains("development").first()

Upvotes: 4

Views: 1183

Answers (1)

udondan
udondan

Reputation: 60029

You can do this with the select filter, combined with the match filter.

groups.rds | select("match", ".*production.*") | first

But you're asking for the cleanest solution. From here it looks like you're trying to identify an environment (development) based on a host name. If that's the case, wouldn't it make sense to have another group for this?

If you'd had something like this:

[non_rds]
some.unrelated.development.host

[rds]
some.production.host
some.staging.host
some.development.host

[production]
some.production.host

[staging]
some.staging.host

[development]
some.development.host
some.unrelated.development.host

Then it would be very easy to get the intersection of these groups:

groups.rds | intersect(groups.development) | first

This would give you only the hosts which are in both groups rds and development, which is some.development.host.

Upvotes: 2

Related Questions