2 Ansible 任务与 Stat 和 include_vars

2 Ansible task with Stat and include_vars

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

最终目标是从之前生成的 YAML 文件中刷新变量,如果该文件存在的话。

我正在使用 stat 检查文件是否存在并且这有效 - 'name: Registering if file exists'。如果我调试文件检查,我可以看到数据集中的 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"}

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

如有任何意见,我们将不胜感激。

干杯

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

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