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.
Table of Contents
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
- Playbook with vars
- Create new file for variable
- 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:
- How to Install Ansible on Ubuntu 18.04/16.04 LTS
- How to Install Ansible on Ubuntu 20.04 LTS
- How to Install Ansible on CentOS 8
- Ansible ad hoc commands with Examples
Reference:
Hi Shweta do you have time to talk about classes, please ping me at +17039448455. Thank you.
Hello Jay,
on which tool you are looking for classes.