2017-07-25 48 views
0

Ansible版本:2.2.1.0Ansible多重條件不工作

HOSTFILE:

[master] 54.65.104.4 [slaves] 52.69.71.248

任務/ main.yaml

- name: Checking if the node is already having spark master installed (master) 
    stat: 
    path: /etc/init.d/spark-master 
    register: spark-master 
    when: inventory_hostname in groups['master'] 
- name: Checking if the node is already having spark worker installed 
    stat: 
    path: /etc/init.d/spark-slave 
    register: spark-slave 
    when: inventory_hostname in groups['slaves'] 

    # Downloading to the master 
- include: download.yaml 
    when: (inventory_hostname in groups['master']) and (spark-master.stat.exists == false) 

# Downloading to the slaves 
- include: download.yaml 
    when: (inventory_hostname in groups['slaves']) and (spark-slave.stat.exists == False) 

但是,當我執行腳本我收到出現以下錯誤:

fatal: [52.69.71.248]: FAILED! => {"failed": true, "msg": "The conditional check '(inventory_hostname in groups['slaves']) and (spark-slave.stat.exists == False)' failed. The error was: error while evaluating conditional ((inventory_hostname in groups['slaves']) and (spark-slave.stat.exists == False)): 'slave' is undefined\n\nThe error appears to have been in '/opt/ansible-role-spark/tasks/download.yaml': line 2, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n---\n- name: Download Spark\n^here\n"} 

任何人都可以幫助我計算出我正在犯的錯誤。

+0

「下載Spark」任務會拋出錯誤? – techraf

回答

1

要麼引用條件,如:

when: "(inventory_hostname in groups['master']) and (spark-master.stat.exists == false)" 

還是不要用括號括。

1

有問題的誤差約爲:

變量名應該是字母,數字和下劃線。

參見docs

但是,您的手冊中還有其他設計缺陷超出了這個問題。

+0

是的,我的劇本中有設計缺陷,儘管我已經將變量名改爲了下劃線。 – Bidyut

+0

如果初始化腳本不存在,我想在主服務器和從服務器上執行'download.yml',但是這兩個服務器中初始化腳本的名稱是不同的。 – Bidyut