Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible Playbook to run Shell commands

I recently dived into Ansible for one of my servers, and found it really interesting and time saving. I am running an Ubuntu dedicated server and have configured number of web applications written on Python and a few on PHP. For Python I am using uwsgi as the HTTP gateway. I have written shell scripts to start/restart a few processes in order to run the instance of a specific web application. What I have to do everytime is, connect ssh and navigate to that specific application and run the script.

WHAT I NEED

I've been trying to find a way to write Ansible playbook to do all that from my personal computer with one line of command, but I have no clue how to do that. I have'nt found a very explanatory (for a beginner) documentation or help on the internet.

QUESTION

How can I restart Nginx with Ansible playbook? How can I kill a process by process id?

like image 926
Rai Ehtisham Avatar asked Nov 24 '13 18:11

Rai Ehtisham


People also ask

How do you use shell commands in Ansible-playbook?

Run a Command Using Shell Module If a File Does Not ExistIt specifies the path to the file which, if it exists, the command to be executed is skipped. The playbook shown checks if the file hello. txt exists in the home directory of the target host. If the file is absent, then the shell command specified is executed.

Which command do you use to run an Ansible-playbook?

Playbooks are written in the YAML format and have a . yml file extension. Use this command to run a playbook: $ ansible-playbook <playbook. yml>

How do you run a shell script on a remote server using Ansible?

To run a command on a remote host we can use the Ansible's shell module (or win_shell for Windows targets). By default, the shell module uses the /bin/sh shell to execute commands, but it is possible to use other shells such as /bin/bash by passing the executable argument.

What is the difference between shell and command in Ansible?

Both modules execute commands on target nodes but in a sensible different way. The command modules execute commands on the target machine without using the target shell, it simply executes the command. The target shell is for example the popular bash , zsh , or sh .


1 Answers

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.

like image 71
leucos Avatar answered Oct 13 '22 22:10

leucos