# How to load variable dynamically according to os in ansible?

#### Ansible 10

### Problem Statement:

Create an Ansible Playbook which will dynamically load the variable file named the same as OS\_name and just by using the variable names we can Configure our target node.  
(Note: No need to use when keyword here.)

Let’s understand the use case of this problem. There will be many cases when some of the package names are different Linux OS distributions. Also, there will be cases that they are different file formats only such as deb or rpm.

To solve this problem, Ansible offers us one module named “include\_vars” which loads variables from files dynamically within the task. This module can load the YAML or JSON variables dynamically from a file or directory, recursively during task runtime.

To detect on which operating system we are performing tasks, we can use the “setup” module. This module is automatically called by playbooks to gather useful variables about the remote hosts that can be used in playbook. This module is also available for windows target.

Let’s take an example of installing an Apache web server in CentOS and Ubuntu Linux. We do not want a hard-coded playbook installing a web server for a different distribution, OS, etc.

First, find out which variables setup module offers use by which we can detect remote host distribution, and accordingly we can load the variable file on the fly.

First check, can we connect both the system using the ping module.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685729104689/d7097df0-02e3-46d0-9299-a7e0e88c11dd.png align="left")

Now, we have connectivity to both the servers, we have to get that variable name which gives us a remote host type variable. The setup module gives many variables under the “ansible\_distribution” section. so let's see what variable it gives.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685729107570/bf99a674-a217-4284-b1a5-3295578848ad.png align="left")

Now, we know the distribution name and major version, we will create variables files and give this distribution name and version name to that files. eg: Ubuntu-18.yml, CentOS-8.yml

Also, you can use the os\_family variable which gives the os\_family of remote hosts.

Create Ubuntu-18.yml file and add the below variables in that file

```plaintext
*# Ubuntu-18.yml*  
package_name: apache2  
service_name: apache2  
document_root: /var/www/html
```

Create CentOS-8.yml file and add below variables

\# CentOS-8.yml package\_name: httpd service\_name: httpd document\_root: /var/www/html/

```plaintext
*# CentOS-8.yml*  
package_name: httpd  
service_name: httpd  
document_root: /var/www/html/
```

Now we have to write a playbook that will install a webserver and read variables dynamically according to distribution type.

Let's first make sure, we are using selecting proper files using ansible facts. so for that, we will use debug module to create a final string.

```plaintext
---  
- name: "Install webserver"  
  hosts: webserver  
  tasks:  
  - name: "Test variables"  
    debug:  
      msg: "{{ ansible_distribution }}-{{ ansible_distribution_major_version }}.yml"
```

Run this playbook, `$ ansible-playbook main.yml`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685729109479/be0744df-e949-4762-b743-2335d5a0a634.png align="left")

Now let’s write a playbook that will read the variables according to the OS type of remote host and install the webserver.

```plaintext
---  
- name: "Install webserver"  
  hosts: all  
  vars_files:  
     - "{{ ansible_distribution }}-{{ ansible_distribution_major_version }}.yml"  
    
  tasks:  
    - name: "Install the web server"  
      package:  
              name: " {{ package_name }}"  
              state: present  
      
    - name: "Create document root directory"  
      file:   
          path: "{{document_root }}"  
          state: directory  
          recurse: yes
```

```plaintext
 - name: "Create index.htm page in document root"  
      copy:  
              content: "<h1> Welcome to {{ ansible_distribution }} server !! </h1>"  
              dest: "{{ document_root }}/index.html"  
      
    - name: "Start the service"  
      service:  
              name: "{{ service_name }}"  
              state: started
```

```plaintext
- name: "Test the servers"  
  hosts: localhost  
  tasks:  
          - name: "HealthCheck the servers"  
            uri:  
               url: "http://{{item}}"  
               return_content: yes  
            with_items: "{{ groups['webserver'] }}"  
            register: output  
            failed_when: '"Welcome" not in output.content'
```

The above playbook will install and test our web server is working properly or not.

`$ ansible-playbook main.yml`

We can test the installation is successful or not using the curl command.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685729112200/bf2111da-c846-40c3-a6a9-004ee167dbaa.png align="left")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685729114881/60c69613-bbe1-4f90-b72e-3d9bbcd8a29e.png align="left")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685729116120/fd97b3c2-d33c-47e2-a77d-6d15ba8440e4.png align="left")

### Conclusion:

We have successfully made our playbook variables dynamically according to distribution and major version number without using the when keyword.

### Thank you…

***About the writer:***  
***Shubham*** *loves technology, challenges, continuous learning, and reinventing himself. He loves to share his knowledge and solve daily problems using automation.*

*Visit him:* [*blog.shubhcodes.tech*](https://blog.shubhcodes.tech) *to know more.*
