使用Ansible stat模块检查目录是否存在

使用Ansible stat模块检查目录是否存在或者是否为一个目录还是文件

理论知识

在Ansible中,你可以使用stat模块来检查一个目录是否存在。stat模块可以用来获取文件或目录的状态信息,包括它是否存在。下面是一个简单的例子,说明如何使用stat模块来检查一个目录是否存在:

yaml 复制代码
- name: Check if a directory exists
  stat:
    path: /path/to/directory
  register: directory_stat

- name: Print a message depending on the existence of the directory
  debug:
    msg: "Directory exists"
  when: directory_stat.stat.exists and directory_stat.stat.isdir

- name: Print an alternative message if the directory does not exist
  debug:
    msg: "Directory does not exist or is not a directory"
  when: not directory_stat.stat.exists or not directory_stat.stat.isdir

在这个例子中:

  • stat模块被用来获取指定路径的状态。
  • 使用register关键字将结果存储到directory_stat变量中。
  • 在第一个debug任务中,我们检查目录是否存在并且是一个目录(isdir为True)。
  • 在第二个debug任务中,如果目录不存在或者不是一个目录,则打印一条消息。

这个方法确保了不仅检查路径的存在性,还确认了该路径确实是一个目录。如果需要进一步确认目录的状态(例如权限、所有者等),可以通过directory_stat.stat访问更多的属性。

记得将/path/to/directory替换为你想要检查的实际目录路径。


例子懂得多

在使用中遇到了一下问题,记录一下:

playbook包含一下内容:

yaml 复制代码
---
- name: test how 'stat' work here
  hosts: webservers
  tasks:
	  - name: Check ssl cert dir
	    stat:
	      path: /etc/httpd/conf.d/ssl
	    register: dir_status
	
	  - name: The status of dir
	    ansible.builtin.debug:
	      var: dir_status
	
	  - name: If Directory not exist
	    ansible.builtin.file:
	      path: "{{ ssl_cert_dir }}"
	      state: directory
	    when: not dir_status.stat.isdir

比如检查此目录/etc/httpd/conf.d/ssl是否存在于server上,当这个目录不存在的时候,

执行playbook这个会有下面的错误:

复制代码
TASK [Check ssl cert dir] ******************************************************
ok: [serverb.lab.example.com]

TASK [If Directory not exist] **************************************************
fatal: [serverb.lab.example.com]: FAILED! => {"msg": "The conditional check 'not dir_status.stat.isdir' failed. The error was: error while evaluating conditional (not dir_status.stat.isdir): 'dict object' has no attribute 'isdir'\n\nThe error appears to be in '/home/student/control-review/playbook.yml': line 37, column 11, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n        - name: If Directory not exist\n          ^ here\n"}

按照理论来说xx.stat.isdir应该是没语法问题可以正常使用的,但是这个确保错了,说'dict object' has no attribute 'isdir'...

然后从debug中我们就知道问题了, 确实dir_status的stat中没有isdir属性,只有一个exists为false

当我们创建一个对应的ssl目录出来时:

再次运行playbook,会看到

这里可以看到exists变为了true,并且我们有了更多的属性,其中就包括了isdir,这就得出了一个小结论:
不要上来就直接用 dir_status.stat.isdir, 而应该用dir_status.stat.exists 先判断一下文件/目录是否存在,然后再来判断是否为目录。

相关推荐
chu_bingzhi32 分钟前
GNU 汇编器
服务器·gnu
北京阿尔泰科技厂家1 小时前
阿尔泰SC5061八串口联网服务器技术解析与工程应用指南
服务器·物联网·串口服务器·串口通讯·远程监测·自动化控制·工业测试测量
tianyuanwo1 小时前
RPM Spec中Name: %{?scl_prefix}xxx中增加宏定义的设计原理
linux·服务器·rpm·scl
cc_瀚海知行1 小时前
工具调用 6|助手什么时候该停下来?
java·服务器·前端·人工智能·python·ai
红姐跨境书2 小时前
Facebook账号频繁被要求验证怎么办?从登录异常到浏览器环境排查
运维·服务器
月落汀兰2 小时前
服务器出问题没人告警?Zabbix6.0 LNMP 部署,MySQL 监控 + 邮件告警保姆级实战
运维·服务器·zabbix
骇客野人2 小时前
CI/CD A 服务器(构建机)免密登录 B 服务器(目标部署机)完整操作
服务器·servlet·ci/cd
虚无的纽扣3 小时前
【Linux】线程(下):怎么创建、终止与分离一个线程
linux·运维·服务器
FITA阿泽要努力3 小时前
/goal 命令:给 Agent 设定目标
服务器·人工智能·python·agent
晴天的雨.9923 小时前
【Linux】编译器gcc/g++
linux·运维·服务器