簡體   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