在角色之间共享重复的代码块

时间:2016-08-17 21:50:46

标签: ansible ansible-playbook ansible-2.x

在我的大部分角色开始时,都有以下代码块。它尝试加载操作系统和版本特定的变量和任务。我可以不为每个角色重复这段代码吗?

---

- 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 个答案:

答案 0 :(得分:1)

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