Creating an Ansible Playbook which will dynamically load the variable file named same as OS_name and Configuring Web Server.

Creating an Ansible Playbook which will dynamically load the variable file named same as OS_name and Configuring Web Server.

In this Article, I'll show you how to create an Ansible Playbook which will dynamically load the variable file named same as OS_name and Configuring Web Server.

In this task, we have to create a dynamic var files , with name of remote server which we will used in the playbook. For this task I'm using two AWS EC2 instance, one is AMI2 and other is Ubuntu, you can also use your own local system for this task.

Let's Start Implementation :-

Prerequisites :-

  • Ansible must be installed in controller node
  • Managed nodes login name, password or key and IP must be known to Controller Node's user

1- Launching Instance :-

No alt text provided for this image

2- Configuring Host File :-

No alt text provided for this image

Here we give the user name according to the distribution used and also need same aws key pair which has been used at the time of launching instance. Give your key name in the .pem format (that key-pair file should be within permission of 600).

3- Configuring ansible.cfg file

[defaults]
inventory = /etc/ansible/hosts
host_key_checking = False
deprecation_warnings = False
command_warnings = False

[privilege_escalation]
become = True
become_method = True
become_user = root
become_ask_pass = False

4- Now Playbook and other required files :-

(A) Creting "Amazon.yml" file -

No alt text provided for this image

(B) Creting "Ubuntu.yml" file -

No alt text provided for this image

(C) Creating Web Page "var.html" -

No alt text provided for this image

(D) Creating Ansible Playbook "setup.yml" -

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


  tasks:
          - name: Installing Web Server
            package:
                    name: "{{  package  }}"
                    state: present
          
          - name: Copying Web Pages
            template:
                    src: var.html
                    dest: "{{  doc_root  }}"
          
          - name: Starting Web Services
            service:
                    name: "{{  service  }}"
                    state: started

5- Running the Playbook :-

No alt text provided for this image

Now, Let's check if the Web Services are running or not on both instances.

  1. For Ubuntu
No alt text provided for this image
No alt text provided for this image

2. For Amazon

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

That's all

I hope this article will help you to understand the concept behind it.

Thank you for Reading .....!!!!!!!!!!!

No alt text provided for this image


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

Mohit Singh Tomar的更多文章

社区洞察

其他会员也浏览了