如何用 Ansible/Jinja2 将字符串拆分为列表?

How to split a string into a list with Ansible/Jinja2?

我有以下变量:

domain_names:
  - app1.example.com
  - app2.example.com

customers:
  - name: customer1
  - name: customer2

我正在尝试生成以下域名列表:

- customer1.app1.example.com
- customer2.app1.example.com
- customer1.app2.example.com
- customer2.app2.example.com

使用以下 Ansible/Jinja2 代码:

- name: check which certificates exist
  stat:
    path: '/etc/nginx/{{item}}.crt'
  register: cert_file
  loop: '{% for d in domain_names %}{{ d }} {% for customer in customers %}{{ customer.name }}.{{ d }} {% endfor %}{% endfor %}'

但是,我收到以下错误:

failed | msg: Invalid data passed to 'loop', it requires a list, got this instead: customer1.app1.example.com customer2.app1.example.com customer1.app2.example.com customer2.app2.example.com. Hint: If you passed a list/dict of just one element, try adding wantlist=True to your lookup invocation or use q/query instead of lookup.

我该如何解决这个问题?

只需使用正确的工具 :)。在这种情况下,您的朋友是:

例如test.yml 剧本:

---
- name: product and map filters demo
  hosts: localhost
  gather_facts: false
  
  vars:
    domain_names:
      - app1.example.com
      - app2.example.com

    customers:
      - name: customer1
      - name: customer2

  tasks:
    - name: Demonstrate product and map filters use
      debug:
        msg: "{{ item.0 }}.{{ item.1 }}"
      loop: "{{ customers | map(attribute='name') | product(domain_names) | list }}"

给出:

$ ansible-playbook test.yml 

PLAY [product and map filters demo] *******************************************************************************************************************************************************************************

TASK [Demonstrate product and map filters use] ********************************************************************************************************************************************************************
ok: [localhost] => (item=['customer1', 'app1.example.com']) => {
    "msg": "customer1.app1.example.com"
}
ok: [localhost] => (item=['customer1', 'app2.example.com']) => {
    "msg": "customer1.app2.example.com"
}
ok: [localhost] => (item=['customer2', 'app1.example.com']) => {
    "msg": "customer2.app1.example.com"
}
ok: [localhost] => (item=['customer2', 'app2.example.com']) => {
    "msg": "customer2.app2.example.com"
}

PLAY RECAP ********************************************************************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

应用于你的任务,这给出:

- name: Check which certificates exist
  stat:
    path: '/etc/nginx/{{ item.0 }}.{{ item.1 }}.crt'
  register: cert_file
  loop: "{{ customers | map(attribute='name') | product(domain_names) | list }}"

如果您真的想要 re-use 该列表,您可以将其构建到另一个变量中。一种简单的理解方法是将其构建在 set_fact 任务中,例如

- name: Create my application names list
  vars:
    current_name: "{{ item.0 }}.{{ item.1 }}"
  set_fact:
    application_names_list: "{{ application_names_list | default([]) + [current_name] }}"
  loop: "{{ customers | map(attribute='name') | product(domain_names) | list }}"

- name: Check which certificates exist
  stat:
    path: '/etc/nginx/{{ item }}.crt'
  register: cert_file
  loop: "{{ application_names_list }}"

但您也可以在您的 vars 中使用更复杂的表达式“静态”声明它(请参阅 map 过滤器可能性和 join 过滤器)

---
- name: product and map filters demo
  hosts: localhost
  gather_facts: false

  vars:
    domain_names:
      - app1.example.com
      - app2.example.com

    customers:
      - name: customer1
      - name: customer2

    application_names_list: "{{ customers | map(attribute='name') | product(domain_names) | map('join', '.') | list }}"

  tasks:
    - name: Demonstrate product and map filters use
      debug:
        var: application_names_list

=>

PLAY [product and map filters demo] ****************************************************************************************************************************************************************************************************

TASK [Demonstrate product and map filters use] *****************************************************************************************************************************************************************************************
ok: [localhost] => {
    "all_domains": [
        "customer1.app1.example.com",
        "customer1.app2.example.com",
        "customer2.app1.example.com",
        "customer2.app2.example.com"
    ]
}

PLAY RECAP *****************************************************************************************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0