Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ansible 条件concat jinja2列表_Ansible_Jinja2 - Fatal编程技术网

Ansible 条件concat jinja2列表

Ansible 条件concat jinja2列表,ansible,jinja2,Ansible,Jinja2,ansible角色中使用的某些数据: list1: - foo - bar list2: # sometimes this is empty 此任务失败: - name: hello somemodule: dosomething: "{{ list1 + list2 }}" 错误: 致命:[localhost]:失败!=>{“msg”:“在({list1+list2}})上发生意外的模板类型错误:只能将列表(而不是\“NoneType\”)连接到

ansible角色中使用的某些数据:

list1:
  - foo
  - bar
list2:             # sometimes this is empty
此任务失败:

- name: hello
  somemodule:
    dosomething: "{{ list1 + list2 }}"
错误:

致命:[localhost]:失败!=>{“msg”:“在({list1+list2}})上发生意外的模板类型错误:只能将列表(而不是\“NoneType\”)连接到列表“}”

只有当列表2不为空时,才有条件连接的方法吗?

您可以将bulitin测试与an结合使用:

- debug:
    msg: "{{ list1 + (list2 if list2 is not none else []) }}" 
考虑到剧本

- hosts: local
  gather_facts: no
  vars:
    list1:
      - foo
      - bar
    list2:

  tasks:
    - debug:
        msg: "{{ list1 + (list2 if list2 is not none else [])  }}"
一个剧本将导致以下重述:

PLAY [local] **************************************************************************************

TASK [debug] **************************************************************************************
ok: [local] => {
    "msg": [
        "foo",
        "bar"
    ]
}

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

除非
list2
完全未定义,否则这将不起作用。在当前OP的示例中,它仍然会触发一个错误。您需要完全删除该声明,或者将其变成一个空列表(即,
list2:[]
)是的,我的坏消息,我认为未定义会抓住它。我有一个编辑来了,恰巧是最好的;)好吧,虽然您的编辑在技术上是绝对正确的,但我仍然强烈建议OP使用
default
,而不声明列表,并且详细地将空列表声明为。。。。空列表,而不是作为无/空字符串。@Zeitounator您的
list2:[]
方法的优点是,上面的条件检查变得不必要了。