首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >条件set_fact不求值

条件set_fact不求值
EN

Stack Overflow用户
提问于 2020-04-07 16:58:41
回答 1查看 55关注 0票数 1

我希望根据以下评估的结果设置一个变量:({{ found_files.files|length > 0 }}),然而,它似乎没有得到充分的评估。可能的问题是什么?

期望的结果:found_files2: /dev/null

结果:

代码语言:javascript
复制
TASK [debug] ********************************************************************************************************************************************************************************
ok: [localhost] => {
    "found_files2": "{'files': [], 'changed': False, 'msg': '', 'matched': 0, 'examined': 0, 'failed': False} if (False) else '/dev/null'"
}

攻略:

代码语言:javascript
复制
---
- 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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-07 19:06:16

正确的语法如下

代码语言:javascript
复制
- set_fact:
    found_files2: "{{ found_files if (found_files.files|length > 0) else '/dev/null' }}"

过滤器三元组给出相同的结果

代码语言:javascript
复制
- set_fact:                                                                                         
    found_files2: "{{ (found_files.files|length > 0)|ternary(found_files, '/dev/null') }}"
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61076320

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档