我希望根据以下评估的结果设置一个变量:({{ found_files.files|length > 0 }}),然而,它似乎没有得到充分的评估。可能的问题是什么?
期望的结果:found_files2: /dev/null
结果:
TASK [debug] ********************************************************************************************************************************************************************************
ok: [localhost] => {
"found_files2": "{'files': [], 'changed': False, 'msg': '', 'matched': 0, 'examined': 0, 'failed': False} if (False) else '/dev/null'"
}攻略:
---
- hosts: localhost
gather_facts: no
vars:
backup_dir: /home/user1/projects/ansible/modules/backups/router2
tasks:
- name: get files in backups/<router name>/
delegate_to: localhost
find:
paths: "{{ backup_dir }}"
register: found_files
- set_fact:
found_files2: "{{ found_files }} if ({{ found_files.files|length > 0 }}) else '/dev/null'"
- debug: var=found_files2发布于 2020-04-07 19:06:16
正确的语法如下
- set_fact:
found_files2: "{{ found_files if (found_files.files|length > 0) else '/dev/null' }}"过滤器三元组给出相同的结果
- set_fact:
found_files2: "{{ (found_files.files|length > 0)|ternary(found_files, '/dev/null') }}"https://stackoverflow.com/questions/61076320
复制相似问题