The easiest way to understand the basics of Ansible is by executing AdHoc commands. They are useful when you want to run simple tasks like rebooting all servers, changing the permission of a particular file in multiple systems etc. You don’t have to create a file for running an Adhoc command.You can run a basic Ansible AdHoc command as easy as executing a ping command.
I believe you have already installed the latest Ansible version. If not, please refer Ansible installation and install Ansible for your preferred system.
Now before running the ad-hoc command, we need some information about the setup. At a minimum, we need remote machines IP/hostname and a command which we intend to run.
Details of Remote Systems
The particulars of the remote systems are stored in an inventory file. By default it is the /etc/ansible/hosts file. You can enter the IP/hostname of you remote machines. You can also enter ssh username, ssh port number etc. For more information on Inventory lists, you can refer Inventory List.
Now you have to update the inventory file with the details of your remote machines. I have added the IPs of the two remote machines I have.
sudo vim /etc/ansible/hosts 10.128.0.5 188.8.131.52
Command to be Executed
Now for the actual command to be executed. I will be running a wall command. Wall command sends a message to the terminal of every user logged in. The following is the plain Linux command.
wall Hello to ansible
Now we have to convert this command to Ansible tasks. A task is a call to an Ansible module. The corresponding Ansible task is
ansible all -m shell -a "wall Hello to ansible"
- ansible – Keyword for running ansible AdHoc command.If we are running a playbook, we will be using ansible-playbook.
- all – The set of remote servers in which you intend to execute the command. ‘all‘ means all the servers specified in the inventory file(/etc/ansible/hosts). If we have any other groups in the in the inventory list, we can also give the group name.
- -m shell – We need to specify a module for every task. The module does the real work in Ansible. Each module is a script, written for a particular function(copy files, git commands etc). Every module has a similar structure. Here we are using the shell module. It is used for running shell commands.
- -a “wall Hello to ansible” – the arguments to the shell module. Here we are only giving the actual command as the argument. We can give other arguments like which bash to use, from which directory we should run the command in etc.
Now before running the task, you should make sure passwordless ssh connection has been setup between ansible control machine and the remote machines. For info on how to setup that you can refer http://www.tecmint.com/ssh-passwordless-login-using-ssh-keygen-in-5-easy-steps/.
In the following gif, you can see the execution of the task. You can see how the ansible control machine shows whether the task was successful on each remote server.
If you want to use another inventory file you can specify it during execution by using -i <path of inventory file>.
ansible all -m shell -a "wall Hello to ansible" -i inventory.ini
Do try out with some examples, so you get a grip on how to run the Ansible command.