2016-07-04 95 views
2

我的意圖是在不同的主機中執行每個角色。我正在做一個簡單的任務,即在每臺主機上下載文件。我有一個主機文件,它看起來像這樣Ansible在不同的主機上執行每個角色

[groupa] 
10.0.1.20 

[groupb] 
10.0.1.21 
下面

是我main_file.yml文件我的角色

--- 
    - hosts: local 
    connection: local 
    gather_facts: no 
    roles: 
     - oracle 
     - apache 

結構

main_file.yml 
roles 
|-- oracle 
| |-- tasks 
|  |-- main.yml 
|  |-- download_file.yml 
|-- apache 
| |-- tasks 
|  |-- main.yml 
|  |-- download_file.yml 

ORACLE/main.yml

--- 
- name: downloading a file in groupa 
    hosts: groupa 
    tasks: 
    - include: tasks/download_file.yml 

oracle/download_file.yml

--- 
- name: download file 
    shell: wget http://dummyurl.com/random.sh 

對於「groupb」,Apache角色也遵循相同的步驟。但是,當我執行main_file.yml我提示以下錯誤:

ERROR! no action detected in task. This often indicates a misspelled module name, or incorrect module path. 

The error appears to have been in '/etc/ansible/roles/oracle/tasks/main.yml': line 2, column 3, but may 
be elsewhere in the file depending on the exact syntax problem. 

The offending line appears to be: 

--- 
- name: downloading a file 
^here 
+0

您的main.yml沒有如所示的調試語句。你能包括整個事情嗎? –

+0

即使我只是在oracle/main.yml中添加任何主機而不執行調試任務,我也會得到相同的錯誤 – shwetha

回答

5

在ansible有兩個層次,一個是劇本的水平,另外一個是任務的水平。在劇本級別上,您可以指定要在哪些主機上運行任務,但在任務級別下,這已不再可行,因爲已經指定了主機。角色包含在任務級別中,因此您不能在其中包含主機聲明。

您應該從main.yml除去主機,而只顯示包括:

--- 
- name: downloading a file in groupa 
    include: download_file.yml 

由於角色基本上都是模板特定主機,如果你想讓他們到一個特定的主機只包括上運行他們在你的劇本相應。例如,在您的main_file.yml中,您可以編寫以下內容:

--- 
- hosts: groupa 
    roles: 
    - oracle 

- hosts: groupb 
    roles: 
    - apache 

- hosts: local 
    connection: local 
    tasks: 
    - { debug: { msg: "Tasks to run locally" } } 
+0

謝謝,這是有效的。但是我希望每個角色在不同的主機上運行,​​這是可能的嗎? – shwetha

相關問題