Ansible:如何避免使用文件src和with_first_found重复代码?

时间:2017-08-23 02:33:28

标签: loops ansible repeat

我有几十个复制操作可以根据以下配置文件存储层次结构查找相应的文件(无法更改):

{{role_path}}/file.name.{hostname}
{{role_path}}/file.name
/config/current/file.name.{hostname}
/config/current/file.name
/config/legacy/file.name.{hostname}
/config/legacy/file.name

有没有办法避免为每个配置文件重复整个with_first_found子句,如下所示?

- name: Copy /etc/file.name
  copy:
    src: "{{item}}"
    dest: "/etc/file.name"
    owner: root
    group: root
    mode: 0644
  with_first_found:
    - files:
      - files/etc/file.name.{{inventory_hostname}}
      - files/etc/file.name
      paths:
        - "{{ role_path }}"
        - /config/current
        - /config/legacy

1 个答案:

答案 0 :(得分:1)

将任务解压缩到单独的文件中,并在外部循环中loop over include using loop_control,以避免item变量发生冲突。