010 Variables

Tech Tutorial: Understand Core Components of Ansible - Variables #

Introduction #

In the realm of automation, Ansible stands out for its simplicity and versatility. This tutorial focuses on a core component crucial for Ansible’s dynamic and flexible automation capabilities: Variables. Variables in Ansible are used to manage differences between systems, environments, and users in a manageable and scalable way. For those preparing for the Red Hat Certified Engineer (RHCE) exam, understanding how to effectively use variables is essential.

Step-by-Step Guide #

1. Understanding Variables in Ansible #

Variables in Ansible are akin to variables in any programming language. They store information that can be used in your playbooks. Ansible provides several types of variables:

  • Inventory Variables: Defined in the inventory file or dynamically generated.
  • Playbook Variables: Defined within the playbook itself.
  • Role Variables: Defined within roles.
  • Facts: Gathered by Ansible from the managed nodes.
  • Magic Variables: Special variables, for example, hostvars, group_vars, inventory_hostname.

2. Defining Variables in an Inventory File #

Variables can be set directly in your inventory file, which can be organized in multiple ways. Here’s an example using an INI-like format:

[webserver]
web1.example.com ansible_user=rhel_user ansible_ssh_pass=yourPassword

[webserver:vars]
http_port=80
max_clients=200

In this example, ansible_user and ansible_ssh_pass are variables assigned to web1.example.com, whereas http_port and max_clients are assigned to all hosts within the webserver group.

3. Using Variables in Playbooks #

Variables can be defined in playbooks and used to parameterize tasks. Below is an example of a playbook that uses variables:

---
- name: Configure Web Server
  hosts: webserver
  vars:
    http_port: 80
    max_clients: 200

  tasks:
  - name: Install httpd
    yum:
      name: httpd
      state: present

  - name: Configure httpd
    template:
      src: templates/httpd.conf.j2
      dest: /etc/httpd/conf/httpd.conf
    notify:
    - restart httpd

  handlers:
  - name: restart httpd
    service:
      name: httpd
      state: restarted

In this playbook, http_port and max_clients are playbook variables used in configuring the HTTP server.

4. Role Variables #

Roles in Ansible allow you to load automatically certain vars_files, tasks, and handlers based on a known file structure. Here’s how you can define variables in roles:

Create a directory structure like this:

roles/
   webserver/
      defaults/
         main.yml
      vars/
         main.yml
      tasks/
         main.yml
      handlers/
         main.yml
  • defaults/main.yml - defaults for the role, which can be overridden easily.
  • vars/main.yml - other variables for the role, which have a higher priority than defaults.

Example of roles/webserver/defaults/main.yml:

---
http_port: 80
max_clients: 200

5. Priority of Variables #

The order of precedence from least to greatest (the last listed variables override all others) is as follows:

  1. Variables defined in defaults/main.yml in a role
  2. Variables defined in the inventory file
  3. Variables defined in the playbook under the vars section
  4. Variables passed at the command line

Detailed Code Examples #

Let’s dive deeper into using variables dynamically:

Using Facts #

Facts are system properties that Ansible collects from the systems it manages. You can use them like any other variable. Here’s an example of using facts:

---
- name: Print system facts
  hosts: all
  tasks:
    - name: Print IP address
      debug:
        msg: "The IP address is {{ ansible_default_ipv4.address }}"

Using Magic Variables #

Magic variables provide information about the current execution context, such as hostvars, group_names, etc. Here is an example of using hostvars to access variables from another host:

---
- name: Access variable from another host
  hosts: webserver
  tasks:
    - name: Print database server IP
      debug:
        msg: "The database server IP is {{ hostvars['dbserver']['ansible_default_ipv4']['address'] }}"

Conclusion #

Understanding and effectively using variables is a cornerstone of mastering Ansible, especially for the RHCE exam. This tutorial covered the basics and some advanced concepts about variables in Ansible. Experiment with these examples and integrate them into larger playbooks to harness the full power of Ansible’s automation capabilities. Remember, mastering variables improves the flexibility and reusability of your Ansible code, making your automation tasks more efficient and robust.