Ansible Variable |
A Variable is a letters, underscores & numbers value that can be changed depending on conditions. it should always start with a letter.
In the ansible, variable define in vars: section with following syntax
vars:
- variable_name: variable_value
For example we define variable name “name” in ansible-playbook and its value “linuxtopic”. We use file module to create a file name according to variable.
To Create a ansible-playbook
cd /etc/ansiblevi variable.yml
---
- hosts: 127.0.0.1
gather_facts: false
vars:
- name: linuxtopic
tasks:
- name: variable
file:
state: touch
path: /tmp/{{ name }}.txt
How to use variables in ansible playbook |
To execute ansible playbook
ansible-playbook variable.yml
Verify
ll /tmp/linuxtopic.txt
Example : array or list variables
In this example we use list variable and create a 5 files using loop
file_names:
- linuxtopic1
- linuxtopic2
- linuxtopic3
- linuxtopic4
- linuxtopic5
OR
file_names: [ linuxtopic1, linuxtopic2, linuxtopic3, linuxtopic4, linuxtopic5 ]
---
- hosts: 127.0.0.1
gather_facts: false
vars:
file_names:
- linuxtopic1
- linuxtopic2
- linuxtopic3
- linuxtopic4
- linuxtopic5
tasks:
- name: Create Multiple File using Variable
file:
state: touch
path: /tmp/{{ item }}.txt
loop: "{{ file_names }}"
Ansible list variables |
We can also use this syntax
---
- hosts: 127.0.0.1
gather_facts: false
vars:
file_names: [ linuxtopic1, linuxtopic2, linuxtopic3, linuxtopic4, linuxtopic5 ]
tasks:
- name: Create Multiple File using Variable
file:
state: touch
path: /tmp/{{ item }}.txt
loop: "{{ file_names }}"
To Run Ansible Playbook
ansible-playbook variable.yml
Verify :
If we want to use single variable from variable list, following example will help you.
---
- hosts: 127.0.0.1
gather_facts: false
vars:
file_names:
- linuxtopic1
- linuxtopic2
- linuxtopic3
- linuxtopic4
- linuxtopic5
tasks:
- name: Create Multiple File using Variable
file:
state: touch
path: /tmp/{{ item }}.txt
loop:
- "{{ file_names[2] }}"
file_names have five variable and count start from 0 means linuxtopic1 value 0 linutopic2 value 1 … linuxtoipic5 value is 4.
We define 3rd variable of the list so we use following syntax in the loop:
loop:
- "{{ file_names[2] }}"
To run playbook :
ansible-playbook variable.yml
Variables Dictionary
In this example we declare 3 variable, 1th is “base_path” and set value /var/www/html/, this is our apache directory path where we create a directory using variable “ansible_hostname” , 2nd variable is index_file, we create a index.html and 3rd index_msg, we echo a message in index file.
Note: we enable gather_facts because we use “ansible_hostname” facts
---
- hosts: 127.0.0.1
# gather_facts: false
vars:
base_path: /var/www/html/
index_file: index.html
index_msg: 'This is a welcome page'
tasks:
- name: Create directory using Variable
file:
state: directory
path: "{{ base_path}}{{ ansible_hostname }}"
- name: Create Index file
file:
state: touch
path: "{{ base_path }}{{ ansible_hostname }}/{{ index_file }}"
- name: Echo message in index.html
shell: echo {{ index_msg }} >> "{{ base_path }}{{ ansible_hostname }}/{{ index_file }}"
Ansible Variables Dictionary |
To Run Ansible Playbook
ansible-playbook variable.yml
ll /var/www/html/ll /var/www/html/linuxservercat /var/www/html/linuxserver/index.html
Thanks
End of this ansible variables, we need your support so i request you to please like and share this post also comment if something missing by me or give me a suggestion to make more easy/useful.
www.linuxtopic.com