【问题标题】:Ansible custom nested loops with subelements带有子元素的 Ansible 自定义嵌套循环
【发布时间】:2016-01-25 15:37:53
【问题描述】:

这是我的vars/main.yml 文件中简短而富有表现力的数据结构:

webapp_dirs:
  - /var/www/example.com
  - /var/www/test.example.ru

webapp_acls:
  - dirs:
    - app
    - web
    - src
    - vendor
    perm: rX
    recursive: yes
  - dirs:
    - app/cache
    - app/logs
    perm: rwX
    recursive: no

我想遍历第一个列表,然后遍历第二个列表,最后遍历第二个列表中的嵌套列表:

for top_dir in webapp_dirs:
    for acl in webapp_acls:
        for dir in i.dirs:
               ....

我有with_subelementswith_nested,但它们不能合并。有什么办法可以在 ansible 中做我想做的事吗?

UPD

@chrism 给了我一个好主意,但我无法使外循环工作:

- include: app-permissions.yml
  vars:
    webapp_dir: "{{ item }}"
  with_items:
    - /var/www/example.com

app-permissions.yml 包含变量的内部循环。这失败并出现错误{"failed": true, "msg": "ERROR! an unexpected type error occurred. Error was can only concatenate list (not \"str\") to list"}

【问题讨论】:

  • 请将此问题移至serverfault.com,我将它发布到了错误的站点,并且这里没有足够的声誉来自己移动它。
  • 我认为在 SO 上没问题。大多数 Ansible 问题到此结束。此外,任何可能的答案都最适合 SO,因为您无法使用 Ansible 的内置循环来做到这一点。为此,您需要create your own lookup plugin

标签: ansible


【解决方案1】:

在 ansible 2.0 中,您可以通过 - include playbook.yml 进行嵌套循环。其中外循环包括playbook.yml,内循环在playbook.yml

【讨论】:

  • 好主意,但我无法使外循环工作,请参阅问题更新
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-19
  • 1970-01-01
相关资源
最近更新 更多