run quick ad-hoc tasks using Ansible

Ansible with no doubt is one of the most popular tools that its name always comes up in every discussion about DevOps Automation. Its no joke that Ansible does its best when it’s used against an inventory of infrastructure containing nodes with designated roles and tasks.

at my work I often find myself in need to provision and configure single nodes, machines that are not a part of any architecture & their only reason to exist is to serve a single purpose.

Ansible has a list of hundreds of useful modules that can always come in handy in these provisioning tasks, but I find it rather difficult to create an inventory or a playbook just for a single use of a module on my target server. What works here is to use Ansible for ad-hock tasks by using the command line options.

All the information that I’m posting here are in Ansibles documentation as well as the help menu, but I was not able to find a cheatsheet or a single page that gives tips on how to use Ansible against single IP nodes.

The Inventory

every single time you run ansible, it expects you to pass the inventory to it. to force ansible to takes our single (it can be even multiple) IP string as inventory you should pass “all” option after “i” option followed by our IP address followed by commas.

The all option is simply is required as we are not passing a inventory name within our IP address string.

ansible -i all ","

that is the first step.

The module part

pass your module with -m followed by the name of module. to pass the arguments of your module add -a option together with arguments in quotes.

ansible -i all "," -m hostname -a ""

The User

assuming your local user is not the same with the one that ansible should login to the node, you can pass the option -u or –user followed by the user to ask ansible to use it for the secure shell.

ansible -i all "," -m hostname -a "" -u root

Escalate user level

-b, –become options provide a way to run the task with a escalated user level, by default runs the task with “sudo” but you can change this by adding an additional –become-method option.

ansible -i all "," -m hostname -a "" -u mahdi -b

Well thats it, that is the minimum it takes to be able to run awesome ansible modules against your freshly created machine.

Leave a Reply