In this section, we will explore the concepts of variables and facts in Ansible. Variables allow you to store values that can be reused throughout your playbooks, making them more dynamic and flexible. Facts, on the other hand, are pieces of information about your managed nodes that Ansible gathers automatically.

Key Concepts

Variables

  • Definition: Variables are used to store values that can be referenced in your playbooks.
  • Types: Variables can be defined in multiple places, such as inventory files, playbooks, roles, and more.
  • Scope: Variables have different scopes, such as global, play, and host.

Facts

  • Definition: Facts are system properties collected by Ansible from managed nodes.
  • Gathering Facts: By default, Ansible gathers facts at the beginning of each play.
  • Usage: Facts can be used to make decisions in your playbooks, such as conditionals and loops.

Defining Variables

Variables can be defined in several ways in Ansible:

In Playbooks

---
- name: Example Playbook
  hosts: all
  vars:
    my_variable: "Hello, World!"
  tasks:
    - name: Print the variable
      debug:
        msg: "{{ my_variable }}"

In Inventory Files

[webservers]
web1 ansible_host=192.168.1.1 my_variable="Hello from web1"
web2 ansible_host=192.168.1.2 my_variable="Hello from web2"

In Group Vars and Host Vars

  • Group Vars: Define variables for a group of hosts.
  • Host Vars: Define variables for individual hosts.

Directory structure:

inventory/
  group_vars/
    all.yml
  host_vars/
    web1.yml
    web2.yml

Example group_vars/all.yml:

---
my_variable: "Hello from all hosts"

Example host_vars/web1.yml:

---
my_variable: "Hello from web1"

Using Variables

Variables can be referenced using the Jinja2 templating syntax {{ variable_name }}.

Example:

---
- name: Example Playbook
  hosts: all
  vars:
    greeting: "Hello"
    target: "World"
  tasks:
    - name: Print the greeting
      debug:
        msg: "{{ greeting }}, {{ target }}!"

Gathering and Using Facts

Gathering Facts

By default, Ansible gathers facts at the beginning of each play. You can control this behavior using the gather_facts directive.

Example:

---
- name: Example Playbook
  hosts: all
  gather_facts: yes
  tasks:
    - name: Print the OS type
      debug:
        msg: "The OS type is {{ ansible_os_family }}"

Commonly Used Facts

  • ansible_os_family: The OS family (e.g., RedHat, Debian).
  • ansible_distribution: The distribution name (e.g., Ubuntu, CentOS).
  • ansible_hostname: The hostname of the managed node.
  • ansible_ip_addresses: List of IP addresses of the managed node.

Practical Example

Let's create a playbook that uses both variables and facts.

Playbook: variables_and_facts.yml

---
- name: Variables and Facts Example
  hosts: all
  gather_facts: yes
  vars:
    greeting: "Hello"
  tasks:
    - name: Print a custom greeting
      debug:
        msg: "{{ greeting }}, {{ ansible_hostname }}!"
    - name: Print the OS type
      debug:
        msg: "The OS type is {{ ansible_os_family }}"

Running the Playbook

ansible-playbook -i inventory/hosts variables_and_facts.yml

Exercises

Exercise 1: Define and Use Variables

  1. Create a playbook that defines a variable message with the value "Welcome to Ansible".
  2. Print the value of the message variable using the debug module.

Solution

---
- name: Exercise 1: Define and Use Variables
  hosts: all
  vars:
    message: "Welcome to Ansible"
  tasks:
    - name: Print the message
      debug:
        msg: "{{ message }}"

Exercise 2: Use Facts in a Playbook

  1. Create a playbook that gathers facts and prints the hostname and OS type of each managed node.

Solution

---
- name: Exercise 2: Use Facts in a Playbook
  hosts: all
  gather_facts: yes
  tasks:
    - name: Print the hostname
      debug:
        msg: "The hostname is {{ ansible_hostname }}"
    - name: Print the OS type
      debug:
        msg: "The OS type is {{ ansible_os_family }}"

Common Mistakes and Tips

  • Undefined Variables: Ensure that all variables used in your playbooks are defined. Use the default filter to provide default values.
  • Fact Gathering: If you disable fact gathering (gather_facts: no), ensure you manually gather the required facts using the setup module.

Conclusion

In this section, we covered the basics of variables and facts in Ansible. We learned how to define and use variables, gather and use facts, and created practical examples to reinforce these concepts. Understanding variables and facts is crucial for writing dynamic and flexible playbooks. In the next section, we will dive into conditionals and loops to further enhance our playbooks.

© Copyright 2024. All rights reserved