Ansible断言模块因“检测到无效的条件:语法无效<未知>,第1行”而失败

问题描述

我正在研究一个角色,该角色有时会在已经存在的路径上创建一些新目录,第一个目录名为“ DB2_patching”。因为多个用户需要能够访问这些目录,所以我想确保所有位于我上面的目录都可以为其他目录执行。

我的计划是这样

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

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

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

assert任务看起来像这样,我试图循环一个变量,其中“ item”也是变量。这是“ 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中提取值;例如,在调试时使用“ {{result_join.results.0.stat.path}}”。

貌似,当我在循环中使用“ result_join.results。{{item}}。stat.xoth”时,有些事情搞砸了。我该如何工作?

解决方法

请参见Referencing key:value dictionary variables。引用属性

    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

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