🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[stat模块官方文档][1] stat模块和linux的stat命令实现的功能相同 ### 常用示例 ~~~ - stat: path: /etc/foo.conf register: st - fail: msg: "Whoops! file ownership has changed" when: st.stat.pw_name != 'root' # 检测path是否存在并且是符号链接,如果path不存在,直接检测sym.stat.islnk会报错;因此,先检测sym.stat.islnk是否定义 - stat: path: /path/to/something register: sym - debug: msg: "islnk isn't defined (path doesn't exist)" when: sym.stat.islnk is not defined - debug: msg: "islnk is defined (path must exist)" when: sym.stat.islnk is defined - debug: msg: "Path exists and is a symlink" when: sym.stat.islnk is defined and sym.stat.islnk - debug: msg: "Path exists and isn't a symlink" when: sym.stat.islnk is defined and sym.stat.islnk == False # 检测目录是否存在 - 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 ~~~ 还有exists方法 [1]:http://docs.ansible.com/ansible/latest/stat_module.html