Loops Ansible:我如何避免在src文件和first-found文件中重复代码?

Loops Ansible:我如何避免在src文件和first-found文件中重复代码?,loops,ansible,repeat,Loops,Ansible,Repeat,我有几十个复制操作,它们根据以下配置文件存储层次结构(无法更改)执行相同的操作以查找适当的文件: 有没有办法避免对每个配置文件重复整个with_first_found子句,如下所示 - name: Copy /etc/file.name copy: src: "{{item}}" dest: "/etc/file.name" owner: root group: root mode: 0644 with_first_found: - fil

我有几十个复制操作,它们根据以下配置文件存储层次结构(无法更改)执行相同的操作以查找适当的文件:

有没有办法避免对每个配置文件重复整个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

将任务提取到单独的文件和外部循环中,以避免
变量中的冲突

- 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