使用循环拼接字符串,item未定义

Use loop to splice string, item is undefined

下面是我的任务代码:

- name: Add calculation nodes of different groups to the list
  set_fact: 
    list_of_all_calculate_node: "{{  list_of_all_calculate_node | default([]) + ['random_calculate_host' + ('{{ item }}' | string ) ]  }}"
  loop: "{{ range(1,5) }}"

- name: Print  list_of_all_calculate_node
  debug:
    var: "{{ item }}"
  loop: "{{ list_of_all_calculate_node }}"

错误类型:致命

TASK [Print  list_of_all_calculate_node]
fatal: [*.*.*.*]: FAILED! => {"msg": "['random_calculate_host2', 'random_calculate_host3', 'random_calculate_host4', 'random_calculate_host{{ item }}']: 'item' is undefined"}

预期的输出应该是: ['random_calculate_host1','random_calculate_host2', 'random_calculate_host3', 'random_calculate_host4']

在 Ansible 中,花括号 {{ }} 不能嵌套。修复语法,例如

    - set_fact:
        l: "{{  l|d([]) + ['random_calculate_host' ~ item] }}"
      loop: "{{ range(1,5) }}"

给予

  l:
  - random_calculate_host1
  - random_calculate_host2
  - random_calculate_host3
  - random_calculate_host4

您不必重复 范围,例如下面的任务给出了相同的结果

    - set_fact:
        l: "{{ ['random_calculate_host']|product(range(1,5))|map('join')|list }}"