Skip to content
Ansible

icon picker
Using variables in playbook

Variables can be defined in a playbook and can be referenced using debug module
- name: This is an example use case of variable
hosts: localhost

vars:
test: hello, this is dummy variable
tasks:
- name: Reading ansible playbook variable
debug:
var: test

Ways of declaring variables in playbook
- name: This is an example use case of variable
hosts: localhost

vars:
test: hello, this is dummy variable
packages: ['wget', 'curl', 'unzip', 'vim']
cities:
- New York
- London
- Tokyo
- Dubai
web_server: {'Linux': 'httpd', 'Unix': 'apache2'}
tasks:
- name: Reading ansible playbook variable
debug:
var: test
- name: Reading ansible playbook variable
debug:
var: packages
- name: Reading ansible playbook variable
debug:
var: cities


Getting values from users for variables and arithmetic operations
- name: This is to Display Arithmetic Opeations on Varaibles
hosts: localhost
gather_facts: false

vars:
a : 10
b : "{{a*10}}"

vars_prompt:
- name : x
prompt: Please enter Value of x
private: no

- name : y
prompt: Please eneter value of y
private: no

tasks:
- name: Operations on variables
debug:
msg:
- "value of a is : {{a}}"
- "value of b is : {{b}}"
- "Addition of User Defined Values x, y is : {{x+y}}"
- "Addition of User Defined Values x, y is : {{x|int + y|int}}"
- "Multiple of User Defined Values x, y is : {{x|int * y|int}}"
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.