This post is mostly notes in my own notebook, in order for myself to remember something I have found useful.
Given my virtual laboratory that I created in an earlier post, it soon becomes tedious to run the same commands over and over again on the different nodes in the virtual laboratory. So I started to look for a solution and came across Ansible that allows me to issue a shell command that is then executed on multiple computers. I very much appreciate that Ansible does not require any special agent on the managed nodes. In addition, Ansible is open source.
Install Ansible
Ansible only needs to be installed on the computer from which you send commands to the remote or virtual machines you want to control. This computer is called the “control machine” in the Ansible documentation. In my case, it is the host computer on which I run the virtual machines that make up my virtual laboratory.
The only requirement for a basic Ansible installation is Python 2.x, which is installed as default on OS X but may need to be installed if you are using another OS.
Since the OS of my main computer is Mac OS X, Ansible can be installed using pip:
sudo pip install ansible
For Ubuntu, you would use:
sudo apt-get install software-properties-common sudo apt-add-repository ppa:ansible/ansible sudo apt-get update sudo apt-get install ansible
There are instructions on how to install Ansible on a number of different OSes in the excellent documentation.
Preparing the Control Machine
The control machine is the computer at which commands are to be issued, typed into the terminal window to put it simply.
The control machine then sends the commands to the computers on which the commands are to be executed. These computers are called the managed computers or managed nodes.
If you haven’t already manually logged in to the remote or virtual machines you want to control using Ansible from the control machine, now is a very good time to do so. The reason for this is that, in the configuration I have used, the SSH fingerprints of the remote or virtual machines must have been added to the list of known hosts on your control machine before attempting to use Ansible.
Preparing the Managed Nodes
Ansible requires Python 2.x to be installed on the managed nodes as well, which in my case is the nodes in my virtual laboratory. Installation on Ubuntu is trivial:
sudo apt-get install python
Configuring Ansible
I noticed that the directory /etc/ansible, which is to hold Ansible configuration files on the control machine, is not created when installing Ansible on OS X while on Ubuntu it is.
- If the directory /etc/ansible does not exist, create it.
sudo mkdir /etc/ansible
- Create or edit the hosts file in the Ansible configuration directory.
sudo vi /etc/ansible/hosts
- Add the following two lines to the Ansible hosts file:
[ubuntulab] 192.168.1.[180:183] ansible_user=ivan
This will create a group of computers that later can be referred to as “ubuntulab” when using Ansible.
The IP addresses of the nods in my virtual laboratory are 192.168.1.180, 192.168.1.181, 192.168.1.182 and 192.168.1.183, which can be shortened as above.
Update the above to reflect the IP addresses of your managed nodes. If you cannot specify a range, for instance if using DNS names for your managed nodes, then use one line per managed node. Change the name of the user used to log in to the manged nodes from “ivan” to the name of the user on your managed nodes. You may also want to change the group name of the managed nodes from “ubuntulab” to something more appropriate. - Save the modified Ansible hosts file.
Execute a Command
We now are ready to execute a command on the managed nodes.
ansible all -s -v -a "/bin/echo hello" --ask-pass
When asked, enter the password for the user you specified in the Ansible hosts file. If successful, output similar to the following will be output on the console:
192.168.1.181 | SUCCESS | rc=0 >> hello 192.168.1.180 | SUCCESS | rc=0 >> hello 192.168.1.182 | SUCCESS | rc=0 >> hello 192.168.1.183 | SUCCESS | rc=0 >> hello
The above executes the echo command on all the managed nodes in the Ansible hosts file. You may want to maintain different groups of computers and execute commands only on one group of nodes. To execute the echo command only on the nodes in the “ubuntulab” group, replace “all” with the name of the group:
ansible ubuntulab -s -v -a "/bin/echo hello" --ask-pass
That’s it! You are now ready to execute commands on your managed nodes. For instance, to check for updated packages on Ubuntu issue the following command:
ansible ubuntulab -s -v -a "sudo apt-get update" --ask-pass
This of course requires that the user specified in the Ansible hosts file is on the sudo list.
There is a lot more to Ansible – please refer to the Ansible documentation.
Happy coding!