Ansible jinja2 根据多级值操作列表

Ansible jinja2 manipulate lists based on multi level values

我正在尝试在 Ansible 中操作这个列表:

webserver:
  sites:
    - user: userone
      domains:
        - domain: domainone.com
        - domain: domaintwo.com
    - user: usertwo
      domains:
        - domain: domainthree.com
    - user: userthree
      domains:
        - domain: domainfour.com
        - domain: domainfive.com

进入这个:

domains_with_user:
  - domain: domainone.com
    user: userone
  - domain: domaintwo.com
    user: userone
  - domain: domainthree.com
    user: usertwo
  - domain: domainfour.com
    user: userthree
  - domain: domainfive.com
    user: userthree

但随后代码仅基于第一个“网络服务器:”配置。

我该怎么做?我希望能够将它作为一个新的变量 decleration ("domains_with_user": {{ some code }})

您可以使用带有过滤器子元素的循环

---
- hosts: localhost
  gather_facts: false

  vars:
    webserver:
      sites:
        - user: userone
          domains:
            - domain: domainone.com
            - domain: domaintwo.com
        - user: usertwo
          domains:
            - domain: domainthree.com
        - user: userthree
          domains:
            - domain: domainfour.com
            - domain: domainfive.com

  tasks:
    - name: Set domains_with_user variable
      set_fact:
        domains_with_user: "{{ domains_with_user | default([]) + [{ 'domain': item.1.domain , 'user': item.0.user }] }}"
      loop: "{{ webserver.sites | subelements('domains') }}"

    - name: DEBUG
      debug:
        msg: "{{ domains_with_user }}"

和输出:

PLAY [localhost] **********************************************************************************

TASK [Set domains_with_user variable] *************************************************************
ok: [localhost] => (item=[{'user': 'userone', 'domains': [{'domain': 'domainone.com'}, {'domain': 'domaintwo.com'}]}, {'domain': 'domainone.com'}])
ok: [localhost] => (item=[{'user': 'userone', 'domains': [{'domain': 'domainone.com'}, {'domain': 'domaintwo.com'}]}, {'domain': 'domaintwo.com'}])
ok: [localhost] => (item=[{'user': 'usertwo', 'domains': [{'domain': 'domainthree.com'}]}, {'domain': 'domainthree.com'}])
ok: [localhost] => (item=[{'user': 'userthree', 'domains': [{'domain': 'domainfour.com'}, {'domain': 'domainfive.com'}]}, {'domain': 'domainfour.com'}])
ok: [localhost] => (item=[{'user': 'userthree', 'domains': [{'domain': 'domainfour.com'}, {'domain': 'domainfive.com'}]}, {'domain': 'domainfive.com'}])

TASK [DEBUG] **************************************************************************************
ok: [localhost] => {
    "msg": [
        {
            "domain": "domainone.com",
            "user": "userone"
        },
        {
            "domain": "domaintwo.com",
            "user": "userone"
        },
        {
            "domain": "domainthree.com",
            "user": "usertwo"
        },
        {
            "domain": "domainfour.com",
            "user": "userthree"
        },
        {
            "domain": "domainfive.com",
            "user": "userthree"
        }
    ]
}

PLAY RECAP ****************************************************************************************