ARTH - Task 14.3 ??????
Gaurav Rathi
Cloudops Engineer | RHCSA | RHCE | RHCoE(CEPH) ,(OpenShift) | Docker | Ansible | Kubernetes | OpenStack | AWS | MLOps | Jenkins |
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
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
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