1

I have a huge playbook that calls multiple playbooks within it. It looks something like this:

cat global_playbook.yml
---
- hosts: server150
  tasks:
    - include_tasks: folder/playbook1.yml 
      vars: 
        ...

    - include_tasks: folder/playbook2.yml # I'm trying to start from here.
      vars:
        ...
     tags: test

    - include_tasks: folder/playbook3.yml
      vars:
        ...

... and so on

Sometimes, when I run it, it fails. And I can't seem to get it to start from a specific position.

I'm trying to start from folder/playbook2.yml. I tried doing:

ansible-playbook global_playbook.yml -start-at-task="folder/playbook2.yml"
ansible-playbook global_playbook.yml -start-at-task="**task within playbook2**"
ansible-playbook global_playbook.yml --tags="test"

But none of the work.

  • I can't run the playbook directly because as seen in the example at the beginning of this thread, the variables are in the global playbook. It wouldn't work when I had the variables in each playbook. They also don't have a hosts defined.
  • I know I can use --step so it would ask me which task to run but it just runs too many playbooks for that. Not to mention, since I'm using include_tasks, it won't tell me what the task only after I agree to it!

When using a playbook that calls other playbooks, is it possible to have it start from a specific point? Or have it continue from where it failed?

Thanks ahead!

  • Maybe you could use tags: https://stackoverflow.com/questions/23945201/how-to-run-only-one-task-in-ansible-playbook – Go0fy Apr 12 '21 at 14:07
  • Why are you breaking that into several playbooks? – Stefano Martins Apr 12 '21 at 14:14
  • 1
    Technically speaking, you have one single playbook including several lists of tasks. You could transform all that in several playbooks that you could call individually and eventually keep a all-in-one playbook that would run all of them using [`import_playbook`](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/import_playbook_module.html). Regarding `--start-at-task`, you are using includes in your example which are dynamic. Ansible cannot know before hand the tasks contained in the included file so it can't work. Using a static import if possible might make this possible. – Zeitounator Apr 12 '21 at 14:48
  • Thanks for the response, so using `import_playbook` would make it a static import? when using it, can I control the hosts the playbooks would run on from the global playbook? This was the main concern I had originally. – rogersjoshmac123 Apr 13 '21 at 07:36

1 Answers1

1

It all depends why you're using several playbooks. Right now, I would suggest you to think about the possibility of using roles instead. But you could store your variables in files and pass them to Ansible using the -e option like this:

ansible-playbook -i hosts --limit server150 -e @my-var-file.yml folder/playbook2.yml

Another option is to use the host_vars directory, so variables belong to that specific host.

In my opinion, tags are a handy resource when we have in a same playbook loosely coupled tasks. For instance: We have a playbook that implements a MySQL database and a SMB/CIFS server to store and share its backup files. So you can run only each one of these playbook parts.

Stefano Martins
  • 472
  • 2
  • 7