Ansible is an open-source configuration management and software provisioning tool that can be used for controlling multiple remote hosts from the central location and this article will show you how to install Ansible on a Ubuntu 18.04 server.
Ansible can support on all major operating systems like, Linux, Unix, macOS, BSD and Windows. It is very similar to other configuration management systems like, Chef and Puppet. Ansible uses SSH to retrieve information from the remote machines and does not need any client agent on remote servers. You can also create automated tasks using Ansible playbooks to run on your servers.
Prerequisites
- Two fresh Ubuntu 18.04 server installed, one for Ansible server and second for Ansible hosts.
- The two server must be able to communicate each other. In our article, 192.168.0.4 is set up on the Ansible server and 192.168.0.3 is set up on the Ansible host.
- The root password of both servers.
Setup SSH Key-based Authentication
Before starting, you will need to configure SSH key-based authentication for Ansible hosts on Ansible server. So you will not require a password to manage Ansible hosts.
To do so, log in to Ansible server and generate an ssh key pair with the following command:
ssh-keygen -t rsa
You should see an output looking like this:
Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: SHA256:5JfGu2stiaee+oOud8NDAQVY0YJQ87iwKGud42rl6Oo root@ubuntu1804 The key's randomart image is: +---[RSA 2048]----+ | .oo++=. | | o+o . | | . . .o. | | . o . o.. . | |o . . S.= | |..... .o . | |..++ + ..o | |.o... o B.=.. | |*E...+o==Boo | +----[SHA256]-----+
Next, copy the generated public key to your Ansible hosts with the following command:
ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.0.3
Once the SSH key is added to the Ansible hosts, you should see the following output:
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub" /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys root@192.168.0.3's password: Number of key(s) added: 1
Now try logging into the machine, with:
ssh root@192.168.0.3
If it worked correctly, you can now access Ansible hosts from Ansible server using SSH without a password.
Install Ansible on Ubuntu 18.04
Next, you will need to install Ansible package on the Ubuntu Ansible server. By default, the latest version of Ansible is not available in the Ubuntu 18.04 repository.
First, add the Ansible repository with the following command:
apt-add-repository ppa:ansible/ansible
Then, install Ansible by just running the following command:
apt-get install ansible -y
After installing Ansible, you can check the version of Ansible with the following command:
ansible --version
You should see the following output:
ansible 2.8.2 config file = /etc/ansible/ansible.cfg configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules'] ansible python module location = /usr/lib/python2.7/dist-packages/ansible executable location = /usr/bin/ansible python version = 2.7.15+ (default, Nov 27 2018, 23:36:35) [GCC 7.3.0]
Configure Ansible Hosts
First, you will need to define Ansible hosts in /etc/ansible/hosts to monitor all the hosts.
nano /etc/ansible/hosts
Add the following lines:
[hosts] host1 ansible_host=192.168.0.3
Note: You can add the multiple hosts in the above file to monitor multiple hosts at a time.
Save and close the file.
Next, create a group named group_vars in /etc/ansible directory and create a files for each group you want to configure:
mkdir /etc/ansible/group_vars
nano /etc/ansible/group_vars/hosts
Add the port and user for remote hosts:
ansible_port: 22 ansible_user: root
Then, save and close the file, when you are finished.
Next, check all the host’s connectivity with the following command:
ansible -m ping hosts
If everything is fine, you should see the following output:
host1 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "ping": "pong" }
Working with Ansible
Now, you can easily monitor all the hosts defined in the hosts group easily.
To check the disk size of Ansible hosts, run the following command:
ansible -m shell -a 'df -h' hosts
You should see the following output:
host1 | CHANGED | rc=0 >> Filesystem Size Used Avail Use% Mounted on udev 1.9G 4.0K 1.9G 1% /dev tmpfs 384M 1.3M 383M 1% /run /dev/sda1 92G 6.1G 81G 7% / none 4.0K 0 4.0K 0% /sys/fs/cgroup none 5.0M 0 5.0M 0% /run/lock none 1.9G 70M 1.9G 4% /run/shm none 100M 48K 100M 1% /run/user /dev/sda5 184G 67G 108G 39% /home /dev/sda6 179G 12G 159G 7% /Data
To check the uptime of Ansible hosts with the following command:
ansible -m shell -a 'uptime' hosts
You should see the following output:
host1 | CHANGED | rc=0 >> 11:14:45 up 1:18, 5 users, load average: 0.89, 1.11, 1.45
Conclusion
Congratulations! you have successfully installed and configured Ansible server and ansible hosts on Ubuntu 18.04 server. You can also configure Ansible to manage and monitor multiple hosts easily.
For more information, refer the Ansible official doc at https://docs.ansible.com/ansible/latest/index.html