Ansible – How Default Ansible Variables Work (Part-10)

How Default Variables Work

1.Default Variables in a Role

Default variables for a role are defined in the defaults/main.yml file. These variables provide baseline values that the role will use unless they are explicitly overridden elsewhere.

Example: roles/nginx/defaults/main.yml

---
server_name: localhost
proxy_pass: 127.0.0.1:8080

2.Overriding Default VariablesYou can override these default values in several ways:

  • Playbook Variables
  • Inventory Variables
  • Extra Vars (Command Line)

Overriding Default Variables in Different Contexts

  1. Overriding in a PlaybookYou can override the default values by defining the variables in the playbook that includes the role.Example: site.yml
---
- name: Configure web servers with custom settings
  hosts: webservers
  become: yes
  vars:
    server_name: example.com
    proxy_pass: backend.example.com:8080
  roles:
    - nginx

2.Overriding in an Inventory File

You can override the default values by defining the variables in the inventory file for specific hosts or groups.

Example: inventory.ini

[webservers]
server1 ansible_host=192.168.1.10 server_name=example.com proxy_pass=backend.example.com:8080
server2 ansible_host=192.168.1.11 server_name=example.org proxy_pass=backend.example.org:8080

3.Overriding with Extra Vars (Command Line)

You can override the default values by providing variables on the command line when running the playbook.

Command Line Example

ansible-playbook site.yml -e "server_name=example.com proxy_pass=backend.example.com:8080"

Example Role with Default and Overridable Variables

Let’s create a complete example to demonstrate how default values can be overridden.

  1. Role Structure
roles/
└── nginx/
    ├── defaults/
    │   └── main.yml
    ├── tasks/
    │   └── main.yml
    ├── templates/
    │   └── nginx.conf.j2
    └── meta/
        └── main.yml

2.Default Variables

roles/nginx/defaults/main.yml

---
server_name: localhost
proxy_pass: 127.0.0.1:8080

3.Tasks

roles/nginx/tasks/main.yml

---
- name: Install Nginx
  ansible.builtin.apt:
    name: nginx
    state: present

- name: Configure Nginx
  ansible.builtin.template:
    src: nginx.conf.j2
    dest: /etc/nginx/sites-available/default
  notify:
    - Restart Nginx

- name: Ensure Nginx is running
  ansible.builtin.service:
    name: nginx
    state: started

4.Template

roles/nginx/templates/nginx.conf.j2

server {
    listen 80;
    server_name {{ server_name }};

    location / {
        proxy_pass http://{{ proxy_pass }};
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

5. Playbook

site.yml

---
- name: Configure web servers
  hosts: webservers
  become: yes
  vars:
    server_name: mywebsite.com
    proxy_pass: backend.mywebsite.com:8080
  roles:
    - nginx

6.Inventory

inventory.ini

[webservers]
webserver1 ansible_host=192.168.1.10 server_name=mywebsite.com proxy_pass=backend.mywebsite.com:8080
webserver2 ansible_host=192.168.1.11 server_name=myotherwebsite.com proxy_pass=backend.myotherwebsite.com:8080

Running the Playbook

  1. Using Default VariablesIf you run the playbook without any additional variable overrides, it will use the default values specified in defaults/main.yml.
ansible-playbook -i inventory.ini site.yml

2.Overriding in the Playbook

If you define variables in the playbook, those values will override the defaults.

---
- name: Configure web servers
  hosts: webservers
  become: yes
  vars:
    server_name: mywebsite.com
    proxy_pass: backend.mywebsite.com:8080
  roles:
    - nginx

3.Overriding in the Inventory File

If you define variables in the inventory file, those values will override the defaults for specific hosts.

[webservers]
webserver1 ansible_host=192.168.1.10 server_name=mywebsite.com proxy_pass=backend.mywebsite.com:8080
webserver2 ansible_host=192.168.1.11 server_name=myotherwebsite.com proxy_pass=backend.myotherwebsite.com:8080

4.Overriding with Extra Vars

If you provide variables on the command line, those values will override both the defaults and any playbook or inventory variables.

ansible-playbook -i inventory.ini site.yml -e "server_name=commandline.com proxy_pass=backend.commandline.com:8080"

Conclusion

The ability to define default values and override them as needed is a powerful feature in Ansible that allows for flexible and adaptable configurations. This approach helps in managing different environments and scenarios without modifying the core role, promoting reuse and maintainability. Understanding and effectively using default values and overrides is key to writing robust and flexible Ansible roles and playbooks.

Related Posts

Ansible Terminology

Here are simplified one-line explanations for key Ansible terminology: Mohammad Gufran Jahangir

Read More

Ansible interview questions that are commonly asked in interviews

Basic Questions Intermediate Questions How does Ansible handle authentication? What is the purpose of ansible.cfg? What are handlers in Ansible? What is Ansible Galaxy? Explain the difference…

Read More

Ansible Playbook for Patching & Upgrading Different Services

Playbook for Patching & Upgrading Linux APP Servers reating an Ansible playbook for patching and upgrading Linux application servers involves several steps. Below is a detailed playbook,…

Read More

Ansible – Vault in Ansible (Part-11)

What is Ansible Vault? Ansible Vault is a feature within Ansible that allows you to keep sensitive data, such as passwords, keys, and other secrets, secure. Vault…

Read More

Ansible – Inventory variables in Ansible (Part-11)

What are Inventory Variables in Ansible? Inventory variables in Ansible are key-value pairs that provide additional context and configuration for hosts and groups defined in your inventory….

Read More

Ansible – Ansible Templates (Part-9)

We can declare variables in Which can be used & Intropolate only in playbook Tasks but not in the files used in playbook. Solution: TemplateUsing Template module…

Read More
guest
0 Comments
Oldest
Newest Most Voted
0
Would love your thoughts, please comment.x
()
x