Ansible:使模板使用在不同文件中为不同运行定义的变量

Ansible:使模板使用在不同文件中为不同运行定义的变量,ansible,Ansible,我在剧本中有一个任务,如图所示: - name: Create configuration.json for every analytics from the template. template: src: ./src-zip/{{ item.key }}/configuration_sample.j2 dest: ./src-zip/{{ item.key }}/configuration.json with_dict: "{{ apps }}" 现在,我在文件中为

我在剧本中有一个任务,如图所示:

- name: Create configuration.json for every analytics from the template.
  template:
    src: ./src-zip/{{ item.key }}/configuration_sample.j2
    dest: ./src-zip/{{ item.key }}/configuration.json
  with_dict: "{{ apps }}"
现在,我在文件中为每次运行定义了一些通用变量和一些不同变量

比如,一次运行就有文件
var\u alerts-manager.yml
。 对于另一次运行,我有
var_abc.yml

现在我想为不同的运行使用不同的文件。换句话说,模板将在一次运行中使用
var_alerts-manager.yml
中定义的变量,在另一次运行中使用
abc.yml
中定义的变量,依此类推


如何在ansible中实现这一点?我应该将这些文件保存在何处,以便每次运行时任务只能包含该特定文件?

这是角色机制演变的场景:

  • 在剧本目录中创建一个
    my_角色
    角色

  • 将任务和模板移动到该角色中

  • 将变量存储在角色的
    vars
    目录中

  • 使用以下命令执行角色:

    - name: Create configuration.json for every analytics from the template.
      include_role:
        name: my_role
        vars_from: "{{ item.key }}"  # or whatever key your naming is defined in
      loop: "{{ apps|dict2items }}"
    

  • 您可能希望用其他变量替换模板任务中的
    ,为清楚起见,请阅读。

    这是角色机制演变的场景:

  • 在剧本目录中创建一个
    my_角色
    角色

  • 将任务和模板移动到该角色中

  • 将变量存储在角色的
    vars
    目录中

  • 使用以下命令执行角色:

    - name: Create configuration.json for every analytics from the template.
      include_role:
        name: my_role
        vars_from: "{{ item.key }}"  # or whatever key your naming is defined in
      loop: "{{ apps|dict2items }}"
    
  • 您可能希望用其他变量替换模板任务中的
    ,为清楚起见,请阅读