已关闭,此问题需要details or clarity。目前不接受答复。
**想改善这个问题吗?**通过editing this post添加详细信息并澄清问题。
2天前关闭。
Improve this question
我想用ansible检查一个目录是否不为空。如果目录不为空,我希望获得服务器主机名或IP地址。谁能帮帮我?谢谢!
我想做个测试。
- hosts: all
tasks:
- find :
paths: /tmp/foo
file_type: all
hidden: true
register: out
- fail :
msg: the /tmp/foo directory is not empty
when: out.matched != 0
2条答案
按热度按时间whlutmcx1#
Q:***“检查目录,如果为空则列出服务器。"***
答:有两个非常有用的模式,你应该一步一步地去了解细节。
例如,给定两个远程主机,第二个主机上的目录为空
找到文件
1.克里特岛事实词典
给予
1.从字典中选择/拒绝属性并获取与条件匹配的属性的名称
给你想要的
完整的测试剧本示例
7fhtutme2#
由于没有定义空的意思,如果可能有zero byte files,我理解你的问题,你感兴趣的是文件夹的大小。
根据Use folder size in Conditional
如果您要计算文件夹中包含的数据量,可以使用proceed further with the answer there或Ansible: Need generic solution to find size of folder/directory or file。
由于这需要使用
command
或shell
,通常不建议在所有情况下使用,因此可以将任务转移到自定义模块中。为此,创建一个用Bash或Shell编写的自定义模块似乎是最好的方法。下面的概念/示例模块接受一个字符串参数(
path
)并测试它是一个目录还是文件。之后,它提供了所有文件的目录大小,包括子目录或仅作为结果集的文件大小。概念/示例模块
size.sh
测试攻略
size.yml
结果
ansible-playbook --user ${ANSIBLE_USER} size.yml
感谢
对于脚本和模块
对于一般Linux和Shell