Skip to main content

How to setup Ansible and setup a lab of simple Ansible Playbook?

 


How to install Ansible on Ubuntu 18.04/16.04/14.04



First step: Update the Ubuntu packages
sudo apt-get update ; apt-get upgrade


Now install Ansible by simple command
sudo apt-get install ansible


Verify the Ansible and Python Installed or not
ansible --version
python --version


Create an inventory file where added all the host.


Like – inventory
[webserver]
52.66.239.76
52.66.235.128
13.233.67.129
# 13.233.122.239 ansible_user=ec2-user
# 13.127.93.201 ansible_user=ec2-user


Before proceed. We will generate and ssh public to which will be added to our all host in authorize_keys. To generate the ssh public key on Ansible machine. We will run the following command.
ssh-keygen


This command with generate a id_rsa.pub and id_rsa key under .ssh folder. We will copy the contents of id_rsa.pub and paste in each hosts mentioned under inventory file and append the contents under host user .ssh/authorize_keys.

Make sure host_key_ckecking = flase is to be uncommented. (This line is in file under /etc/ansible/ansible.cfg)



Ansible Simple Playbook
---
- hosts: webserver
tasks:
- name: Install Apache package in Ubuntu machine
apt: name=apache2 state=present
- name: Start apache server and enable on boot mode
service: name=apache2 state=started enabled=yes



To check syntax error in Ansible Playbook file
ansible-playbook webserver.yml --syntax-check



To Check the no. of hosts will be used in particular playbook
ansible-playbook webserver.yml --list-host


To dry run (means check before run playbook) the playbook, this is for testing. If everything is working fine or not
ansible-playbook webserver.yml --diff --check


To run playbook on a particular host among all the host mentioned in hosts file
ansible-playbook webserver.yml -l '13.233.67.129'



How to run Ansible Playbook
ansible-playbook -i  


Ansible Playbook with Notify and Handler (copy the apache conf file to host machine)
---
- hosts: webserver
tasks:
- name: Install Apache package in Ubuntu machine
apt: name=apache2 state=present
- name: copy file to remote server
template: src=apache2.conf dest=/etc/apache2/apache2.conf
notify:
- restart apache

handlers:
- name: restart apache
service: name=apache2 state=restarted enabled=yes


Call variable in Ansible


Here we have changed the variable in apache.conf file Timeout 300 to Timeout {{Timeout}} and when we run the playbook it will change the variable in run level
ansible-playbook -i   -e 'Timeout=600'


In template, We can call the variables but in file we can’t call variables.
---
- hosts: webserver
tasks:
- name: Install Apache package in Ubuntu machine
apt: name=apache2 state=present
- name: copy file to remote server
file: src=apache2.conf dest=/etc/apache2/apache2.conf
notify:
- restart apache

handlers:
- name: restart apache
service: name=apache2 state=restarted enabled=yes


To check the exactly where are we getting a the error. This is we used for debugging
ansible-playbook webserver.yml -vv

Here as many vv we will use that shows that level the errors.

Popular posts from this blog

WordPress Site is not loading properly behind Google Cloud/AWS Load Balancer

Hello Guys, Today we are going to understand how can we fix a WordPress loading issue (CSS and JS loading issue) behind the Google Cloud Load Balancer or AWS Load Balancer. Generally, When we host a WordPress site directly with Google Cloud Compute Engine VM instance or AWS EC2 instance. It's loading perfectly fine. But once we added this WordPress server behind any Load Balancer either from GCP Load Balancer or AWS Load Balancer, you site will completely broken, means the CSS and JS of your site not loaded properly. The reason for this is - When you put a load balancer in front of WordPress, you need to modify wp-config.php to process the HTTP header HTTP_X_FORWARDED_PROTO to detect the protocol that the user is using and not the protocol the load balancer is using to connect to your backend. To fix this issue, we have to make following changes in the  wp-config.php  file and add the below code snippet on the top of  wp-config.php file - Google Cloud Platform Load Bal...

How to Setup Kubernetes Cluster in Google Cloud Virtual Machine using "kubeadm"? | Ubuntu 20.04/22.04 LTS

  Hello Friends, In this post, we are going to setup Kubernetes Cluster on Virtual Machine in Google Cloud Platform using kubeadm tool. Hope this post will help you in Kubernetes learning Hand-On Labs (HOL). Requirements: Master Node: No. of VMs 1 Specifications - 2 vCPUs, 4GB RAM, 20 GB HDD (Balanced PD or SSD PD), Operating System (OS) Ubuntu 20.04 LTS x86/64, amd64  Firewall Rule - Ingress Allow 6443 (API Server) | 2379 (ETCD) | 10251 (Scheduler) | 10252 (Controller Manager) 10250 (Kubelet), sudo access with admin access  Worker Node: No. of VMs 2 Specifications - 2 vCPUs, 4GB RAM, 20 GB HDD (Balanced PD or SSD PD), Operating System (OS) Ubuntu 20.04 LTS x86/64, amd64  Firewall Rule - Ingress Allow 30000-32767 (Services) | 10250 (Kubelet),  sudo access with admin access  Disable Swap and comment fstab entry: First, Disable Swap and remove or comment the Swap entries from fstab file : sudo swapoff -a sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab Next...

How to Convert PEM KeyFile into PPK KeyFile and vice versa?

Hi, In this post, we will learn that how can we convert PEM KeyFile into PPK KeyFile and vice versa including the installation of the tool which we will use to accomplish this. For the Windows Operating System In Windows system is easy and straight forward. You just need a tool called "puttygen.exe" from the URL -  https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html Choose the appropriate version as per your OS. Convert PEM into PPK in Windows After downloading the PuTTYgen and open it, load your file (select All Files (*.*) in the bottom dropdown to select your PEM key file).  You will get a message Successfully imported foreign key, Just click OK, Now click on Save private key button to save that key in the location where do you want to save it. Convert PPK into PEM in Windows Now we will convert PPK file into PEM using PuTTYgen in Windows PC Open the PuTTYgen tool, now load your PPK file into it, and finally, Go to Conversions Menu and Click on Export OpenSSH k...