仅当文件存在时,Ansible包含任务 [英] Ansible include task only if file exists

查看:64
本文介绍了仅当文件存在时,Ansible包含任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图仅在文件存在的情况下包括它.如果我的角色的用户需要,这允许在现有任务/角色"之间进行自定义的任务/角色".我发现了:

I'm trying to include a file only if it exists. This allows for custom "tasks/roles" between existing "tasks/roles" if needed by the user of my role. I found this:

- include: ...
  when: condition

但是Ansible文档指出:

But the Ansible docs state that:

评估了所有任务,但有条件地应用于了每个任务"-所以

- stat: path=/home/user/optional/file.yml
  register: optional_file
- include: /home/user/optional/file.yml
  when: optional_file.stat.exists

如果所包含的文件不存在,将失败.我猜可能存在另一种机制,允许用户将任务添加到现有配方中.我不能让用户在我的角色之后添加角色,因为他们将无法控制顺序:他们的角色将在我的角色之后执行.

Will fail if the file being included doesn't exist. I guess there might be another mechanism for allowing a user to add tasks to an existing recipe. I can't let the user to add a role after mine, because they wouldn't have control of the order: their role will be executed after mine.

推荐答案

感谢您的帮助!我终于在今天的Ansible中尝试了所有答案和我自己的问题的代码后才提出自己的问题: ansible 2.0.1.0

Thanks all for your help! I'm aswering my own question after finally trying all responses and my own question's code back in today's Ansible: ansible 2.0.1.0

我的原始代码现在似乎可以正常工作,除了我要查找的可选文件位于本地计算机中之外,因此我必须通过 local_action 运行stat并为该特定任务设置become: no,因此ansible不会尝试在我的本地计算机上执行sudo并出现以下错误:"sudo:需要密码\ n"

My original code seems to work now, except the optional file I was looking was in my local machine, so I had to run stat through local_action and set become: no for that particular tasks, so ansible wouldn't attempt to do sudo in my local machine and error with: "sudo: a password is required\n"

- local_action: stat path=/home/user/optional/file.yml
  register: optional_file
  become: no
- include: /home/user/optional/file.yml
  when: optional_file.stat.exists

这篇关于仅当文件存在时,Ansible包含任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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