Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run only one task and handler from ansible playbook

Tags:

ansible

How can I run a single task from an Ansible playbook and the handler that gets notified when that task completes successfully, while skipping all other tasks in the relevant playbook?

Currently I execute the following:

ansible-playbook --start-at-task "task1" --step -K -i hosts playbook.yml

and then press Ctrl+c after the task has finished. This will also skip the handler however.

I know I can add a tag to the task and use that, as in How to run only one task in ansible playbook?, but I would prefer being able to do this without adding a tag. Is that possible?

like image 871
jdoestackoverflow Avatar asked Sep 05 '25 04:09

jdoestackoverflow


2 Answers

It is possible to run a separate role (from roles/ dir):

ansible -i stage.yml -m include_role -a name=create-os-user localhost

and a separate task file:

ansible -i stage.yml -m include_tasks -a file=tasks/create-os-user.yml localhost

If you externalize tasks from role to root tasks/ directory (reuse is achieved by import_tasks: ../../../tasks/create-os-user.yml) you can run it independently from playbook/role.

like image 60
gavenkoa Avatar answered Sep 08 '25 23:09

gavenkoa


There's currently nothing coming with ansible-playbook to allow you to run a single task, like --task. Thus, to me, the tag along with the --tags option is your best solution here.

like image 21
Cedric Morent Avatar answered Sep 08 '25 23:09

Cedric Morent