如何获取 Ansible hosts 文件中定义的当前机器的主机名? [英] How to get the host name of the current machine as defined in the Ansible hosts file?

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

问题描述

我正在设置 Ansible playbook 来设置几个服务器.如果当前主机是我的本地开发主机(在我的主机文件中命名为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 解析为创建机器时生成的主机名,而不是您在主机文件中定义的主机名.

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 hosts 文件中定义的当前机器的主机名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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