You don't even need a playbook to do this :
- Restarting nginx :
ansible your_host -m service -a 'name=nginx state=restarted'
(see service module)
- Kill a process by process id
ansible your_host -m command -a 'kill -TERM your_pid'
(adjust signal, and use pkill/killall if you need to match a name; see command module)
However, I wouldn't say that ansible shines if you're just using it for ad-hoc commands.
If you need a tutorial to get you started with playbooks, there is one over here.
Now if you can to put these (the official name for service, commands, etc.. are modules) in a playbook (let's call it playbook.yml), you can just :
- hosts: webappserver
tasks:
- name: Stops whatever
command: kill -TERM your_pid
notify:
- Restart nginx
- name: Another task
command: echo "Do whatever you want to"
handlers:
- name: Restart nginx
service: name=nginx state=restarted
Create an inventory file (hosts
) containing :
# webappserver should resolve !
webappserver
Invoke with :
ansible playbook.yml -i hosts
and it should work.
This is all very basic and can be grasped easily reading the docs or any tutorial out there.