gpt4 book ai didi

yaml - 如何使用 Ansible 遍历嵌套的 dict 结构?

转载 作者:行者123 更新时间:2023-12-04 13:38:21 26 4
gpt4 key购买 nike

我在 ansible playbook 中有以下 dict 结构变量:

apache_vhosts:
- name: foo
server_name: foo.com
server_aliases:
- a.foo.com
- b.foo.com
- c.foo.com
- name: bar
server_name: bar.com
server_aliases:
- d.bar.com
- e.bar.com
- f.bar.com

我需要为每个 server_name 创建一个符号链接(symbolic link)和 server_aliases域,例如:
/tmp/foo.com     ->   /var/www/foo
/tmp/a.foo.com -> /var/www/foo
/tmp/b.foo.com -> /var/www/foo
/tmp/c.foo.com -> /var/www/foo
/tmp/bar.com -> /var/www/bar
/tmp/d.bar.com -> /var/www/bar
/tmp/e.bar.com -> /var/www/bar
/tmp/f.bar.com -> /var/www/bar

我有以下适用于 server_name 的任务:
- name: Add a domain symlinks /tmp for server_name.
file:
src: "/var/www/{{ item.name }}"
dest: "/tmp/{{ item.server_name }}"
state: link
with_items: apache_vhosts

但我不确定如何对 server_aliases 的数组做同样的事情.

如有必要,我很乐意使用两个单独的任务,但我希望避免添加单独的 domains在平面结构中复制域列表的变量。

这个 Google Groups post很接近,但我不知道如何使它适用于多个虚拟主机。

这可能吗?或者这根本上是错误的方法?

最佳答案

您可以使用 with_subelements 循环遍历 server_aliases。下面的片段

 - name: Add a domain symlinks /tmp for server_name.
debug: msg="{{ item.server_name }}"
with_items: apache_vhosts
- name: Add a domain symlinks /tmp for server_aliases.
debug: msg="name - {{ item.0.name }} and serverAlias - {{ item.1 }}"
with_subelements:
- apache_vhosts
- server_aliases

关于yaml - 如何使用 Ansible 遍历嵌套的 dict 结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30869519/

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