如何在ansible中检查文件?

时间:2016-02-26 14:28:20

标签: ansible

我必须检查- name: checking the file exists command: touch file.txt when: $(! -s /etc/file.txt) 中是否存在文件。如果该文件存在,那么我必须跳过该任务。 这是我正在使用的代码:

file.txt

如果<ul class="ui-autocomplete ui-front ui-menu ui-widget ui-widget-content ui-corner-all"> 存在,那么我必须跳过任务。

13 个答案:

答案 0 :(得分:118)

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

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: stat_result.stat.exists == False 

答案 1 :(得分:20)

stat模块将执行此操作以及获取文件的许多其他信息。从示例文档:

{
    "name": "miteshmap/commerce",
    "type": "my-repo",
    "require": {
        "composer/installers": "~1.0"
    }
}

答案 2 :(得分:15)

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

[branch "1"]

答案 3 :(得分:11)

一般情况下,您会使用stat module执行此操作。但是command modulecreates选项,这使得这很简单:

- 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

答案 4 :(得分:3)

vars:
  mypath: "/etc/file.txt"

tasks:
  - name: checking the file exists
    command: touch file.txt
    when: mypath is not exists

答案 5 :(得分:2)

I find it can be annoying and error prone to do a lot of these .stat.exists type checks. For example they require extra care to get check mode (--check) working.

Many answers here suggest

  • get and register
  • apply when register expression is true

However, sometimes this is a code smell so always look for better ways to use Ansible, specifically there are many advantages to using the correct module. e.g.

- name: install ntpdate
  package:
    name: ntpdate

or

- file:
    path: /etc/file.txt
    owner: root
    group: root
    mode: 0644

But when it is not possible use one module, also investigate if you can register and check the result of a previous task. 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'

Note the when: but also the creates: so --check doesn't error out

I mention this because often these less-than-ideal practices come in pairs i.e. no apt/yum package so we have to 1) download and 2) unzip

Hope this helps

答案 6 :(得分:1)

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

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

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

答案 7 :(得分:1)

您可以使用Ansible stat模块注册文件,以及何时应用条件。

- 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

答案 8 :(得分:0)

**

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

**

下面是当文件存在于操作系统端时我用来删除文件的ansible播放。

 - 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

答案 9 :(得分:0)

如果您只想确保某个文件存在(例如,因为应该使用与ansible不同的方式创建文件),并且如果没有则失败,则可以执行以下操作:

- name: sanity check that /some/path/file exists
  command: stat /some/path/file
  check_mode: no # always run
  changed_when: false # doesn't change anything

答案 10 :(得分:0)

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

在将基础结构用作代码时,我通常使用的角色和任务接受相对路径,特别是针对那些角色中定义的文件。

Special variables(例如playbook_dir和role_path)对于创建测试存在性所需的绝对路径非常有用。

答案 11 :(得分:0)

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

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

答案 12 :(得分:0)

你可以使用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