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

How to run different role for different hosts in single ansible playbook?

· 52CE 我爱测

I have 3 ansible playbook. my_ansible ├── hosts ├── play1.yaml ├── play2.yaml ├── play3.yaml └── roles ├── role1 ├── role2 └── role3 hosts [play1] host1 [play2] host2 [play3] host3 Now, when I run play3 , I want to run play2(role…

问题原文

原帖链接 · Nilesh · CC BY-SA 3.0

I have 3 ansible playbook.

my_ansible
├── hosts
├── play1.yaml
├── play2.yaml
├── play3.yaml
└── roles
    ├── role1
    ├── role2
    └── role3

hosts

[play1]
host1

[play2]
host2

[play3]
host3

Now, when I run play3, I want to run play2(role2) on host2 and play1(role1) on host1.

I check, role-dependency, but that apply on same host, means, if I add role-dependency in role3 for role1 and role2, then these 2 roles will run against host3.

I want to make sure, play1 runs on host1 only, play2 runs on host2 only and play3 runs on host3 only.

I try to find the playbook dependency, but no luck yet.

If is there any better way to solve this, then I am ready to head into that way too.

已采纳解答

原帖链接 · Henrik Pingel · CC BY-SA 3.0

I guess the easiest way to achieve this is to create a master playbook and define the order of playbooks there.

It is possible to include playbooks inside a master playbook. [...] The plays and tasks in each playbook listed will be run in the order they are listed, just as if they had been defined here directly.

See Ansible documentation on playbooks reuse includes

In order to achieve that play3 runs play2 and play1 as well one would just need to define a master playbook like this:

---
- import_playbook: play3.yml
- import_playbook: play2.yml
- import_playbook: play1.yml

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

资料来源与适用范围

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

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

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