Ansible Variables with Examples

In this article we are going to cover Ansible Variables with Examples, How to declare a valid variable in Ansible, Define variables in Ansible playbook,Create new file for variable in Ansible.

What is Ansible Variables?

Ansible uses variables to manage differences between systems. With Ansible, you can execute tasks and playbooks on multiple different systems with a single command. To represent the variations among those different systems, you can create variables with standard YAML syntax, including lists and dictionaries. 

You can define these variables in your playbooks, in your inventory, in re-usable files or roles, or at the command line. You can also create variables during a playbook run by registering the return value or values of a task as a new variable.

Ansible Variable Declare Types

There are three ways to declare a variable in ansible

  1. Playbook with vars
  2. Create new file for variable
  3. Declaring variable in the inventory file

How to declare a valid variable in Ansible

  • A variable name can only include letters, numbers, and underscores.
  • Variable names can begin with an underscore.

Examples:

Project_name , _project_name , project_name_2 , _project_name_

First of all check your system information using below command:

ansible all -m setup 

Now if you want to check any specific information then you can use filter like below command:

ansible all -m setup -a 'filter=ansible_hostname'

Define variables in Ansible playbook

The simplest way to define a variable is using vars keyword in the playbook

Simple example of variables in playbook:

create a playbook using below command:

sudo nano sample.yml
---
-
  hosts: devopshint
  vars:
    title: Welcome to DevOpsHint!

  tasks:
  - name: Ansible Playbook variable definition example
    debug:
      msg: "{{ title }}"

Run the playbook using below command:

ansible-playbook sample.yml

Output:

PLAY [devopshint] ***********************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************************
ok: [3.110.134.50]

TASK [Ansible Playbook variable definition example] *************************************************************************************
ok: [3.110.134.50] => {
    "msg": "Welcome to DevOpsHint!"
}

PLAY RECAP ******************************************************************************************************************************
3.110.134.50               : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Create new file for variable in Ansible

You can define variables in one or more files 

Here we will take an example of installing an Apache web server on our client node.

Below is our variable file present in the same folder where our playbook is.

vars.yml
package_name: apache2
Install-apache.yml
 
---
- name: Setting up Apache webserver
  hosts: devopshint
  become: true
    
  tasks:
  - include_vars: vars.yml 
  - name: Install apache2
    apt: name={{ package_name }} update_cache=yes state=latest

Conclusion:

In this article we have covered Ansible Variables with Examples, How to declare a valid variable in Ansible, Define variables in Ansible playbook,Create new file for variable in Ansible.

Related Articles:

Reference:

Ansible Variables Official page

Shweta Mamidwar

I am Shweta Mamidwar working as a Intern in Product Company. Likes to share knowledge.

2 thoughts on “Ansible Variables with Examples”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap