↓ Skip to main content
Linux

Ansible: Variables

·2 mins
Table of Contents

Ansible Variables are used to store values that can be reused throughout an Ansible playbook. They can be set in a number of ways, including being passed as command-line options, defined in an inventory file, or defined in a playbook itself. Variables can be used to store values such as file paths, user-defined parameters, and results of tasks.

Ansible System Variables, on the other hand, are predefined variables provided by Ansible. These variables contain information about the host, such as the host name, operating system, IP address, etc. These variables are automatically available in playbooks and can be accessed using the “hostvars” dictionary. For example, the hostname of a target host can be accessed using the variable “ansible_hostname”.

System Variables can be useful in writing more dynamic playbooks that adapt to the target host’s characteristics.

Example
#

---
- name: Example Playbook with Variables and System Variables
  hosts: all
  gather_facts: true
  tasks:
    - name: Print IP and FQDN
      debug:
        msg: "The IP address is {{ ansible_default_ipv4.address }} and the FQDN is {{ ansible_fqdn }}"
    - name: Print a custom variable
      debug:
        msg: "The custom message is: {{ custom_message }}"
  vars:
    custom_message: "Hello from the playbook!"

System Variables
#

Ansible system variables are special variables that are used to represent information about the system, such as the hostname, architecture, IP address, and others. These variables are set by the Ansible control node and are available to all playbooks, tasks, and roles. Some of the common system variables in Ansible include:

  • ansible_hostname: hostname of the system
  • ansible_os_family: family of the operating system (e.g. RedHat, Debian, etc.)
  • ansible_distribution: distribution of the operating system (e.g. CentOS, Ubuntu, etc.)
  • ansible_distribution_version: version of the operating system distribution
  • ansible_architecture: architecture of the system (e.g. x86_64, aarch64, etc.)
  • ansible_all_ipv4_addresses: list of all IPv4 addresses of the system
  • ansible_default_ipv4: default IPv4 address of the system

These variables can be used in tasks and playbooks to conditionally execute tasks based on system information, customize the behavior of tasks and playbooks, and more.

- name: Get IP and FQDN of Host
  hosts: all
  gather_facts: yes
  tasks:
    - name: Debug print host IP
      debug:
        msg: "Host IP: {{ ansible_default_ipv4.address }}"

    - name: Debug print host FQDN
      debug:
        msg: "Host FQDN: {{ ansible_fqdn }}"
Author
Samarthya
Engineer, learner, tinkerer. This is my trophy shelf of everyday work and leisure.

Related

Ansible: A thought

·3 mins
Ansible is an open-source software platform for automating and managing IT infrastructure. It provides a simple and easy-to-learn method for automating and managing various IT processes such as provisioning, configuration management, and application deployment. Ansible uses YAML (Yet Another Markup Language) and playbooks written in YAML, which are a series of instructions that specify what actions need to be performed.

Ansible: Building Inventory

·2 mins
The blog is adapted from the information available here. — https://docs.ansible.com/ansible/ An Ansible inventory is a file that defines the hosts and groups of hosts that Ansible will manage. Inventory files are used to define the nodes (hosts) that will be managed by Ansible. These files can be in different formats, including INI and YAML.

Ansible: SSH Access

·1 min
To set up SSH access for inventory hosts in Ansible, follow these steps: Generate an SSH key pair ssh-keygen -t rsa Copy the public key to the remote host ssh-copy-id user@hostname Test the connection to the remote host: ssh user@hostname Modify the ansible.cfg file to set the remote user, if necessary. Add the remote host to the Ansible inventory file. Run the Ansible playbook to execute the desired tasks on the remote host. Note: If you need to access multiple remote hosts, repeat steps 2-5 for each host.