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?
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
ansible main -i ./hosts -m apt -a "update_cache=yes upgrade=safe"
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
ansible 192.168.0.1 -m service -a 'name=nginx state=restarted'
ansible main -i ./hosts -m service -a 'name=apache2 state=restarted'
ansible webservers -m service -a "name=httpd state=started"
I’ve given 2 examples above to show:
- That you can reference a host directly on the terminal.
- 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.