【问题标题】:Unable to get the list value returned by the json query from the ansible facts无法从 ansible facts 获取 json 查询返回的列表值
【发布时间】:2023-01-22 22:20:50
【问题描述】:

我正在尝试从可靠的事实中获取 /home 文件系统的 size_available 值。

我在设置 gather_facts 后使用以下代码:True

{{ansible_facts['mounts']|json_query('[?mount==`/home`].size_available')}}

通过这种方式,我从调试模块中得到了类似 [34545646] 和 msg: 的信息。 我需要将此值与静态值进行比较,然后继续或不继续该剧本,但是当我尝试时:

{{ansible_facts['mounts']|json_query('[?mount==`/home`].size_available')[0]}}

我得到:

"msg": "template error while templating string: expected token 'end of print statement', got '['. String: > {{ansible_facts['mounts']|json_query('[?mount==`/home`].size_available')[0]}}

即使 type_debug 向我显示结果确实应该是 [0] 扩展名应该可以访问的列表。

【问题讨论】:

    标签: ansible ansible-facts


    【解决方案1】:

    在尝试索引结果之前,您需要将初始表达式括起来,如下所示:

    {{
      (
        ansible_facts['mounts'] |
        json_query('[?mount==`/home`].size_available')
      )[0]
    }}
    

    例如,如果我在我的系统上运行这个剧本:

    - hosts: localhost
      gather_facts: true
      tasks:
        - debug:
            msg: >-
              {{
                (
                  ansible_facts['mounts'] |
                  json_query('[?mount==`/home`].size_available')
                )[0]
              }}
    

    我得到调试任务的输出:

    TASK [debug] ********************************************************************************************
    ok: [localhost] => {
        "msg": "402658955264"
    }
    

    【讨论】:

      猜你喜欢
      • 2012-08-16
      • 2017-04-18
      • 2017-02-08
      • 2020-12-07
      • 2023-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多