繁体   English   中英

2 Ansible 任务与 Stat 和 include_vars

[英]2 Ansible task with Stat and include_vars

想知道是否有人可以帮助我们。

最终目标是将变量从 YAML 文件刷新回游戏中,如果该文件存在,则该文件之前生成。

我正在使用 stat 检查文件是否存在并且这有效 - '名称:如果文件存在则注册'。 如果我调试文件检查,我可以在数据集中看到 2 个条目。

接下来我尝试从文件-final-dataset-file-1重新加载变量,如果文件存在,则使用 when 条件 - filecheck.stat.exists

- name: Registering if file exists
  stat:
    path: "{{ inventory_dir }}/host_vars/{{ inventory_hostname }}/final-dataset-{{ item }}.yml"
  loop:
    - file-1
    - file-2
  register: filecheck

- name: IOS - Refreshing final-dataset variables generated by the above tasks in order to service any subsequent tasks
  include_vars:
    file: "{{ inventory_dir }}/host_vars/{{ inventory_hostname }}/final-dataset-{{ item }}.yml"
  loop:
    - file-1
    - file-2
  when: filecheck.stat.exists

这是我看到的错误

fatal: [router]: FAILED! => {"msg": "The conditional check 'filecheck.stat.exists' failed. The error was: error while evaluating conditional (filecheck.stat.exists): 'dict object' has no attribute 'stat'\n\nThe error appears to be in '/ansible/roles/roles_nane/tasks/report.yml': line 94, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: IOS - Refreshing final-dataset variables generated by the above tasks in order to service any subsequent tasks\n  ^ here\n"}

就像我们需要遍历每个文件/项目的文件检查并将它们与属于 include_vars 项目的文件相匹配,但我不确定如何执行此操作。

任何意见将不胜感激。

干杯

迭代上一个任务的结果,例如

    - include_vars: "{{ item.stat.path }}"
      loop: "{{ filecheck.results }}"
      when: item.stat.exists

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM