英文:
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 }}"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论