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

hed*_*gie 1 jinja2 ansible

我有以下变量:

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

customers:
  - name: customer1
  - name: customer2
Run Code Online (Sandbox Code Playgroud)

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

- customer1.app1.example.com
- customer2.app1.example.com
- customer1.app2.example.com
- customer2.app2.example.com
Run Code Online (Sandbox Code Playgroud)

通过使用以下 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 %}'
Run Code Online (Sandbox Code Playgroud)

但是,我收到以下错误:

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.
Run Code Online (Sandbox Code Playgroud)

我怎样才能解决这个问题?

Zei*_*tor 5

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

例如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 }}"
Run Code Online (Sandbox Code Playgroud)

这使:

$ 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   

Run Code Online (Sandbox Code Playgroud)

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

$ 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   

Run Code Online (Sandbox Code Playgroud)

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

- 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 }}"
Run Code Online (Sandbox Code Playgroud)

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

- 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 }}"
Run Code Online (Sandbox Code Playgroud)

=>

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
Run Code Online (Sandbox Code Playgroud)