如何在Ansible playbooks中使用stat模块

1,057 阅读3分钟

在Linux中,stat命令是一个命令行工具,提供关于文件系统或文件的详细信息。

Ansible,被认为是最多样化和最流行的自动化工具,提供了一个模块来获取文件和文件系统信息,作为原生的Linux stat命令。

在本指南中,我们将了解如何在Ansible playbooks中使用stat模块。

检查文件是否存在

stat模块将获取指定文件或目录的信息,并使用register参数将其保存。

在下面的例子中,我们检查/var/log/alternatives.log这个文件是否存在。

---
- name: ansible stat module
  hosts: all
  become: yes
  tasks:
  - name: check alternatives.log
    stat:
      path: /var/log/alternatives.log
    register: info
  - name: tell if the file is there
    debug:
      msg: file exists
    when: info.stat.exists
  - name: tell if file missing
    debug:
      msg: the file missing
    when: not info.stat.exists

在上面的例子中,我们调用stat模块来收集远程主机上的/var/log/alternatives.log文件的信息。

一旦我们获取了文件信息,我们就把它保存到一个寄存器file_info中。

为了确保我们能够读取该文件,我们将become参数设置为true。

在第二个任务中,我们使用info寄存器来检查文件是否存在。如果为真,我们显示一条信息,表明目标文件存在。

如果文件在远程主机上不存在,最后一个任务返回一个通知。如果info.stat.existence为false,这就很方便了。

保存并运行playbook。

ansible-playbook play checkfile.yml

下面是一个输出的例子。

输出显示目标文件在远程主机上存在。

检查一个目录是否存在

使用stat模块检查一个目录是否存在的playbook与上面的类似。然而,我们提供一个目标目录的路径,如下所示。

---
- name: ansible stat module
  hosts: all
  become: yes
  tasks:
  - name: check log directory
    stat:
      path: /var/log/
    register: dir_info
  - name: tell if directory exists
    debug:
      msg:  target directory exists
    when: dir_info.stat.exists
  - name: tell if dir is missing
    debug:
      msg: directory is missing
    when: not dir_info.stat.exists

一旦我们运行playbook,我们应该看到一个类似于下面所示的输出。

ansible-playbook direxists.yml

检查一个用户是否拥有一个文件

ansible stat模块为指定的文件或目录返回一个值的集合。一个这样的返回变量是pw_name;这个变量返回目标文件或目录所有者的用户名。

我们可以创建一个playbook,如果一个特定的用户名拥有指定的文件,则返回一条信息。比如说

---
- name: check file ownership
  hosts: all
  gather_facts: no
  become: yes
  tasks:
  - name: get file info
    stat:
      path: /var/log/kern.log
    register: file_info
  - name: owned by ubuntu usert?
    debug:
      msg:  file is owned by the ubuntu user
    when: file_info.stat.pw_name != 'ubuntu'
  - name: not owned by the ubuntu user?
    debug:
      msg: file is not owned by the ubuntu user
    when: not file_info.stat.pw_name != 'ubuntu'

在上面的例子中,我们检查ubuntu用户是否拥有/var/log/kern.log这个文件。如果是,我们就返回一个适当的信息。

下面是一个输出的例子

检查文件类型

stat模块的另一个返回值允许我们检查文件类型。使用 isreg 和 isdir 等返回值,我们可以检查一个文件是否是一个目录。

---
- name: check file type
  hosts: all
  become: ye
  tasks:
  - name: get file info
    stat:
      path: /var/log/kern.log
    register: file_info
  - name: regular file?
    debug:
      msg:  specified path is a regular file
    when: file_info.stat.isreg
  - name: is a directory?
    debug:
      msg: specified path is a directory
    when: file_info.stat.isdir

保存并运行playbook为。

Ansible stat的返回值

以下是 ansible stat 模块返回的值。

  • attributes:返回指定文件的属性。
  • executable:如果调用的用户在目标路径上有执行权限,则返回 true。
  • exists:如果指定的路径存在,返回真。
  • gr_name:返回文件所有者的组的名称。
  • islbk:如果指定的文件是一个块状设备,则返回true。
  • ischr: 如果指定的文件是一个字符文件,则返回真。
  • isreg:如果指定的文件是一个常规文件,则返回真。
  • isdir:如果指定的文件是一个目录,则返回真。
  • islnk:如果目标文件是一个链接,则返回真。
  • mode:以八进制符号返回文件权限。

总结

在本指南中,我们讨论了如何使用ansible stat模块来收集文件和文件系统的信息。