2016-07-04 27 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

相关问题