在Ansible中检查文件是否存在

3

我想在ansible中检查一个文件是否存在。

用于检查单个文件的代码,完美运行。

- hosts: all
  tasks:
  - name: Ansible check file exists example.
    stat:
      path: /Users/data/info.text
    register: file_details

  - debug:
      msg: "The file exists"
    when: file_details.stat.exists

我想要检查 /Users/data/info.text 或 /Users/data/info.html 是否存在,我尝试使用或运算符,但它提示 yml 文件无效。

- hosts: all
  tasks:
  - name: Ansible check file exists example.
    stat:
      path: /Users/data/info.text or /Users/data/info.html
    register: file_details

  - debug:
      msg: "The file exists"
    when: file_details.stat.exists
1个回答

1

试试这个

    - stat:
        path: "{{ item }}"
      register: file_details
      loop:
        - info.text
        - info.html

    - debug:
        msg: The files exist
      when: file_details.results|map(attribute='stat.exists') is all

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接