Force stop ansible playbook - ansible

Is it possible to stop an ansible playbook?
I am not talking about failed_when condition or some other ansible modules, but actually killing the process!

From within the playbook, you could use:
assert or fail to fail a task on a specific condition
all_errors_fatal to force the entire playbook to stop when a task fails
From the outside world (Linux shell), you could use CTRL-C while the playbook is running on your terminal.
Or, from another terminal, you could get the PID of the ansible-playbook command (ps aux | grep ansible-playbook, for example), and kill that PID.

Related

Playbook 'hangs' when running a Python script that launches another Windows process

I'm having an issue when running a Python script using the win_command module. The playbook execution hangs indefinitely when the Python script starts a Tomcat process which is supposed to keep running even once the script completes. If I were to manually kill the Tomcat process, the Ansible playbook completes.
---
- name: Restore product
win_command: 'python restore-product.py'
args:
chdir: C:\temp
I have tried the following within the Python script hoping that Ansible would not be able to track the launched process, but have had no luck:
subprocess.Popen('start /cmd /c service.bat startup', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
sys.exit(0)
Do not use Python's popen function and rather go for spawn() as described here:
https://stackoverflow.com/a/1196122/4222206

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.

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

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'

Daemonizing an executable in ansible

I am trying to create a task in ansible which executes a shell command to run an executable in daemon mode using &. Something like following
-name: Start daemon
shell: myexeprogram arg1 arg2 &
What am seeing is if I keep & the task returns immediately and the process is not started . If I remove & ansible task waits for quite some time without returning.
Appreciate suggestion on proper way to start program in daemon mode through ansible. Pls note that I dont want to run this as a service but an adhoc background process based on certain conditions.
Running program with '&' does not make program a daemon, it just runs in background. To make a "true daemon" your program should do steps described here.
If your program is written in C, you can call daemon() function, which will do it for you. Then you can start your program even without '&' at the end and it will be running as a daemon.
The other option is to call your program using daemon, which should do the job as well.
- name: Start daemon
shell: daemon -- myexeprogram arg1 arg2
When you (or Ansible) log out the exit signal will still be sent to the running process, even though it is running in the background.
You can use nohup to circumvent that.
- name: Start daemon
shell: nohup myexeprogram arg1 arg2 &
http://en.wikipedia.org/wiki/Nohup
From the brief description on what you want to achieve, it sounds like it would be best for you to set up your executable as a service (using Upstart or similar) and then start/stop it as needed based on the other conditions that require it to be running (or not running).
Trying to run this as a process otherwise will entail having to capture the PID or similar so you can try and shut down the daemon you have started when you need to, with pretty much the same amount of complexity as installing an init config file would take and without the niceties that systems such as Upstart give you with the controls such as start/stop.
I found the best way, particularly because I wanted output to be logged, was to use the "daemonize" package. If you are on CentOS/Redhat, like below. There is probably also an apt-package for it.
- name: yum install daemonize
yum:
name: daemonize
state: latest
- name: run in background, log errors and standout to file
shell: daemonize -e /var/log/myprocess.log -o /var/log/myprocess.log /opt/myscripts/myprocess.sh
Adding to the daemonize suggestions above, if you want to start your program in a specific directory you can do:
- name: install daemonize package
package:
name: daemonize
state: latest
- name: start program
command: daemonize -c /folder/to/run/in /path/to/myexeprogram arg1 arg2
Notably, you also probably want the -e -o flags to log output.

Resources