,ansible,assert,stat"/>

首页 > 解决方案 > Ansible 断言模块失败并显示“检测到无效条件:无效语法 (, 第 1 行)"

问题描述

我正在处理一个角色,该角色在某个时候会在现有路径上创建一些新目录,其中第一个目录名为“DB2_patching”。因为多个用户需要能够访问这些目录,所以我想确保我的“上方”目录都可以为其他用户执行。

我的计划是这样的:

  1. 使用“DB2_patching”分割路径;因此我有我需要检查的目录
  2. 在分隔符“/”上再次拆分缩短的路径
  3. 使用 join 重新创建路径,同时使用 stat 获取必要的信息
  4. 做一个最后的断言,看看是否一切正常

好吧,这个最后的断言失败了:

"{"msg": "条件检查 'result_join.results.0.stat.xoth' 失败。错误是:检测到无效条件:无效语法(第 1 行)“}”

断言任务看起来像这样,我试图循环一个变量,其中“项目”也是变量。这是“result_join.results.{{ item }}.stat.xoth”。

- name: check execute for others
  assert:
    that:
      result_join.results.{{ item }}.stat.xoth
    success_msg: "Directory has execute permission for others."
    fail_msg: "Directory doesn't have execute permission for others!"
  with_sequence: start=0 end={{ nr_entries }}

看起来,当我在循环中使用“result_join.results.{{ item }}.stat.xoth”时,有些事情搞砸了。我怎样才能使这项工作?

标签: ansibleassertstat

解决方案


请参阅引用键:值字典变量。引用属性

    that:
      result_join.results[item].stat.xoth

例如

- hosts: localhost
  vars:
    result_join:
      results:
        item1:
          stat:
            xoth: true
        item2:
          stat:
            xoth: false
  tasks:
    - debug:
        var: result_join.results[item].stat.xoth
      loop:
        - item1
        - item2

TASK [debug] ****
ok: [localhost] => (item=item1) => 
  ansible_loop_var: item
  item: item1
  result_join.results[item].stat.xoth: true
ok: [localhost] => (item=item2) => 
  ansible_loop_var: item
  item: item2
  result_join.results[item].stat.xoth: false

可以显示变量或消息。见调试

    - debug:
        var: result_join.results[item].stat.xoth
      loop: [item1, item2]
    - debug:
        msg: "{{ result_join.results[item].stat.xoth }}"
      loop: [item1, item2]

推荐阅读