HiveBrain v1.2.0
Get Started
← Back to all entries
principleMinor

What is the best strategy for running an ansible playbook within an ansible playbook?

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
thewhatplaybookwithinrunningforstrategyansiblebest

Problem

I have playbook that I've been hacking on for a year and it works OK, but I know it's not great, part of it runs ansible on the target machine with a "command" task. Because of that, I don't see the output of this task in my ansible playbook.

I need to run this play in the middle of the playbook because it is dependent on a prior task and a subsequent task depends on it (otherwise I'd break it up differently)

I'd like to be able to see the output of all my plays, is there a better way to branch off plays in the middle of a play.

For instance: given a playbook running on HOST_A and HOST_B at TASK 3, suspend running plays, run next plays on HOST_C and HOST_D then HOST_E and HOST_F?

- name: update other machines
  command: "/usr/bin/ansible-playbook /etc/ansible/playbooks/other-machines.yml -e 'ansible_user={{ item.ansible_user }}' -l {{ item.ansible_hostname }}"
  with_items: "{{ other_machine_connections }}"

Solution

If I've understood your problem correctly:

  • the ansible shell command in the middle could be converted to a simple playbook using the command task - let's call this middle.yml



  • your existing playbook could then directly import this middle.yml play (or even a more complex playbook). In Ansible 2.4+, you would use import_playbook, specifically import_playbook: middle.yml, or in earlier versions, just include: middle.yml.



This processes the whole middle.yml playbook as if it was part of the main playbook - you have all the features of Ansible plays (hosts etc) and the output appears as part of main playbook.

This answer may need updating depending on your response to my comment above.

Context

StackExchange DevOps Q#2624, answer score: 5

Revisions (0)

No revisions yet.