如何循环集合并应用多个任务

huangapple go评论75阅读模式
英文:

How to loop collection and apply multiple tasks

问题

我理解,你需要将上述内容翻译成中文。以下是翻译好的部分:

集合

- artifactPath: 'C:/artifacts/artifact'

- envs:
  - [{
       name: 'ENV1',
       path: 'C:/site/env1',
       config: 'env1.config'
     },
     {
       name: 'ENV2',
       path: 'C:/site/env2',
       config: 'env2.config'
     }]

任务

- name {{name}} 任务1 清空目标目录 {{path}}
  win_file:
    path: "{{path}}"
    state: absent
- name {{name}} 任务2 复制文件
  win_copy:
    remote_src: yes
    src: "{{artifactPath}}"
    dest: "{{path}}"
- name {{name}} 任务3 覆盖配置
  win_copy:
    remote_src: yes
    src: "{{artifactPath}}/{{config}}"
    dest: "{{path}}/config.json"

请注意,代码部分保持原样,只翻译了文本内容。

英文:

I'm trying to achieve the following in Ansible YAML. I have a collection of environment objects. I'd like to loop through them and complete 3 tasks on each object. I've seen a loop keyword but not with multiple tasks. What is the neatest way to achieve this in Ansible YAML?

The collection

- artifactPath: 'C:/artifacts/artifact'

- envs:
  - [{
       name: 'ENV1',
       path: 'C:/site/env1',
       config: 'env1.config'
     },
     {
       name: 'ENV2',
       path: 'C:/site/env2',
       config: 'env2.config'
     }]

The Tasks

- name {{name}} Task1 Clean down target directory {{path}}
  win_file
    path: "{{path}}"
    state: absent
- name {{name}} Task2 Copy files
  win_copy:
    remote_src:yes
    src: "{{artifactPath}}"
    dest: "{{path}}"
- name {{name}} Task3 Override config
  win_copy:
    remote_src:yes
    src: "{{artifactPath}}/{{config}}"
    dest: "{{path}}/config.json"

答案1

得分: 0

- hosts: all

  vars:

    artifactPath: 'C:/artifacts/artifact'
    envs:
      - name: ENV1
        path: 'C:/site/env1'
        config: 'env1.config'
      - name: ENV2
        path: 'C:/site/env2'
        config: 'env2.config'

  tasks:

    - name: Task1 Clean target directory
      debug:
        msg: |
          win_file:
            path: "{{ item.path }}"
            state: absent          
      loop: "{{ envs }}"
      loop_control:
        label: "{{ item.name }}"

    - name: Task2 Copy files
      debug:
        msg: |
          win_copy:
            remote_src: yes
            src: "{{ artifactPath }}"
            dest: "{{ item.path }}"          
      loop: "{{ envs }}"
      loop_control:
        label: "{{ item.name }}"

    - name: Task3 override config
      debug:
        msg: |
          win_copy:
            remote_src: yes
            src: "{{ artifactPath }}/{{ item.config }}"
            dest: "{{ item.path }}/config.json"          
      loop: "{{ envs }}"
      loop_control:
        label: "{{ item.name }}"
英文:

The play below shows how you can iterate the list

- hosts: all

  vars:

    artifactPath: 'C:/artifacts/artifact'
    envs:
      - name: ENV1
        path: 'C:/site/env1'
        config: 'env1.config'
      - name: ENV2
        path: 'C:/site/env2'
        config: 'env2.config'

  tasks:

    - name: Task1 Clean target directory
      debug:
        msg: |
          win_file:
            path: "{{ item.path }}"
            state: absent          
      loop: "{{ envs }}"

    - name: Task2 Copy files
      debug:
        msg: |
          win_copy:
            remote_src: yes
            src: "{{ artifactPath }}"
            dest: "{{ item.path }}"          
      loop: "{{ envs }}"

    - name: Task3 override config
      debug:
        msg: |
          win_copy:
            remote_src: yes
            src: "{{ artifactPath }}/{{ item.config }}"
            dest: "{{ item.path }}/config.json"          
      loop: "{{ envs }}"
  • The consistency of the win_* parameters is up to you.

  • You can declare the label in each loop to make the output easier to read

      loop_control:
        label: "{{ item.name }}"

huangapple
  • 本文由 发表于 2023年5月17日 19:15:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76271472.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定