【问题标题】:How to get the host name of the current machine as defined in the Ansible hosts file?如何获取 Ansible hosts 文件中定义的当前机器的主机名?
【发布时间】:2014-02-16 06:09:57
【问题描述】:

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

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

- name: Install this only for local dev machine
  pip: name=pyramid
  when: ansible_hostname == "local"

【问题讨论】:

    标签: ansible


    【解决方案1】:

    必要的变量是inventory_hostname

    - name: Install this only for local dev machine
      pip: name=pyramid
      when: inventory_hostname == "local"
    

    它在文档at the bottom of this section 中有些隐藏。

    【讨论】:

      【解决方案2】:

      您可以通过更改剧本中的主机标题来限制剧本的范围,而无需依赖库存中的特殊主机标签“本地”。本地主机不需要库存中的特殊行。

      - name: run on all except local
        hosts: all:!local
      

      【讨论】:

      • hosts: !localhost 可能也适用于例如--limit '!dev' 工作
      【解决方案3】:

      这是另一种选择:

      - name: Install this only for local dev machine
        pip: name=pyramid
        delegate_to: localhost
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-06-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多