简体   繁体   English

2 Ansible 任务与 Stat 和 include_vars

[英]2 Ansible task with Stat and include_vars

wondering if anyone can help us with this.想知道是否有人可以帮助我们。

The end goal is to refresh the variables back into the play from a YAML file which was generated previously if the file exists.最终目标是将变量从 YAML 文件刷新回游戏中,如果该文件存在,则该文件之前生成。

I'm using stat to check if the files exist and this works - 'name: Registering if file exists'.我正在使用 stat 检查文件是否存在并且这有效 - '名称:如果文件存在则注册'。 If i debug filecheck i can see the 2 entries in the dataset.如果我调试文件检查,我可以在数据集中看到 2 个条目。

Next im trying to reload the vars from the file -final-dataset-file-1 if the file exists using the when condition - filecheck.stat.exists接下来我尝试从文件-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

This is the error i'm seeing这是我看到的错误

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"}

Its like we need to loop through filecheck for each file/item and match these against the files that are a part of the include_vars items but i'm not sure how to do this.就像我们需要遍历每个文件/项目的文件检查并将它们与属于 include_vars 项目的文件相匹配,但我不确定如何执行此操作。

Any comments would be appreciated.任何意见将不胜感激。

Cheers干杯

Iterate the results from the previous task eg迭代上一个任务的结果,例如

    - 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