将 dict 转换为期望它的列表 ansible podman 模块
Convert dict into list ansible podman module expecting it
我有以下指令:
ports:
http:
host: 81
container: 80
https:
host: 444
container: 443
我想在我的角色中使用它,部分原因是它非常适合此任务的示例(按预期工作)。
- name: ensure container's exposed ports firewall state
tags: firewall
firewalld:
port: "{{ item.value.host }}/tcp"
permanent: yes
immediate: yes
state: enabled
loop: "{{ lookup('dict', ports, wantlist=True) }}"
when: ports is defined
但是在接下来的任务中,循环将不起作用(我尝试启动一个启用了两个端口的容器):
- name: Run container
containers.podman.podman_container:
name: "{{ container_name }}"
image: "{{ container_image }}"
state: "{{ state }}"
recreate: "{{ recreate }}"
ports:
- "{{ item.value.host }}:{{ item.value.container }}"
loop: "{{ lookup('dict', ports, wantlist=True) }}"
如果我使用 loop 语句执行循环,它将启动两个容器(一个带 http,另一个带 https)和 with_items 它只会应用最后一个值(在本例中为 https)。
模块需要这样的值。 (带有它在测试中工作的硬编码值..)
containers.podman.podman_container:
name: myapplication
...
ports:
- "8080:9000"
- "127.0.0.1:8081:9001/udp"
如何将我现有的字典转换为模块期望的形式?
循环将在您有项目时多次调用该模块。在调用模块之前使用循环创建端口列表,例如
- name: Calculate list of ports to launch container
vars:
current_port: "{{ item.value.host }}:{{ item.value.container }}"
set_fact:
port_list: "{{ port_list | default([]) + [current_port] }}"
loop: "{{ lookup('dict', ports, wantlist=True) }}"
- name: Run container
containers.podman.podman_container:
name: "{{ container_name }}"
image: "{{ container_image }}"
state: "{{ state }}"
recreate: "{{ recreate }}"
ports: "{{ port_list }}"
环境变量
env_vars:
hostname:
key: HOSTNAME
value: '"host1"'
user:
key: USER
value: '"user1"'
我转换为:
- name: Calculate list of env_vars to launch container
set_fact:
env_vars_dict: "{{ env_vars_dict|default({}) | combine( {item.value.key | upper: item.value.value} ) }}"
loop: "{{ lookup('dict', env_vars, wantlist=True) }}"
我有以下指令:
ports:
http:
host: 81
container: 80
https:
host: 444
container: 443
我想在我的角色中使用它,部分原因是它非常适合此任务的示例(按预期工作)。
- name: ensure container's exposed ports firewall state
tags: firewall
firewalld:
port: "{{ item.value.host }}/tcp"
permanent: yes
immediate: yes
state: enabled
loop: "{{ lookup('dict', ports, wantlist=True) }}"
when: ports is defined
但是在接下来的任务中,循环将不起作用(我尝试启动一个启用了两个端口的容器):
- name: Run container
containers.podman.podman_container:
name: "{{ container_name }}"
image: "{{ container_image }}"
state: "{{ state }}"
recreate: "{{ recreate }}"
ports:
- "{{ item.value.host }}:{{ item.value.container }}"
loop: "{{ lookup('dict', ports, wantlist=True) }}"
如果我使用 loop 语句执行循环,它将启动两个容器(一个带 http,另一个带 https)和 with_items 它只会应用最后一个值(在本例中为 https)。
模块需要这样的值。 (带有它在测试中工作的硬编码值..)
containers.podman.podman_container:
name: myapplication
...
ports:
- "8080:9000"
- "127.0.0.1:8081:9001/udp"
如何将我现有的字典转换为模块期望的形式?
循环将在您有项目时多次调用该模块。在调用模块之前使用循环创建端口列表,例如
- name: Calculate list of ports to launch container
vars:
current_port: "{{ item.value.host }}:{{ item.value.container }}"
set_fact:
port_list: "{{ port_list | default([]) + [current_port] }}"
loop: "{{ lookup('dict', ports, wantlist=True) }}"
- name: Run container
containers.podman.podman_container:
name: "{{ container_name }}"
image: "{{ container_image }}"
state: "{{ state }}"
recreate: "{{ recreate }}"
ports: "{{ port_list }}"
环境变量
env_vars:
hostname:
key: HOSTNAME
value: '"host1"'
user:
key: USER
value: '"user1"'
我转换为:
- name: Calculate list of env_vars to launch container
set_fact:
env_vars_dict: "{{ env_vars_dict|default({}) | combine( {item.value.key | upper: item.value.value} ) }}"
loop: "{{ lookup('dict', env_vars, wantlist=True) }}"