ansible is very handy for one-off tasks like gathering quick info or running commands over a group, so I thought I would share some of those today:

Filtering on facts:

  • ansible -m setup -a ‘filter=ansible_distribution_version’ -o all (This runs the setup module on all hosts and filters on the ansible_distribution_version, so ’20’ for Fedora 20, etc. Note also that we are passing -o which means to put all the information on one line, this is very handy for grepping output).
  • ansible -m setup -a ‘filter=ansible_selinux*’ all (Runs the setup module and filters out the selinux info. Note that this is a array with more info, like config and runtime, etc)

Looking for specific information:

  • ansible -a ‘ps aux | grep puppet’ -m shell proxies (This runs the command on all hosts in the proxies group and shows output. Note that here we are using the ‘shell’ module instead of the default command module. This is because command doesn’t understand pipes and such, so you need a full shell module.)
  • ansible -m virt -a ‘command=freemem’ virthost (This uses the virt module and shows free memory on the virthost)

Docs on ansible modules:

  • Of course there’s google and duckduckgo and the ansible project web pages, but if you want to look up something quickly about an ansible module, use ‘ansible-doc’ command line. Just ‘ansible-doc shell’ for example to get all the info about the shell module, it’s arguments, etc.

It’s super easy to get started using ansible this way. All you have to do is install it on your control host and have ssh connectivity to the hosts you want to run things on.