ansible - 检查文件是否存在于 *local* 机器上 [英] ansible - check if file exists on *local* machine

查看:48
本文介绍了ansible - 检查文件是否存在于 *local* 机器上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种情况需要检查本地机器上文件的状态(我将在其中调用 ansible-playbook ...).

I have a situation where I need to check the status of a file on the local machine (the one where I will call ansible-playbook ...).

如果用户创建的文件存在,则需要将其复制到远程主机.如果它不存在,那么远程主机都不需要它.

If a file that is created by the user exists, it needs to be copied over to the remote host(s). If it doesn't exist, then none of the remote hosts need it.

我知道我做过类似的事情:

I know I've done things like :

- name: Check for ~/.blah/config
  stat: path=/home/ubuntu/.blah/config
  register: stat_blah_config

- name: Do something with blah config
  shell: ~/do_something_with_config.sh
  when: stat_aws_config.stat.exists == true

但这仅在文件远程存在时才有效.有没有办法仅在文件本地存在时有条件地执行任务(如复制)(让第一个任务中的 stat 在本地而不是远程执行),并且失败默不作声呢?我不确定 ansible 是否有这种功能,但它会很有用.

But that will only work if the file exists remotely. Is there a way to conditionally execute a task (like copy) only if the file exists locally (have the stat in the first task execute locally instead of remotely), and fail silently if it does not? I'm not sure if ansible has this kind of functionality, but it would be useful.

推荐答案

通过 delegate_to 语句应该做你想做的:

Delegating the tasks to the localhost via the delegate_to statement should do what you want:

- name: Check for ~/.blah/config
  delegate_to: localhost
  stat:
    path: /home/ubuntu/.blah/config
  register: stat_blah_config

这篇关于ansible - 检查文件是否存在于 *local* 机器上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆