0%

Useful Ansible one-liners (AKA: Ad-hoc commands)

You need to execute a single command across multiple servers? You may use Ansible but that’s mainly about playbooks, provisioning, setup and the like isn't it?

Not so.

Having been using Ansible for a couple of years I thought it about time I started using the ad-hoc commands.

In the past I used Fabric for executing SSH commands across multiple hosts but now Ansible fills that role nicely.

Below is a quick list of some Ansible one-liners:

# Update Your Apt Cache and Upgrade

  1. ansible main -i ./hosts -m apt -a "update_cache=yes upgrade=safe"

  2. ansible centosserver -i ./hosts -m yum -a 'name=* state=latest'

You’re not limited by OS with Ansible. Here I’m using both the Apt and Yum command modules to update all packages.

# Restart Your Webserver

  1. ansible 192.168.0.1 -m service -a 'name=nginx state=restarted'

  2. ansible main -i ./hosts -m service -a 'name=apache2 state=restarted'

  3. ansible webservers -m service -a "name=httpd state=started"

I’ve given 2 examples above to show:

  1. That you can reference a host directly on the terminal.
  2. It works for any service

# Deploy a Git Repo

ansible webservers -m git -a "repo=git://example.com/epicapp.git dest=/srv/epicapp version=HEAD”

If Knowledge is what you want
This will give you a JSON output of all your servers and a huge amount of information on each:
ansible all -m setup

The above assumes you have your hats file in the default location or defined somewhere.

  1. Official Ansible Docs
  2. Ansible Video Tutorial Series