ansible - Ansible 断言模块失败并显示“检测到无效条件:无效语法 (, 第 1 行)"
问题描述
我正在处理一个角色,该角色在某个时候会在现有路径上创建一些新目录,其中第一个目录名为“DB2_patching”。因为多个用户需要能够访问这些目录,所以我想确保我的“上方”目录都可以为其他用户执行。
我的计划是这样的:
- 使用“DB2_patching”分割路径;因此我有我需要检查的目录
- 在分隔符“/”上再次拆分缩短的路径
- 使用 join 重新创建路径,同时使用 stat 获取必要的信息
- 做一个最后的断言,看看是否一切正常
好吧,这个最后的断言失败了:
"{"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 中提取值;例如使用 ""{{ result_join.results.0.stat.path }}" 进行调试。
看起来,当我在循环中使用“result_join.results.{{ item }}.stat.xoth”时,有些事情搞砸了。我怎样才能使这项工作?
解决方案
请参阅引用键:值字典变量。引用属性
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]
推荐阅读
- linux - Linux,如何查找最近的文件(在过去 3 天内修改但不是在过去 24 小时内修改)?
- python - 如何为 django 项目修改多个数据库的 docker 设置?
- android - 错误“支持的最低 Gradle 版本为 7.0.2。当前版本为 6.8。” 在 Android Studio 中
- database - 重新启动 TDengine 时无法创建计时器控制器
- python - 是否有多维 ECDF 的 python 实现?
- javascript - react-dnd 文档如何显示 `dragPreview` 与 `useDrag` 钩子一起使用
- python - 在 tweet-nacl (javascript) 中加密并在 python 中解密
- javascript - 如何在 Firefox 控制台中“单击”所有 div?
- python - 如何在 Pandas 中使用 lambda 函数将行索引添加到列?
- flutter - Flutter,动态设置定位的小部件