Test Environment –
What is Ansible –
– Anisble is purely based on Python
– Its a Configuration Management tool used to configure and manage the Linux system for all the types of operations like Installating, Patching, Deployment of Applications, File Management, Service Management, Package Management
– Ansible using the SSH protocol based on the native OpenSSH package for communication with the Linux nodes which it manages
In this article we will see how we can install Ansible and spin up a Google compute engine which we will try to manage using the Ansible as a Managed node. If you are interested in watching the video. Here is the youtube vidoe with the same step by step procedure.
Step1: Install the required dependencies
Ansible is dependent on Python. By default most of the Linux system are having the Python package already installed. But if its not installed lets get that installed with the latest available version along with the Python pip and Python-devel package on our system.
|Install Ansible dependencies|
[admin@fedser ~]$ sudo dnf install -y python3 python-pip python3-devel
Step2: Install Ansible using pip
Ansible can be installed using different methods, one of them using the Python pip tool and other is installing the rpm packcage of Ansible provided by the linux system.
|Install Ansible using pip or rpm package|
[admin@fedser ~]$ sudo pip install ansible or [admin@fedser ~]$ sudo dnf -y install ansible
Step3: Validate the Ansible installation
|Validate Ansible Installation|
[admin@fedser ~]$ ansible --version ansible 2.10.7 config file = None configured module search path = ['/home/admin/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules'] ansible python module location = /usr/local/lib/python3.8/site-packages/ansible executable location = /usr/local/bin/ansible python version = 3.8.7 (default, Jan 20 2021, 00:00:00) [GCC 10.2.1 20201125 (Red Hat 10.2.1-9)]
Step4: Spin up a GCP Compute Engine Instance
Here you need to have the GCP account. If you dont have any sign up and register for the free tier account which provides $300 credit for 90 days and you can use GCP to spin up a Compute Engine Instance.
ansiblenode2 external IP – 22.214.171.124
Step5: Generate SSH private and public key using ssh-keygen
In this step we are going to generated SSH key pair. The public key which gets generated will need to be copied into the instance metadata for managing the GCP Compute instance using SSH remotely.
|Generate ssh key pair|
[admin@fedser ~]$ ssh-keygen -t rsa -f ~/.ssh/gcpssh -C sudhirbhoga
Now, lets copy the SSH public key to the GCP instance metadata ssh keys
Step6: Validate that Remote SSH login to GCP instance
You can check if you are able to login with SSH remotely. If you face any issue check if the SSH public key is properly uploaded into the metadata section of the Compute Engine instance also check if there is no firewall blocking on port 22.
|Validate SSH login to remote GCP instance|
[admin@fedser ~]$ ssh email@example.com
Step7: Create an inventory file and updated the ansiblenode2 external IP address
This inventory file is used by ansible by default to get the list of servers that it is going to manage.
|Create and Update the ansible inventory file with GCP instance Hostname or IP address|
[admin@fedser ~]$ cat /etc/ansible/hosts
126.96.36.199 192.168.47.128 188.8.131.52
Step8: Manage GCP Instance using Ansible Configuration management tool
We can maange and run ad-hoc commands on to get the information related to our GCP instance as shown below.
|Check free space and disk space using ansible ad-hoc commands|
[admin@fedser ~]$ ansible stack -a "free -m" -u sudhirbhoga ...} 184.108.40.206 | CHANGED | rc=0 >> total used free shared buff/cache available Mem: 3737 283 2908 8 545 3223 Swap: 0 0 0 220.127.116.11 | CHANGED | rc=0 >> total used free shared buff/cache available Mem: 3737 231 3104 8 401 3285 Swap: 0 0 0 [admin@fedser ~]$ ansible stack -a "df -hP" -u sudhirbhoga ... 18.104.22.168 | CHANGED | rc=0 >> Filesystem Size Used Avail Use% Mounted on devtmpfs 1.9G 0 1.9G 0% /dev tmpfs 1.9G 0 1.9G 0% /dev/shm tmpfs 1.9G 8.4M 1.9G 1% /run tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup /dev/sda2 20G 2.5G 18G 13% / /dev/sda1 200M 6.9M 193M 4% /boot/efi tmpfs 374M 0 374M 0% /run/user/1001 22.214.171.124 | CHANGED | rc=0 >> Filesystem Size Used Avail Use% Mounted on devtmpfs 1.9G 0 1.9G 0% /dev tmpfs 1.9G 0 1.9G 0% /dev/shm tmpfs 1.9G 8.4M 1.9G 1% /run tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup /dev/sda2 20G 2.5G 18G 13% / /dev/sda1 200M 6.9M 193M 4% /boot/efi tmpfs 374M 0 374M 0% /run/user/1000
Hope you learnt something in this article. Thank you..