ansible 的常用模块中没有判定当文件存在或者不存在时,执行某个执行
使用下面方法能简单判定某个文件是否存在
---
- name: judge a file or dir is exits
shell: ls /home/sfy
ignore_errors: True
register: result
- shell: echo "file exit"
when: result|succeeded
- shell: echo "file not exit"
when: result|failed
另外一种
#当/root/.ssh/id_rsa文件存在时,则修改该文件权限
- command: chmod 600 /root/.ssh/id_rsa removes=/root/.ssh/id_rsa
本文介绍如何在Ansible中检查文件是否存在的两种方法。一种是通过`ls`命令并结合`ignore_errors`参数来判断文件是否存在;另一种是直接使用`command`模块,并设置条件仅在文件存在时执行特定操作。

237

被折叠的 条评论
为什么被折叠?



