ansible - check if file exists on *local* machine

跟風遠走 提交于 2019-12-06 16:40:29

问题


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

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.


回答1:


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


来源:https://stackoverflow.com/questions/32957418/ansible-check-if-file-exists-on-local-machine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!