How to get the host name of the current machine as defined in the Ansible hosts file?

前端 未结 3 915
情书的邮戳
情书的邮戳 2020-12-07 16:12

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\"

相关标签:
3条回答
  • 2020-12-07 16:39

    You can limit the scope of a playbook by changing the hosts header in its plays without relying on your special host label ‘local’ in your inventory. Localhost does not need a special line in inventories.

    - name: run on all except local
      hosts: all:!local
    
    0 讨论(0)
  • 2020-12-07 16:49

    The necessary variable is inventory_hostname.

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

    It is somewhat hidden in the documentation at the bottom of this section.

    0 讨论(0)
  • 2020-12-07 16:50

    This is an alternative:

    - name: Install this only for local dev machine
      pip: name=pyramid
      delegate_to: localhost
    
    0 讨论(0)
自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题