2015-05-29 90 views
13

我在一個角色中擁有多個任務,如下所示。我不想創建另一個yml文件來處理此任務。我已經爲Web服務器提供了一個包含,但是我們的幾個Perl服務器需要安裝一些Web包。僅當主機名包含字符串時才運行Ansible任務

- name: Install Perl Modules 
    command: <command> 
    with_dict: perl_modules 

- name: Install PHP Modules 
    command: <command> 
    with_dict: php_modules 
    when: <Install php modules only if hostname contains the word "batch"> 

主機清單文件

[webs] 
web01 
web02 
web03 

[perl] 
perl01 
perl02 
perl03 
perl-batch01 
perl-batch02 

回答

29

下面應該做的伎倆:

- name: Install PHP Modules 
    command: <command> 
    with_dict: php_modules 
    when: "'batch' in inventory_hostname" 

注意你有幾個劇本運行期間跳過主機。

​​是Ansible的「魔術」變量之一:

此外,inventory_hostname是主機名 在Ansible的庫存主機文件中配置的名稱。當您不想依靠發現的主機名 ansible_hostname或出於其他神祕原因時,這對於 會很有用。如果你有很長 FQDN,inventory_hostname_short還包含部分到第一 期間,沒有域的其餘部分。

來源:Ansible Docs - Magic variables and how to access information about other hosts

相關問題