如何检查Ansible中是否存在文件?

如何检查Ansible中是否存在文件?,ansible,Ansible,我必须检查/etc/中是否存在文件。如果文件存在,则我必须跳过该任务。 以下是我正在使用的代码: - name: checking the file exists command: touch file.txt when: $(! -s /etc/file.txt) 一般来说,您可以使用。但是有创建选项,这使得操作非常简单: - name: touch file command: touch /etc/file.txt args: creates: /etc/file.

我必须检查
/etc/
中是否存在文件。如果文件存在,则我必须跳过该任务。 以下是我正在使用的代码:

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

一般来说,您可以使用。但是有
创建
选项,这使得操作非常简单:

- name: touch file
  command: touch /etc/file.txt
  args:
    creates: /etc/file.txt
我想你的触控命令只是一个例子?最佳实践是根本不检查任何东西,让ansible用正确的模块完成它的工作。因此,如果要确保文件存在,请使用文件模块:

- name: make sure file exists
  file:
    path: /etc/file.txt
    state: touch
该模块将完成这项工作,并获取文件的许多其他信息。从示例文档中:

- stat: path=/path/to/something
  register: p

- debug: msg="Path exists and is a directory"
  when: p.stat.isdir is defined and p.stat.isdir

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

任务:
-名称:检查somefile.conf是否存在
斯达:
路径:/etc/file.txt
寄存器:统计结果
-名称:如果文件不存在,则创建该文件
文件:
路径:/etc/file.txt
状态:触摸
当:not stat_result.stat.存在时

这可以通过stat模块在文件存在时跳过任务来实现

- hosts: servers
  tasks:
  - name: Ansible check file exists.
    stat:
      path: /etc/issue
    register: p
  - debug:
      msg: "File exists..."
    when: p.stat.exists
  - debug:
      msg: "File not found"
    when: p.stat.exists == False

我发现做很多这样的
.stat.exists
类型检查会很烦人,而且容易出错。例如,它们需要格外小心才能使检查模式(
--check
)正常工作

这里有许多答案表明

  • 获取并注册
  • 当寄存器表达式为true时应用
然而,有时这是一种代码味道,所以总是寻找更好的方法来使用Ansible,特别是使用正确的模块有很多优点。e、 g

- name: install ntpdate
  package:
    name: ntpdate

但是,当无法使用一个模块时,还要调查是否可以注册并检查上一个任务的结果。e、 g

# jmeter_version: 4.0 
- name: Download Jmeter archive
  get_url:
    url: "http://archive.apache.org/dist/jmeter/binaries/apache-jmeter-{{ jmeter_version }}.tgz"
    dest: "/opt/jmeter/apache-jmeter-{{ jmeter_version }}.tgz"
    checksum: sha512:eee7d68bd1f7e7b269fabaf8f09821697165518b112a979a25c5f128c4de8ca6ad12d3b20cd9380a2b53ca52762b4c4979e564a8c2ff37196692fbd217f1e343
  register: download_result

- name: Extract apache-jmeter
  unarchive:
    src: "/opt/jmeter/apache-jmeter-{{ jmeter_version }}.tgz"
    dest: "/opt/jmeter/"
    remote_src: yes
    creates: "/opt/jmeter/apache-jmeter-{{ jmeter_version }}"
  when: download_result.state == 'file'
注意:时的
,但也注意
创建:
所以
--检查
不会出错

我之所以提到这一点,是因为这些不太理想的做法往往是成对出现的,即没有apt/yum软件包,因此我们必须1)下载并2)解压缩


希望这有帮助

发现调用
stat
很慢,并且收集了很多文件存在性检查不需要的信息。
在花了一些时间搜索解决方案后,我发现了以下解决方案,其运行速度要快得多:

- raw: test -e /path/to/something && echo true || echo false
  register: file_exists

- debug: msg="Path exists"
  when: file_exists == true
**

如何使用when条件检查Ansible中是否存在文件 **

下面是当文件存在于OS端时,我用来删除该文件的ansible play

 - name: find out /etc/init.d/splunk file exists or not'
      stat:
        path: /etc/init.d/splunk
      register: splunkresult
      tags:
        - always

    - name: 'Remove splunk from init.d file if splunk already running'
      file:
        path: /etc/init.d/splunk
        state: absent
      when: splunkresult.stat.exists == true
      ignore_errors: yes
      tags:
        - always
我使用的游戏条件如下所示

when: splunkresult.stat.exists == true --> Remove the file
你可以根据你的要求给出真/假

when: splunkresult.stat.exists == false
when: splunkresult.stat.exists == true
您可以使用Ansible模块注册文件,并在模块应用条件时使用Ansible模块

- name: Register file
      stat:
        path: "/tmp/test_file"
      register: file_path

- name: Create file if it doesn't exists
      file: 
        path: "/tmp/test_file"
        state: touch
      when: file_path.stat.exists == False

关于相对路径的注释,以补充其他答案

当以代码形式执行基础架构时,我通常使用接受相对路径的角色和任务,特别是那些角色中定义的文件


像playbook_dir和role_path一样,创建测试存在性所需的绝对路径非常有用。

我使用这段代码,它可以很好地用于文件夹和文件。只需确保文件夹名称后没有尾随空格。如果文件夹存在,则文件_exists.stdout将为“true”,否则它将只是一个空字符串“”


您可以使用shell命令检查文件是否存在

  - set_fact:
    file: "/tmp/test_file"

  - name: check file exists
    shell: "ls {{ file }}"
    register: file_path
    ignore_errors: true

  - name: create file if don't exist
    shell: "touch {{ file }}"
    when: file_path.rc != 0

如果该目录不存在怎么办?如果该目录不存在,则寄存器
stat\u result
将有一个
stat\u result.state.exists
为False(此时第二个任务运行)。您可以在这里看到stat模块的详细信息:何时定义stat_result.stat.exists,何时定义stat_result.stat.existhanks。另外,如果你想让它读得更自然,你可以把:
when:stat\u result.stat.exists==False
写到
when:not stat\u result.stat.exists
如果你想让它读得更自然。你能用“not Foo”代替“Foo==False”吗?
状态:file
不创建文件。我会考虑这个问题的第一个例子,根据用户问题的具体情况、用户使用命令模块和ANUTE最佳实践来回答这个问题。注意,您可以通过添加<代码>修改子句时间=保存Access时间=保存< /代码>来生成这个幂等元。这绝对是最简洁、最正确的方法。你不解释你的答案,在这种情况下,
当:mypath不存在时意味着什么?
mypath
不是一个简单的字符串吗?模板在这里帮助验证路径。更多示例请注意,这将检查控制器上的文件,而不是遥控器上的文件。请在回答中说明问题所在,以及此代码片段将如何解决问题,以帮助其他人理解此答案。这对于防止ansible的Bursted get_url每次重新下载20GB下载非常有用,在某些情况下会发生这种情况。统计速度非常慢。看起来在最新版本中,当使用“全文比较”时会出现警告,例如“是”等而不是“=”、“”等。
- name: Register file
      stat:
        path: "/tmp/test_file"
      register: file_path

- name: Create file if it doesn't exists
      file: 
        path: "/tmp/test_file"
        state: touch
      when: file_path.stat.exists == False
- name: check filesystem existence
  shell: if [[ -d  "/folder_name" ]]; then echo "true";  fi
  register: file_exists
  
- name: debug data
  debug: 
    msg: "Folder exists"
  when: file_exists.stdout == "true"
  - set_fact:
    file: "/tmp/test_file"

  - name: check file exists
    shell: "ls {{ file }}"
    register: file_path
    ignore_errors: true

  - name: create file if don't exist
    shell: "touch {{ file }}"
    when: file_path.rc != 0