Ansible Ad-hoc Commands

Posted by Riomhaire Research on Sunday, February 4, 2018

Ansible is a versatile simple and powerful IT automation platform that makes your day to day dev-ops a lot easier. Ansible allows automation of your IT Infrasture such as installing software, restarting servers amongst many other features be it on your own hardware or in the cloud. Riomhaire Research mainly uses Ansible to keep our software up to date, for user management and being part of our deployment pipeline. With the pipeline we use it to deploy applications and services to our various deployment environments.

This short blog entry is not going to describe Ansible and how to use it - there are many other sites far better than us to do that, and Ansible has a lot of excellent documentation itself. The problem is we use Ansible often enough to find it vital, but not frequent enough to have the commands second nature; Most of our usage are part of automated pipeline playbooks. There are some excellent cheatsheets out there, but the feature we use from the command line as developers are the ad-hoc commands for service starting and stopping we use the most and are going to put in this post as an easy to access ‘aide memoire’.

To stop a service on one or more machines use the command:

ansible <machine1>[,<machine2>..] -m service --become -a "name=<someservice> state=stopped" 

To start a service:

ansible <machine1>[,<machine2>..] -m service --become -a "name=<someservice> state=started" 

To restart a service:

ansible <machine1>[,<machine2>..] -m service --become -a "name=<someservice> state=restarted"

Hope this helps.