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

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

问题描述

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

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:

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

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

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