Variables in Ansible are nothing but similar to the variables in a programming language. Ansible makes use of variables to better customize the execution of tasks and playbooks. Using variables it’s possible to use the same playbook with different targets and environments.
You can assign a value to the variable and use it anywhere in the playbook. You can define these variables in your playbooks, in the inventory file, or at the command line. You can also create them during runtime by assigning a return value to the new variable. Once created, you can use them in conditional statements, in loops, or as arguments.
Define Variable:
A variable name can only include letters, numbers, and underscores and it cannot begin with a number. You can define a variable using standard YAML syntax. For example:
- hosts : web
vars:
servicename: apache2
Reference Variable:
{{ servicename }}
In Ansible, a variable is referenced using Jinja2 syntax. Jinja2 variables use double curly braces. Like, the expression servicename is referenced as
- name: using variable in playbook
hosts: webserver1
vars:
servicename: apache2
tasks:
- name: 'creating file using variable'
service: name={{ servicename }} state=started
===================
Condition:
In an ansible playbook, we sometimes need to execute different tasks depending on the value of a variable or the value of the return variable of a previous task. You may want to perform a certain set of tasks if the value of the variable is less than a value and a different set of tasks if the value of the variable is greater. You can achieve this with Ansible conditions.
- name: Install Apache on CentOS Server yum: name=httpd state=present become: yes when: ansible_os_family == "RedHat"
-
name: this is our first play.
hosts: webserver1
vars:
age: 19
tasks:
-
name: 'creating file using variable'
command: touch /tmp/18.txt
when: age == 18
-
name: 'creating file using variable'
command: touch /tmp/grater_then_18.txt
when: age > 18
Loops:
While configuring servers and performing various tasks, you may sometimes need to repeat the execution of the same task using different values. For this purpose we have loops. If when the statement is combined with a loop, Ansible processes the condition separately for each item. Suppose you want to create 50 users at a time. One will have to define a task fifty times for each user. It would be a very inefficient approac
[ansadmin@ip-172-31-45-242 ~]$ cat loop.yml
-
name: this is our first play.
hosts: localhost
become: true
tasks:
-
name: 'installing'
yum: name="{{ item }}" state=present
with_items:
- vsftpd
- tree
In any scripting language, we organize our code using classes, methods, variables, etc. and we reference them as required. In the same way, we can modularize the Ansible playbooks to use them efficiently and in an organized manner. We can store the repeated tasks or variables in some other file and reference them in our main playbook.
-
name: this is our first play.
hosts: webserver1
vars:
var1: var1
var2: var2
var3: var3
var4: var4
var5: var5
var6: var6
var7: var7
var8: var8
var9: var9
var10: var10
tasks:
-
name: 'task 1'
command: touch /tmp/large/{{ var1 }}.txt
-
name: 'task 2'
command: touch /tmp/large/{{ var2 }}.txt
-
name: 'task 3'
command: touch /tmp/large/{{ var3 }}.txt
-
name: 'task 4'
command: touch /tmp/large/{{ var4 }}.txt
-
name: 'task 5'
command: touch /tmp/large/{{ var5 }}.txt
-
name: 'task 6'
command: touch /tmp/large/{{ var6 }}.txt
-
name: 'task 7'
command: touch /tmp/large/{{ var7 }}.txt
-
name: 'task 8'
command: touch /tmp/large/{{ var8 }}.txt
-
name: 'task 9'
command: touch /tmp/large/{{ var9 }}.txt
-
name: 'task 10'
command: touch /tmp/large/{{ var10 }}.txt
In this playbook, there are nearly 10 variables and 10 tasks. This makes our playbook inefficient and unnecessarily huge.
We can store these vars in a separate file variable.yml as follows:
var1: var1
var2: var2
var3: var3
var4: var4
var5: var5
var6: var6
var7: var7
var8: var8
var9: var9
var10: var10
And, in the same way we can store the tasks also in different file tasks.yml:
-
name: 'task 1'
command: touch /tmp/task/{{ var1 }}.txt
-
name: 'task 2'
command: touch /tmp/task/{{ var2 }}.txt
-
name: 'task 3'
command: touch /tmp/task/{{ var3 }}.txt
-
name: 'task 4'
command: touch /tmp/task/{{ var4 }}.txt
-
name: 'task 5'
command: touch /tmp/task/{{ var5 }}.txt
-
name: 'task 6'
command: touch /tmp/task/{{ var6 }}.txt
-
name: 'task 7'
command: touch /tmp/task/{{ var7 }}.txt
-
name: 'task 8'
command: touch /tmp/task/{{ var8 }}.txt
-
name: 'task 9'
command: touch /tmp/task/{{ var9 }}.txt
-
name: 'task 10'
command: touch /tmp/task/{{ var10 }}.txt
No comments:
Post a Comment