Ansible: rerun playbook from failed task - ansible

I am executing play-book on only one host.
My Current task execution flow is:
1) Executing task
2) If any of the task fails in between, clean up everything
3) Rerun from the beginning.
This does not sound very efficient. I want to create flow very much like this,
1) Executing task
2) Task failed ..
3) if we rerun the play-book, ansible should execute tasks from the failed task. It should not rerun from the beginning.
In attempt to do that I was trying to achieve that once task was failed, I was trying to execute play-book with "--limit", it throws following error.
root#centos:/etc/ansible# ansible-playbook stack.yml --limit -vvvv
ERROR: provided hosts list is empty
root#centos:/etc/ansible# cat /root/stack.retry
10.17.10.150
I am not sure is this the right way to rerun the play-book to achieve this work flow.

Use the option --start-at-task=START_AT
This will start your playbook at the task matching the given name. In your case:
ansible-playbook stack.yml --start-at-task=START_AT
change "START_AT" with the name of the task you will start at.
The --limit option you have used is to limit the playbook to hosts matching the pattern and not limiting tasks

Related

How can I run an Ansible play book only if it was not successfully run previously.?

I need to run a playbook having a number of checks performed on hosts but needn’t run the same again if run successfully once. I would want Ansible to trigger a message saying that the checks(Ansible script) was already run successfully. How can I achieve that?
There is an option called: --start-at-task=START_AT
This will start your playbook at the task matching the given name.
example
ansible-playbook playbook_name --start-at-task=START_AT
change "START_AT" with the name of the task you will start at.

Ansible Tower/AWX bug? Job task runs serially instead of parallel

I have a very generic playbook with no hard coded info whatsoever. Everything in the playbook is a variable and filled out by supplying extra vars, even host names for connections. There are no inventory files in use since the host this is run against is random usually.
On a command line in linux, I can run my ansible playbook multiple times with different variables passed and they will all run at the same time.
ansible-playbook cluster_check_build.yml -e {"host": "host1"...}
ansible-playbook cluster_check_build.yml -e {"host": "host2"...}
In tower however, if I create a job template and use the same playbook then things run serially. I call that job template multiple times using the API and passing the data as JSON. Each time I called the API to launch the job task I am supplying new extra_vars so the job task is running against different hosts. I see the jobs run serially and not parallel like from the command line.
I have 400+ hosts that need to have the same playbook run against them at random times. This playbook can take an hour or so to complete. There can be times where the playbook needs to run against 20 or 30 random hosts. Time is crucial and serial Job processing is a non starter.
Is it possible to run the same job template against different hosts in parallel? IF the answer is no then what are my options? Hopefully not creating 400+ job templates. That seems like it defeats the purpose of a very generic playbook.
I feel like an absolute fool. In the bottom right of my job template is a tiny check box that says "ENABLE CONCURRENT JOBS" <---this was the fix.
Yes, you can run templates/playbooks against multiple hosts in parallel in Tower/AWX.
These are the situations where your template will run serially:
"forks" set to 1 in your template
SERIAL=1 within your playbook
Your Tower/AWX instance is setup with only 1 fork
Your Instance is set with >1 forks but other jobs are running at the same time.

Is there any way to get more details of a ansible task

ansible-playbook —list-tasks —tag<tagname>
Lists the tasknames. Is there way to get more details of task other than name?
If you want to see the end result of each task without executing the tasks,
Ansible provides a check mode, also called dry run mode that predicts the changes that may occur in each task without actually executing it.
ansible-playbook --check your_playbook.yaml

Ansible Debugger: continue execution when a task fails

I'm aware that the continue command will continue running the playbook, however it will stop running tasks on the failed host. Is there any way to continue running tasks on the host that failed?
https://docs.ansible.com/ansible/latest/user_guide/playbooks_debugger.html
In the task level, if it's likely to be failure, e.g. stop a stopped server, you can indicate "ignore_errors: yes", to tell Ansible it's OK to fail the task and continue, e.g.:
name: This task could fail, but it doesn't matter, contiue with next task
shell: "stop_server.sh"
ignore_errors: yes
While in host level, you already have the configuration to continue
Not sure if that's something you're looking for
A workaround: always skip a task in check mode by adding the following field to a task:
ignore_errors: "{{ ansible_check_mode }}"

How to resume execution of playbook from same place where it failed?

I am running a playbook for deployment of nodes.Suppose because of some issue like power failure or some other exception, the execution failed after executing some tasks.So, I am trying to resume the execution from same place where it failed, when I run the playbook next time.
It is time consuming to run the playbook for all the task from start.Is it possible to achieve the solution?
You can use function --start-at-task="task name" this will run the whole playbook from the specific task name and continue from there om. But be careful, if you have registered any variables in tasks before and use them afterwards, starting at that task will not register them and therefore the play will not use them. In that case you could give tag "always" to the tasks that register variables
The output of a failed ansible-playbook run actually tells you how to do this (assuming you have 'retry_files_enabled' set true in your config, which is the default):
to retry, use: --limit #/some/path/to/a/retry/file
In the case of a failure, Ansible will create a retry file, which is for the specific purpose of what you requested. Therefore, just append the '--limit' statement to the command you used to run the playbook and kick it off again.
Edit: To be fair, your specific case of a power failure on the control machine would not be covered by this, but any failure that Ansible handles (i.e. the majority), will work with this method.
execute like this:
ansible-playbook --forks=10 myscript.yml --start-at-task='install java'
with limit: ansible-playbook --forks=10 myscript.yml --limit limit1 --start-at-task='install java'

Resources