在任务中检索到set_fact多个事实会导致剧本运行发出错误



在剧本中使用set_fact检索多个事实会导致错误

我尝试了单独的set_fact任务,每个事实检索 1 个。也得到了错误。 当我在set_fact下定义了 3 个事实时,即当我包含mountsize_tmp时,似乎会发生。当我只有前 2 个事实时没有错误。 是否需要在 var 变量部分中定义 set_fact 中使用的变量名称?

错误是: 违规行似乎是: set_fact: ^ 此处异常类型: 异常: 没有第一项,序列为空。

- set_fact:
alto_seal: "{{ ansible_local.alto_bootstrap.seal }}"  # local fact
mountsize: "{{ ansible_mounts | selectattr('mount', 'equalto', '/abc') | map(attribute='size_total') | first }}"     # ansible fact
mountsize_tmp: "{{ ansible_mounts | selectattr('mount', 'equalto', '/tmp') | map(attribute='size_total') | first }}"
- debug:
msg: "{{ 'Alto start mountsize ' ~ mountsize }}"  
- debug:
msg: "{{'Seal  ' ~ alto_seal }}"

期望文件的值挂载自定义事实文件中的大小和事实显示在 2 行中

为了避免异常,您可以通过以下方式进行管理:

  1. 定义默认值:
- set_fact:
alto_seal: "{{ ansible_local.alto_bootstrap.seal }}"  # local fact
- set_fact:
mountsize: "<DEFAULT_VALUE_OF_YOUR_CHOICE>"
mountsize_tmp: "<DEFAULT_VALUE_OF_YOUR_CHOICE>"
- set_fact:
mountsize: "{{ ansible_mounts | selectattr('mount', 'equalto', '/abc') | map(attribute='size_total') | first }}"     # ansible fact
when: ansible_mounts | selectattr('mount', 'equalto', '/abc') | map(attribute='size_total') | list != []
- set_fact:
mountsize_tmp: "{{ ansible_mounts | selectattr('mount', 'equalto', '/tmp') | map(attribute='size_total') | first }}"
when: ansible_mounts | selectattr('mount', 'equalto', '/tmp') | map(attribute='size_total') | list != []

  1. 发送特定消息:
- set_fact:
alto_seal: "{{ ansible_local.alto_bootstrap.seal }}"  # local fact
- set_fact:
mountsize: "{{ ansible_mounts | selectattr('mount', 'equalto', '/abc') | map(attribute='size_total') | first }}"     # ansible fact
msg: "{{ 'Alto start mountsize ' ~ mountsize }}"
when: ansible_mounts | selectattr('mount', 'equalto', '/abc') | map(attribute='size_total') | list != []
- set_fact:
msg: "Alto start mountsize cannot be calculated"
when: ansible_mounts | selectattr('mount', 'equalto', '/abc') | map(attribute='size_total') | list == []
- debug:
msg: "{{ msg }}"

最新更新