从主机文件中获取主机名 [英] Get the hostname from the hosts file

查看:40
本文介绍了从主机文件中获取主机名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的一个模板中获取另一台主机.

I would like to get another host from one of my templates.

给定以下 hosts 文件:

[vm]
vm_hostname

[monitoring]
monitoring_hostname

如何从模板访问monitoring_hostname?

我试过了:

monitor: {{ hostvars['monitoring'][0] }}

注意:我不能只使用 monitor: monitoring_hostname 因为我的 hosts 文件是动态生成的.

NB: I can't just use monitor: monitoring_hostname because my hosts file is dynamically generated.

推荐答案

您想使用 groups 魔法变量,而不是 hostvars:

You want to use groups magic variable, not hostvars:

monitor: "{{ groups['monitoring'][0] }}"

这篇关于从主机文件中获取主机名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆