在 Ansible 模块中循环
Loop inside an Ansible module
我正在尝试使用 community.aws.ecs_taskdefinition
创建 ECS 任务定义 我写了这样的东西:
- name: Create task definition
community.aws.ecs_taskdefinition:
containers:
- name: First_container
<< container definition >>
- name: "{{ item.service }}"
essential: true
image: "{{ user_info.account }}.dkr.ecr.{{ AWS_REGION }}.amazonaws.com/{{ app_name }}-{{ item.service }}"
environment:
"{{ item.env_var }}"
portMappings:
- containerPort: "{{ item.port_a }}"
hostPort: "{{ item.port_b }}"
with_items:
- "{{ parameters }}"
基本上我想创建 one 任务定义,第一个容器加上 parameters[ 中定义的所有其他容器=34=]。
不幸的是,如果我把 with_item 放在上面,它不起作用,如果我把它放在 containers
的同一级别,它将创建相同任务定义的许多修订,并且每个修订都会有第一个容器,并且只是 parameters.
中定义的容器之一
有什么办法可以实现吗?
编辑:澄清
基本上参数是一个字典列表,如:
parameters : [{service:"service1",env_var:"something",port_a:80,port_b:80} ,
{ service:"service2",env_var:"somethingelse",port_a:3000,port_b:3000}]
我想达到的效果是一样的:
- name: Create task definition
community.aws.ecs_taskdefinition:
containers:
- name: First_container
<< container definition >>
- name: "service1"
essential: true
image: "{{ user_info.account }}.dkr.ecr.{{ AWS_REGION }}.amazonaws.com/{{ app_name }}-service1"
environment:
env_var: "something"
portMappings:
- containerPort: 80
hostPort: 80
- name: "service2"
essential: true
image: "{{ user_info.account }}.dkr.ecr.{{ AWS_REGION }}.amazonaws.com/{{ app_name }}-service2"
environment:
env_var: "somethingelse"
portMappings:
- containerPort: 3000
hostPort: 3000
但是因为参数可以有很多服务,所以我想有一个循环来处理它。希望更清楚。
您无法直接在剧本中访问 {{ parameters }}
变量。
解决方法是:
loop:
- { service: "service1", env_var: "something", port_a: 80, port_b: 80 }
- { service: "service2", env_var: "somethingelse", port_a: 3000, port_b: 3000 }
或者在清单文件中注册变量或将其添加到您的剧本中。
您可以尝试创建一个变量并将其包含在任务中:
tasks:
- set_fact:
containers: "{{ container0 + other_containers.names}}"
vars:
parameters : [{"service":"service1", "env_var":"something", "port_a":80, "port_b":80} ,
{ "service":"service2", "env_var":"somethingelse", "port_a":3000, "port_b":3000}]
container0:
- name: "First_container"
other_param: "otherrrrrrs"
other_containers: >-
{ "names": [
{%- for p in parameters -%}
{"name": {
"essential": true,
"environment": "{{ p.env_var }}",
"portMappings": [{ "containerPort": "{{ p.port_a }}", "hostPort": "{{ p.port_b }}" }]
}
} {{ "" if loop.last else "," }}
{%- endfor -%}
]}
- debug:
msg: "{{ containers }}"
- name: Create task definition
community.aws.ecs_taskdefinition:
containers: "{{ containers }}"
我正在尝试使用 community.aws.ecs_taskdefinition
创建 ECS 任务定义 我写了这样的东西:
- name: Create task definition
community.aws.ecs_taskdefinition:
containers:
- name: First_container
<< container definition >>
- name: "{{ item.service }}"
essential: true
image: "{{ user_info.account }}.dkr.ecr.{{ AWS_REGION }}.amazonaws.com/{{ app_name }}-{{ item.service }}"
environment:
"{{ item.env_var }}"
portMappings:
- containerPort: "{{ item.port_a }}"
hostPort: "{{ item.port_b }}"
with_items:
- "{{ parameters }}"
基本上我想创建 one 任务定义,第一个容器加上 parameters[ 中定义的所有其他容器=34=]。
不幸的是,如果我把 with_item 放在上面,它不起作用,如果我把它放在 containers
的同一级别,它将创建相同任务定义的许多修订,并且每个修订都会有第一个容器,并且只是 parameters.
有什么办法可以实现吗?
编辑:澄清 基本上参数是一个字典列表,如:
parameters : [{service:"service1",env_var:"something",port_a:80,port_b:80} ,
{ service:"service2",env_var:"somethingelse",port_a:3000,port_b:3000}]
我想达到的效果是一样的:
- name: Create task definition
community.aws.ecs_taskdefinition:
containers:
- name: First_container
<< container definition >>
- name: "service1"
essential: true
image: "{{ user_info.account }}.dkr.ecr.{{ AWS_REGION }}.amazonaws.com/{{ app_name }}-service1"
environment:
env_var: "something"
portMappings:
- containerPort: 80
hostPort: 80
- name: "service2"
essential: true
image: "{{ user_info.account }}.dkr.ecr.{{ AWS_REGION }}.amazonaws.com/{{ app_name }}-service2"
environment:
env_var: "somethingelse"
portMappings:
- containerPort: 3000
hostPort: 3000
但是因为参数可以有很多服务,所以我想有一个循环来处理它。希望更清楚。
您无法直接在剧本中访问 {{ parameters }}
变量。
解决方法是:
loop:
- { service: "service1", env_var: "something", port_a: 80, port_b: 80 }
- { service: "service2", env_var: "somethingelse", port_a: 3000, port_b: 3000 }
或者在清单文件中注册变量或将其添加到您的剧本中。
您可以尝试创建一个变量并将其包含在任务中:
tasks:
- set_fact:
containers: "{{ container0 + other_containers.names}}"
vars:
parameters : [{"service":"service1", "env_var":"something", "port_a":80, "port_b":80} ,
{ "service":"service2", "env_var":"somethingelse", "port_a":3000, "port_b":3000}]
container0:
- name: "First_container"
other_param: "otherrrrrrs"
other_containers: >-
{ "names": [
{%- for p in parameters -%}
{"name": {
"essential": true,
"environment": "{{ p.env_var }}",
"portMappings": [{ "containerPort": "{{ p.port_a }}", "hostPort": "{{ p.port_b }}" }]
}
} {{ "" if loop.last else "," }}
{%- endfor -%}
]}
- debug:
msg: "{{ containers }}"
- name: Create task definition
community.aws.ecs_taskdefinition:
containers: "{{ containers }}"