gpt4 book ai didi

ansible - Ansible剧本中的动态变量名?

转载 作者:行者123 更新时间:2023-12-01 05:04:47 27 4
gpt4 key购买 nike

我有许多带有名称的列表,它们是通过附加 ec2_public_dns_name 创建的至seeds_
像这样:seeds_ec2-50-8-1-43.us-west-1.compute.amazonaws.com
我需要在每个主机的配置中访问它的列表并循环它。
我尝试这样做:

在剧本中分配给新变量:

- name: Seeds provision
set_fact:
seeds: "seeds_{{ec2_public_dns_name}}"

而不是在配置中使用它:
{% for seed in seeds %}
{{seed.name ~ ","}}
{% endfor %}

但似乎 seeds在配置文件中只是文本,我无法访问列表元素。
如何才能做到这一点?

最佳答案

任务:

- set_fact:
seeds: "seeds_{{ec2_public_dns_name}}

创建文本变量 seeds哪个值是 seeds_ec2-50-8-1-43.us-west-1.compute.amazonaws.com .

如果你想要 seeds要成为您将迭代的列表,您需要添加 seeds_{{ec2_public_dns_name}}seeds列表:
- set_fact:
seeds: [] # define seeds as empty list
- set_fact:
seeds: "{{seeds + ['seeds_' + ec2_public_dns_name]}}"

但这将添加到数组 seeds一个元素。可能你有 ec2_public_dns_names这是您的主机的公共(public) DNS 值列表:
ec2_public_dns_names: 
- ec2-50-8-1-43.us-west-1.compute.amazonaws.com
- ec2-50-8-2-43.us-west-1.compute.amazonaws.com
- ...

使用这样的列表,您可以创建具有以下任务的种子列表:
- set_fact:
seeds: "{{seeds + ['seeds_' + item]}}"
with_items: "{{ec2_public_dns_names}}"

关于ansible - Ansible剧本中的动态变量名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30013967/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com