2016-08-17 29 views
2

在我的大多数角色开始时,有以下代码块。它试图加载操作系统和版本特定的变量和任务。是否有可能我没有为我的每个角色重复这段代码?在角色之间分享重复的代码块

--- 

- name: Include OS specific variables. 
    include_vars: "{{ item }}" 
    with_first_found: 
    - files: 
     - "vars/{{ ansible_os_family }}.yml" 
     skip: True 

- name: Include distribution specific variables. 
    include_vars: "{{ item }}" 
    with_first_found: 
    - files: 
     - "vars/{{ ansible_distribution }}.yml" 
     skip: True 

- name: Include version specific variables. 
    include_vars: "{{ item }}" 
    with_first_found: 
    - files: 
     - "vars/{{ ansible_distribution }}{{ ansible_distribution_major_version }}.yml" 
     skip: True 

- name: Run OS specific preparation work. 
    include: "{{ item }}" 
    with_first_found: 
    - files: 
     - "tasks/{{ ansible_os_family }}.yml" 
     skip: True 

- name: Run distribution specific preparation work. 
    include: "{{ item }}" 
    with_first_found: 
    - files: 
     - "tasks/{{ ansible_distribution }}.yml" 
     skip: True 

- name: Run version specific preparation work. 
    include: "{{ item }}" 
    with_first_found: 
    - files: 
     - "tasks/{{ ansible_distribution }}{{ ansible_distribution_major_version }}.yml" 
     skip: True 

回答

1

是的,您可以使用include statements or even another role从外部共享文件中提取任务。

+0

我主要关心的是,如果我使用include或其他角色,查找路径是否正确。如果我使用另一个角色,它是如何看待这个角色的'vars'目录?你有没有任何工作的例子? –

+0

你是对的,角色只会在传递相对路径时查看相邻目录。但有人猜测,如果你去为独立的角色(我建议不管)。你将拥有共同角色中的所有变量/任务。 – shaps