- Inventories: You can define variables in your inventory files, either globally for all hosts or specific to particular groups or hosts.
- Host or Group Variables: Create separate files for host or group variables inside the
host_vars
andgroup_vars
directories within your Ansible project. - Playbook-level Variables: Declare variables at the top level of your playbook using the
vars
keyword.
Ansible Variable in Playbook with following ways:
- Use variables in playbook directly
- Use variables using external var files in playbook
- Use variables using include_vars into task in playbook
- Use variables using User Prompt in playbook
- Use variables using outout of task using register
List of Way to declare variables in Playbook
- Playbook-level Variables: Declare variables at the top level of your playbook using the vars keyword.
- Task-level Variables: You can also define variables within specific tasks.
- Register Variables: You can capture the output of a task into a variable using the register keyword.
- Environment Variables: You can use environment variables by accessing them using Ansible’s ansible_env dictionary.
- Using Facts: Ansible gathers facts about managed hosts, and you can use these facts as variables in your playbooks.
- Prompting for User Input: You can use the vars_prompt keyword to prompt the user for input during playbook execution.
Declare variables at the top level of your playbook using the vars
keyword.
- name: Example playbook
hosts: all
vars:
variable_name: value
tasks:
# Your tasks here
Task-level Variables:
- name: Example playbook
hosts: all
tasks:
- name: Task with variables
debug:
msg: "My variable: {{ variable_name }}"
vars:
variable_name: value
Register Variables:
- name: Example playbook
hosts: all
tasks:
- name: Task with register
shell: echo "Hello, world!"
register: output
- name: Display registered variable
debug:
var: output.stdout
Environment Variables:
- name: Example playbook
hosts: all
tasks:
- name: Display environment variable
debug:
var: ansible_env.VARIABLE_NAME
Using Facts:
- name: Example playbook
hosts: all
tasks:
- name: Display fact
debug:
var: ansible_facts['ansible_os_family']
Prompting for User Input:
- name: Example playbook
hosts: all
vars_prompt:
- name: variable_name
prompt: "Enter a value for the variable:"
tasks:
- name: Display input variable
debug:
var: variable_name
Ansible Playbook with Variable for Centos/RHEL
---
- name: Update web servers
hosts: web
vars:
myname: "Rajeshkumar"
age: "18"
packagename: "httpd"
servicename: "httpd"
vars_files:
- "vars.yaml"
vars_prompt:
- name: "version"
prompt: "Which version Do you want to install?"
private: no
tasks:
- name: Install Apache in centos7
ansible.builtin.yum:
name: "{{ packagename }}"
state: latest
- name: Copy index.html
ansible.builtin.copy:
src: index.html
dest: /var/www/html/index.html
- name: Starting a Apache Server
ansible.builtin.service:
name: "{{ servicename }}"
state: started
- name: Print return information from the previous task
ansible.builtin.debug:
var: myname
- name: include default step variables
include_vars: tasks_var.yaml
- name: Print return information from the previous task
ansible.builtin.debug:
msg: "My Name is {{ myname }} and My age is {{ age }}"
- name: Print return information from the previous task
ansible.builtin.debug:
var: version
- name: Ansible register variable basic example
shell: "find *.txt"
args:
chdir: "/home/azureuser/rajesh"
register: find_output
- debug:
var: find_output
- debug:
var: find_output.stdout_lines
- debug:
var: find_output.stdout_lines[0]
Ansible Playbook with Variable for Ubuntu
---
- name: Update web servers
hosts: web
vars:
myname: "Rajeshkumar"
age: "18"
packagename: "apache2"
servicename: "apache2"
vars_files:
- "vars.yaml"
vars_prompt:
- name: "version"
prompt: "Which version Do you want to install?"
private: no
tasks:
- name: Install Apache in ubuntu
ansible.builtin.apt:
name: "{{ packagename }}"
state: latest
- name: Copy index.html
ansible.builtin.copy:
src: index.html
dest: /var/www/html/index.html
- name: Starting a Apache Server
ansible.builtin.service:
name: "{{ servicename }}"
state: started
- name: Print return information from the previous task
ansible.builtin.debug:
var: myname
- name: include default step variables
include_vars: tasks_var.yaml
- name: Print return information from the previous task
ansible.builtin.debug:
msg: "My Name is {{ myname }} and My age is {{ age }}"
- name: Print return information from the previous task
ansible.builtin.debug:
var: version
- name: Ansible register variable basic example
shell: "find *.txt"
args:
chdir: "/root/ansible"
register: find_output
- debug:
var: find_output
- debug:
var: find_output.stdout_lines
- debug:
var: find_output.stdout_lines[0]
Ansible Varialble Precedence
Beginner to Advanced Complete Ansible Tutorial in 15 hours 6-2021 Part – 1
Beginner to Advanced Complete Ansible Tutorial in 15 hours 6-2021 Part – 2
Beginner to Advanced Complete Ansible Tutorial in 15 hours 6-2021 Part – 3
Beginner to Advanced Complete Ansible Tutorial in 15 hours 6-2021 Part – 4
Beginner to Advanced Complete Ansible Tutorial in 15 hours 6-2021 Part – 5
Beginner to Advanced Complete Ansible Tutorial in 15 hours 6-2021 Part – 6
Beginner to Advanced Complete Ansible Tutorial in 15 hours 6-2021 Part – 7
Beginner to Advanced Complete Ansible Tutorial in 15 hours 6-2021 Part – 8
Beginner to Advanced Complete Ansible Tutorial in 15 hours 6-2021 Part – 9
Beginner to Advanced Complete Ansible Tutorial in 15 hours 6-2021 Part – 10
Beginner to Advanced Complete Ansible Tutorial in 15 hours 6-2021 Part – 11
Beginner to Advanced Complete Ansible Tutorial in 15 hours 6-2021 Part – 12
Beginner to Advanced Complete Ansible Tutorial in 15 hours 6-2021 Part – 13
Beginner to Advanced Complete Ansible Tutorial in 15 hours 6-2021 Part – 14
Beginner to Advanced Complete Ansible Tutorial in 15 hours 6-2021 Part – 15
Latest posts by Rajesh Kumar (see all)
- Gradle versions, their supported Java versions, and unsupported Java versions - December 23, 2024
- An Introduction of GitLab Duo - December 22, 2024
- Best Hospitals for affordable surgery for medical tourism - December 20, 2024