Ansible Variables

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.

After you create variables, either by defining them in a file, passing them at the command line, or registering the return value or values of a task as a new variable, you can use those variables in module arguments, in conditional “when” statements, in templates, and in loops

  • It is used to store values that varies with different hosts.
  • We can mention variables within the playbook or we can create a separate variable file.

Let’s take below example where we have declared variable “nameserver” and used that variable in “lineinfile” module :

 

 

Below is an another example of using variables where we are adding ports in firewall.

Leave a Reply

Your email address will not be published. Required fields are marked *