How to install Ansible on Controller node and Manage a Google CE node
Here 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.
Test Environment
GCP Account
Fedora 32
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
If you are interested in watching the video. Here is the YouTube video on the same step by step procedure outlined below.
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.
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 package of Ansible provided by the linux system.
sudo pip install ansible
or
sudo dnf -y install ansible
Step3: Validate the Ansible installation
ansible --version
Output:
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.
Get the GCP CE instance external ip address for reference. In my case it is “35.196.18.237” with name “ansiblenode2”.
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
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
ssh sudhirbhoga@35.196.18.237
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.
Location: /etc/ansible
File: hosts
[stack]
35.196.18.237
Step8: Manage GCP Instance using Ansible Configuration management tool
We can mange 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
ansible stack -a "free -m" -u sudhirbhoga
Output:
35.196.18.237 | CHANGED | rc=0
total used free shared buff/cache available
Mem: 3737 231 3104 8 401 3285
Swap: 0 0 0
ansible stack -a "df -hP" -u sudhirbhoga
Output:
35.196.18.237 | 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 enjoyed reading this article. Thank you..
2 COMMENTS