ARTH - Task 14.3 ???????

ARTH - Task 14.3 ??????

Task Description:

Create an Ansible Playbook which Dynamically Loads Variable Filename same as OS_Name and just by using the variable names we can configure our target node. Without using when a keyword

In this tasks, we are going to launch the httpd web server in more than two OS with the same playbook or without using the WHEN keyword we can achieve this scenario easily while using the WHEN keyword but here we don't use the when keyword so here we need to do some research so I find that we can also do this same task with help of vars_file keyword or by using ansible_facts they both play a very virtual role for doing this tasks.

Ansible_facts:

Ansible collects pretty much all the information about the remote hosts as it runs a playbook. The task of collecting this remote system information is called as Gathering Facts by ansible and the details collected are generally known as facts or variables. This information can be obtained manually using Ansible ad-hoc command and a specialized module named setup.  In fact, Ansible playbooks call this setup module by default to perform the Gathering Facts task.

ansible <hostname (or) hostgroup> -m setup


vars_files:

vars_file directive can only be used when defining a play to specify variable files. The variables from those files are included in the playbook. Since it is used in the start of the play, it most likely implies that some other play(before this play) created those vars files or they were created statically before running the configuration; means they were kind of configuration variables for the play.

 I am using the RedHat-8 and Ubuntu-20 operating systems to perform this, but you can perform any type of operating system. but in reality, I have only redhat we can do the same step for ubuntu if that works for redhat it means it also works for ubuntu also.

So here we have to create 5 files ansible_conf file, inventory_file, RedHat-8.yml file, and Ubuntu-18.yml file and at last, our main web playbook that will perform our tasks.

so let's see our inventory file first where I provide only localhost IP to do these tasks.

[h]
192.168.43.64 ansible_ssh_user=root ansible_ssh_pass=redhat ansible_connection=ssh

this is our inventory file where we give the user name and password to enable login and also provide connection type means by which it can connect.

After this Let's see the Ansible_conf file

[defaults]
inventory = /root/ansible/task14.3/ip.txt
host_key_checking=false
remote_user=root
roles_path=/root/ansible/role/


[privilege_escalation]
become=true
become_method=sudo
become_user=root
become_ask_pass=false

this is our ansible config file here we declare all the required ting so that we can efficiently perform our task

After this lets Index.html.j2 file this file we can copy via a template to our web server which will tell the clients which Server is in backend.

 cat index.html.j2
This is a WEB_PAGE {{ ansible_facts['distribution'] }}!!

After our playbook run successfully we can see this file on our browser with the server name in place of the distribution

SO after all this let's see our Redhat-9 os yml file where we declare Packagename document root and Service name that we have to start

 cat RedHat-8.yml
p_name: httpd
s_name: httpd
root_path: /var/www/html/

Here we declare the Package name and document path also on which our web-server is running

Let's see for Ubuntu-18

 cat Ubuntu-18.yml
p_name: apache2
s_name: apache2
root_path: /var/www/html/

as we see here the name is apache2 Apache 2 and httpd is developed by the same and its work is the same but for this case the only name is diff.

Now let's see the Main playbook

 cat web.yml
- hosts: all
  vars_files:
          - "{{ ansible_facts['distribution'] }}-{{ ansible_facts['distribution_major_version'] }}.yml"


  tasks:
          - name: install web-server software
            package:
                    name: "{{ p_name }}"
                    state: present
          - name: copy config file
            template:
                    dest: "{{ root_path }}/index.html"
                    src: index.html.j2
          - name: starting service
            service:
                    name: "{{ s_name}}"
                    state: restarted

this is our main web file or we can say playbook and in this the main work of vars_file keyword that we use in first that will take the name of the file from the ansible facts and then load that particular file so that we use there variable to perform our task like installation of software and start of service if Our os is Ubuntu then Ubuntu file will come and give there variable and if the os is redhat then redhat file come..

Let's see some op of Playbook running and what we get output if run this playbook on RedHat OS

No alt text provided for this image
No alt text provided for this image
No alt text provided for this image

As we see our Playbook is run successfully so It means Our Vars_file Call some os file that we will provide to us so lets check what it gets and is that our httpd service is started or not for go to Browser and check is that all ok

No alt text provided for this image


It's working fine It will take the correct OS name as we use RedHat os so it Pulls RedHat yml files and it also uploads to config files so all ok we install webserver soft on two OS without using when keyword by the use of ansible_facts and vars_file keywords.


Thanks, ForReading

Have A Nice Day

要查看或添加评论,请登录

Gaurav Rathi的更多文章

  • GUI Application inside Docker

    GUI Application inside Docker

    Docker containers run as a process under the main operating system, you can run multiple containers on a single…

  • Industry Use-Cases of Jenkins

    Industry Use-Cases of Jenkins

    In this article, we are going to learn about the DevOps tool and the name of that tool is Jenkins This is the main tool…

  • Research for Industry Use-Cases of Openshift

    Research for Industry Use-Cases of Openshift

    In this article we are going to learn about OpenShift and how it works and what is the need for these tools in the…

  • NETFLIX-AWS CASE STUDY

    NETFLIX-AWS CASE STUDY

    In this blog, we are going to learn how Netflix uses AWS cloud to run its business. AWS provides resources to Netflix…

  • Industry Use-Case of Neural Networks(NN)

    Industry Use-Case of Neural Networks(NN)

    What is a Neural Network? Neural networks can be taught to perform complex tasks and do not require programming as…

  • AKS-Azure Kubernetes Service

    AKS-Azure Kubernetes Service

    AKS is a good example of INFRASTRUCTURE as a Service and PLATFORM as a Service. Here as we know Azure is a public…

  • Summary of Expert Session Delivered on Kubernetes | Container | Openshift

    Summary of Expert Session Delivered on Kubernetes | Container | Openshift

    Hello Connections, “Expert session on Industry use cases of Kubernetes/OpenShift ” was one of the great sessions. Which…

  • Git and GitHub Workshop

    Git and GitHub Workshop

    What is GIT? Git is a distributed version control system for tracking changes in any set of files, originally designed…

  • ARTH - Task 14.2 ??????

    ARTH - Task 14.2 ??????

    *Task Description*?? ?? *14.2* Further in ARTH - Task 10 has to create an Ansible playbook that will retrieve new…

  • ARTH - Task 17 ??????

    ARTH - Task 17 ??????

    Task Description?? ?? Create your own Chat Servers, and establish a network to transfer data using Socket Programing by…

社区洞察

其他会员也浏览了