问题
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?
回答1:
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.
回答2:
It is possible to run separate role (from roles/
dir):
ansible -i stage.yml -m include_role -a name=create-os-user localhost
and 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 ccan run it independently from playbook/role.
来源:https://stackoverflow.com/questions/41281997/run-only-one-task-and-handler-from-ansible-playbook