如何获取Ansible主机文件中定义的当前计算机的主机名?

Tan*_*rad 160 ansible

我正在设置一个Ansible手册来设置几个服务器.如果当前主机是我的本地开发主机,在我的主机文件中名为"local",那么我只想运行几个任务.我怎样才能做到这一点?我在文档中的任何地方都找不到它.

我在if语句时尝试了这个,但它失败了,因为ansible_hostname解析了创建机器时生成的主机名,而不是你在hosts文件中定义的主机名.

- name: Install this only for local dev machine
  pip: name=pyramid
  when: ansible_hostname == "local"
Run Code Online (Sandbox Code Playgroud)

Tan*_*rad 261

必要的变量是inventory_hostname.

- name: Install this only for local dev machine
  pip: name=pyramid
  when: inventory_hostname == "local"
Run Code Online (Sandbox Code Playgroud)

它隐藏在本节底部的文档.

  • 当前变量记录在:https://docs.ansible.com/ansible/latest/reference_appendices/special_variables.html (3认同)