如何检查文件是否存在于 Ansible 中? [英] How to check if a file exists in Ansible?

查看:39
本文介绍了如何检查文件是否存在于 Ansible 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须检查 /etc/ 中是否存在文件.如果文件存在,那么我必须跳过该任务.这是我正在使用的代码:

I have to check whether a file exists in /etc/. If the file exists then I have to skip the task. Here is the code I am using:

- name: checking the file exists
  command: touch file.txt
  when: $(! -s /etc/file.txt)

推荐答案

您可以先检查目标文件是否存在,然后根据其结果的输出做出决定:

You can first check that the destination file exists or not and then make a decision based on the output of its result:

    tasks:
      - name: Check that the somefile.conf exists
        stat:
          path: /etc/file.txt
        register: stat_result

      - name: Create the file, if it doesnt exist already
        file:
          path: /etc/file.txt
          state: touch
        when: not stat_result.stat.exists

这篇关于如何检查文件是否存在于 Ansible 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆