Linux OS / Servers / Software

Automate Your System Administration Tasks with Ansible – vMware Lab

Share this post

Do you want to automate your system administration or Linux skills?Perhaps you have local LAN and dozens of hosts in it, you want to make your life easier? In this article, i will explain how to set up small home lab and testing automatization on multiple machines ( three ).

if you need tools for automatization everyone meets with SaltStack, Puppet, Chef, and Ansible. This is a most popular options. Throughout this article, i will focus on Ansible and explain how to set up it and use on small home lab ( three vMware hosted machines ).

First we need to install vMware Workstation and boot three systems, in my case this will be 2x Lubuntu and 1x Kali Linux. You can try with some other OS’s but in this turotial i will use those. Every Lubuntu OS need to have 1GB of RAM and 20Gb HDD minimum – 1 processor and Network Adapter with Bridged: Connected Directly to the Physical Network checked.

For Kali i will use 2Gb of RAM and 20 Gb HDD with 2 processors and Newtork Adapter Settings like on Lubuntu machines. Since we used Debian based distributions we need to use code like below to install Ansible. We use Kali OS host for install Ansible.

sudo apt-get install software-properties-common -y 
sudo apt-add-repository ppa:ansible/ansible 
sudo apt-get update 
sudo apt-get install ansible -y

When we finished installation we need to check version of ansible using command #ansible –version. After we successfully install Ansible we need to edit some files and add hosts which we want to use and automate. First we need to check ansible config file ( ansible.cfg ). Ansible find this file at the following locations:

  • ANSIBLE_CONFIG (environment variable if set)
  • ansible.cfg (in the current directory)
  • ~/.ansible.cfg (in the home directory)
  • /etc/ansible/ansible.cfg

Ansible will process the above list and use the first file found, all others are ignored.

In this tutorial we will change ansible.cfg on /etc/ansible/ansible.cfg location. When we open the file we will see some default values. In this tutorial we do not change too many values, only values that will be important for our tutorial. First we will uncomment inventory, this is the important for ansible to know which hosts he has available to talk to,  other values we don’t touch.

Configure ansible.cfg - uncomment inventory value
Configure ansible.cfg – uncomment inventory value

After we setup ansible.cfg we need to add some hosts. For that step we need to edit /etc/ansible/hosts. In this file on the bottom, we need to create our group of hosts and add hosts. In the picture below we can see example ( [demo_hosts] ).

Configure Ansible Hosts - Create Group and Add Hosts
Configure Ansible Hosts – Create Group and Add Hosts

We also need to edit /etc/hosts file and add our hosts from /etc/ansible/hosts in this file too. Best way is to add ip address and computer name in hosts file. In our tutorial that looks like on picture below.

Add Hosts in /etc/hosts
Add Hosts in /etc/hosts

Now we can try to ping our host or group of hosts. The command for that is #ansible -m ping all . Probably ping will not be successful because ssh connection is failed. To check that you can use command #ssh administrator@lub1( in our case ) and see message below. If message “Permission denied (publickey)” then we need to take the following steps.

First create key.pem, to create that we need to type command:

openssl req -newkey rsa:2048 -new -nodes -x509 -days 3650 -keyout key.pem -out cert.pem

We must check active services on hosts and see is ssh active or is it even instaled on hosts, if not just type following command in terminal #sudo apt-get  install openssh-server -y and install it. Then try again to start ssh conection with host.

Also in terminal go to root and type #eval `ssh-agent -t`after that type #eval `ssh-agent -s` than we will see message “Agent pid {number}” which means that agent is started. Now we need to type command #ssh-add key.pem and we can use ping to check our host or group of hosts. Using this command below we can check that ( see picture below ).

ansible -m ping all --ask-pass
Ping all Ansible hosts
Ping all Ansible hosts successful

Finishing this steps we can try to use some Ansible command. In our case we will try to copy one file to all hosts from Ansible server ( Kali ). To do that we will create example file on our Desktop and name that file “text.txt” in that file we will type “Hello World” and using following command to copy this file to all other machines.

ansible demo_host -m copy -a "src=/home/anonymus/Desktop/text.txt dest=/home/administrator/Desktop/text.txt"

We can take care to use right path in our command or will see an error. After successfully execute command we will see message like on picture below.

Copy File To All Ansible Hosts
Copy File To All Ansible Hosts

This is some simple and effective way to automate your tasks with ansible. We have seen a how simple command can make the life of the system administrator easier. If you need some advanced command or possibility you can check Ansible Docs, there you can find all useful thing about Ansible and how to use its full potential.

Related Stories