Whether you are a sysadmin looking for a better way to automate deployment of a software or a tester looking to automate installation tests, a configuration management tool such as Ansible is worth exploring!
I have been recently exploring whether to use Ansible Playbooks or Docker containers or both for installation testing. In this blog post, I will give an overview on installing and configuring Ansible on CentOS 7.
Before getting started let me acquaint you with some terminology and concepts that I will use in the later sections of this blog post:
The configuration management scripts of Ansible are called playbooks. It uses YAML syntax, a simple data serialization language and hence easy to read and write.
The machine where Ansible is installed
I found the setup required for managing a server with Ansible to be quite simple and there is detailed documentation available too.
Make sure that the CentOS 7 EPEL repository is installed:
sudo yum install epel-release
Once the repository is installed, install Ansible with yum:
sudo yum install ansible
Configuring Ansible Hosts
Before the controller machine starts communicating to other machines, “hosts” file needs to be made aware of which servers the Ansible needs to keep track of.
Open the file as root:
sudo vi /etc/ansible/hosts
The syntax looks something like this:
- group_name = one word tag to refer to any servers listed under it
- alias = name to refer to the server that the controller machine would talk to
Example hosts file
Create a file with the same name as group_name in /etc/ansible/group_vars/ to tell Ansible as which user it should connect to the “servers” group.
sudo mkdir /etc/ansible/group_vars
sudo vi /etc/ansible/group_vars/servers
In “servers” file add the following lines
– – –
If you do not already have an SSH key pair (which consists of a public and private key) for the user used by Ansible to connect to the “servers” group, you need to generate one.
On the controller machine as the user, generate a new key pair, by entering the following command:
Assuming you are root, you will see output that looks like the following:
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
You can either accept the file name and path or enter a new name. Next, you will be prompted for a passphrase to secure the key with and you can either enter a passphrase or leave the passphrase blank.
After generating an SSH key pair, copy your public key to your host server by running the ssh-copy-id script and providing your password when prompted
Your public key would be now added to the remote user’s .ssh/authorized_keys file.
Make sure that the user used by the Ansible on the controller machine to connect to remote hosts exists on the remote system. Otherwise a connection attempt will result in a connection error.
Check whether Ansible has connection to its hosts
You can issue a command of the following syntax
ansible -m ping X
Where, X = single host, group_name or all
- ansible -m ping Centos7VM
- ansible -m ping servers
- ansible -m ping all
For creating a basic Ansible playbook and explore further, see the official intro to Playbooks.
Have fun with Ansible!