[英]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. 我正在使用Ansible,需要查找我的数据库服务器以在配置文件中使用。
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. 我有一组所有数据库服务器: groups.rds
我知道我正在寻找的服务器包含某个字符串,例如。 "development". “发展”。
What is the cleanest way to find that hostname? 找到该主机名最简洁的方法是什么?
I'm looking for something like this: groups.rds.contains("development").first()
我正在寻找这样的东西: groups.rds.contains("development").first()
You can do this with the select
filter , combined with the match
filter . 您可以使用select
过滤器和match
过滤器来完成此操作。
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
. 这将只为您提供rds
和development
两个组中的主机,即some.development.host
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.