1

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.

Nilesh
  • 255
  • 1
  • 6
  • 18

2 Answers2

2

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
Henrik Pingel
  • 9,380
  • 2
  • 28
  • 39
1

Not sure I understand, but I think you're saying your problem is you don't want to run play3 without first running play2 and play1?

I don't know a way to enforce this, but I can think of a few ways to make it more convenient.

First, just put all three plays into one file, then when you run one, you run them all. If you want to occasionally run play1 without play2 and play3, use tags. With tags, you could make play1 and play2 have the play3 tag, so you'd never run play3 without play1 and play2 by accident.

I usually put my ansible-playbook command into a script file so I don't have to remember it. If you like doing that, you can make the script launch the three plays. It's especially nice when you have co-workers who don't know ansible so well. EG I make a file called run.sh that contains:

ansible-playbook -i hosts myplay.yaml $@

The $@ is bash for add any args, so I can then do ./run.sh -t play1 and stuff like that.

Hope that helps!

Dylan Martin
  • 548
  • 4
  • 14
  • Thanks Dylan, I updated my question, sorry for the confusion, but I want to make sure, each play run on different hosts. – Nilesh Jan 11 '18 at 18:38
  • Oh, well if you've got separate plays, each play can have a different hosts entry. – Dylan Martin Jan 12 '18 at 16:44
  • yes, but how to give dependency for play? like when I run `play3` it should run `play2` and `play1` – Nilesh Jan 12 '18 at 16:46