如何获得Ansible主机文件中定义的当前计算机的主机名? [英] How to get the host name of the current machine as defined in the Ansible hosts file?
问题描述
我正在设置Ansible剧本来设置几个服务器.如果当前主机是我的本地dev主机(在我的hosts文件中名为"local"),我只想执行几个任务.我怎样才能做到这一点?我在文档的任何地方都找不到它.
I'm setting up an Ansible playbook to set up a couple servers. There are a couple of tasks that I only want to run if the current host is my local dev host, named "local" in my hosts file. How can I do this? I can't find it anywhere in the documentation.
我已经尝试了when语句,但是失败了,因为ansible_hostname
解析为创建计算机时生成的主机名,而不是您在hosts文件中定义的主机名.
I've tried this when statement, but it fails because ansible_hostname
resolves to the host name generated when the machine is created, not the one you define in your hosts file.
- name: Install this only for local dev machine
pip: name=pyramid
when: ansible_hostname == "local"
推荐答案
必需的变量是 inventory_hostname
.
The necessary variable is inventory_hostname
.
- name: Install this only for local dev machine
pip: name=pyramid
when: inventory_hostname == "local"
它在文档位于本节底部.
这篇关于如何获得Ansible主机文件中定义的当前计算机的主机名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!