6

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 }}"
3
  • Does the task that you run with ansible include -m command -s 'some commands', i.e. you aren't actually running a playbook? In your last example, is it one play for HOST_A and HOST_B, then run ansible command on HOST_C and HOST_D, then final play on HOST_E and HOST_F? Examples of the plays and commands would help a lot, even if you include dummy code, to understand structure.
    – RichVel
    Nov 22, 2017 at 21:28
  • A piece of code would greatly enrich your question... Nov 23, 2017 at 6:04
  • @RichVel It's pretty simple and as you describe, except I'm using ansible-playbook instead of just ansible
    – Peter Turner
    Nov 24, 2017 at 14:00

2 Answers 2

5

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.

1
  • Looking into some of the docs, I discovered something I'd tried before, but forgot about - putting multiple plays inside the same playbook (should have figured that was obvious since it's a playbook!) but I think that'll accomplish what I want perfectly. My only problem is that the device I'm running ansible on can't directly reach the devices I want to mess with, but that's not a show stopper.
    – Peter Turner
    Nov 24, 2017 at 14:12
3

Take a look at Delegation - you can delegate the execution of tasks to a different host from within a playbook:

  - name: enable the server in haproxy
    haproxy: 'state=enabled backend=myapplb host={{ inventory_hostname }} socket=/var/lib/haproxy/stats'
    delegate_to: "{{ item }}"
    with_items: groups.lbservers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.