Ansible is simply an open-source IT engine that is used for automating remote system management tasks like provisioning tasks, service orchestration, and application deployment, among many other IT tasks.
Ansible has no agents for there is no need to install any software on the target nodes. It has a file called inventory from where it gets the information about the nodes it has to manage.
Ansible uses playbooks to describe automation jobs and playbooks use YAML, a very simple language that is simple for humans to understand, read, and write. YAML is a human-readable data serialization language that is frequently used for configuration files. But it could be used in many applications where data is being stored. Because YAML is in human readable form, even the IT infrastructure support staff can read the playbook and troubleshoot it as necessary.
What
will We Cover?
In this tutorial, we will explore variables used in Ansible and also review some use cases of these variables in Ansible playbooks.
Pre-requisites
To perform the various examples in this tutorial, the following requirements should be fulfilled:
1. An Ansible controller node (Ubuntu 20.04 in our case) and one or two target nodes.
2. Basic understanding of Ansible and writing a playbook.
If you are just starting to learn Ansible, we would recommend you to start learning Vagrant first and then set up a basic local testing environment using Vagrant. This lab should contain a controller node and two target nodes.
Ansible’s Operation
Ansible operates by connecting to target nodes and sending them little programs known as “Ansible modules”. After that, Ansible runs these modules (through SSH) and then deletes them. There is no need for servers, daemons, or databases, and your library of modules may be stored on any computer.
The managing node is the controlling node that oversees how the playbook is carried out in its entirety. It is the node that you are operating the setup from. A list of hosts where Ansible modules need to be run is provided in the inventory file. The management node connects via SSH, runs the tiny modules on the host system, and installs the product or software.
The beauty of Ansible is that it successfully removes modules after they have been installed. It connects to the host system, runs the instructions, and, if the installation is successful, deletes the code that was copied on the host machine and then performed.
Ansible’s Variables
Variables used in Ansible and variables in any other programming language are fairly similar. A variable can arrive from a playbook file, can be passed to CLI, or we can use the ‘register’ keyword to store the return value/values from a task. You can utilize a variable anywhere in the module arguments, in p, with the ‘when’ statements and of course inside loops by giving it a value and using them. Conditions can be placed around the value of the variables and the playbook can utilize them accordingly.
There are some rules that make a variable name to be a valid one, such as it must begin with a letter and consist of letters, numbers, and underscores only.
Variables in a Playbook
We can define variables in a number of ways for use in Ansible tasks:
1. From command line: Using the ‘–extra-vars’ option when executing the ansible-playbook command:
$ ansible-playbook sample.yml --extra-vars "my_vars=bar"
2. Similarly, a file (JSON/YAML) containing list of variables can be passed as:
$ ansible-playbook sample.yml --extra-vars "@my_vars_file.json"
The ‘my_vars_file.json’ file is in the same location as the playbook.
3. Variables can be listed inside the main playbook itself by introducing a ‘vars’ section:
--- - hosts: web vars: my_var2: my_val2 tasks: # Display "Variable ‘my_var2’ is set to ‘my_val2’". - debug: msg="Variable ‘my_var2’ is set to {{ my_var2 }}"
4. Similar to the above, we can use the ‘vars_file’ section to specify a file containing variables:
--- - hosts: web vars_files: - my_vars.yml tasks: # Display "Variable ‘my_var1’ is set to ‘my_val1’". - debug: msg="Variable ‘my_var1’ is set to {{ my_var1 }}"
Variables in an Inventory Files
In this method, variables are either specified in-line with a host or at the group level. However, Ansible does not recommend using inventory files for storing variables. Refer to the example below:
#Host-specific variables (inline definition). [London] web1.example.com proxy_state=present web2.example.com proxy_state=absent # Variables defined for the entire group. [London:vars] cdn_host=london.times.example.com compute_val=96
Registered Variables
Registered variables can be used to store the value returned from a task/command at runtime and later be used in other tasks. Example:
--- - hosts: all gather_facts: no become: false tasks: - name: Check the user name ansible.builtin.shell: /usr/bin/whoami register: login - name: Display the user name using the output from previous task debug: msg="Logged in as user {{ login.stdout }}"
Scope of variable is decided by the location where a variable is set.
They are defined in three scopes:
1. Global: These variables are set either from the command line or using the Ansible configuration file.
2. Play: This variable is defined at the play level.
3. Host: These variables are defined on the host groups and/or individual hosts by the inventory, fact gathering, or task
Conclusion
In this tutorial we have learned about various types of variables used in Ansible. In fact, they are very useful for increasing the flexibility and comprehensibility of a code when used in an appropriate manner.