大网数据广告活动
服务器运维问答

Ansible: execute tasks.yml from playbook.yml

· 52CE 我爱测

I searched a lot for exact the same thing that I wanted to do, but found nothing. So, I'm here to ask: I have a playbook.yml, where defined some tasks. The first task of that playbook is checking existence of file and, if file ex…

问题原文

原帖链接 · Andrey N. · CC BY-SA 4.0

I searched a lot for exact the same thing that I wanted to do, but found nothing. So, I'm here to ask:

I have a playbook.yml, where defined some tasks. The first task of that playbook is checking existence of file and, if file exists I want to execute only the tasks, that are defined in the tasks.yml file and stop executing of tasks from playbook.yml (or vice versa). I have read Ansible documentation of course, first of all. But I still don't get if I'm able to do exactly what I want with import/include_tasks modules (tried both of them).

已采纳解答

原帖链接 · Vladimir Botka · CC BY-SA 4.0

Q: "Checking the existence of a file"

A: Use stat. For example

- stat:
    path: /etc/foo.conf
  register: st

Q: "If the file exists execute tasks, that are defined in tasks.yml file"

A: Use include_tasks. For example

- include_tasks: tasks.yml
  when: st.stat.exists

Q: "Stop executing of tasks from playbook.yml"

A: Use meta. For example

- meta: end_play
  when: st.stat.exists

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2019-11-26)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。