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

查看:376
本文介绍了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.

推荐答案

通过 local_action将任务委派给本地主机语句应执行您想要的操作:

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

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

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

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