Ansible - Day 3 (Ansible Inventory & Variables)

Ansible - Day 3 (Ansible Inventory & Variables)

Today we are on Day 3 of Ansible learning and we'll explore Ansible inventory and variables, including creating an inventory file, using variables in playbooks, and understanding dynamic inventories.

Ansible Inventory

  1. What is Ansible Inventory?

    Ansible inventory is like a playbook for your servers. It's a list of all the machines (hosts) you want Ansible to manage. This inventory file can be in various formats, such as TXT, INI or YAML, and it tells Ansible where to connect and what to do on each host.

  2. Creating an Inventory File

    Creating an inventory file is the first step to using Ansible. In a simple text file, you list the target servers or nodes along with some basic information like their IP addresses or hostnames.

     [web_servers]
     server1 ansible_host=192.168.1.101
     server2 ansible_host=192.168.1.102
     [db]
     dbserver1 ansible_host=192.168.1.20
     dbserver2 ansible_host=192.168.1.21
    
  3. Grouping Servers

    You can organize servers into groups, making it easier to manage and execute tasks on specific sets of servers. For example, you could group web servers as shown in the example above.

Using Variables in Playbooks

Variables are essential for making your Ansible playbooks more flexible and reusable. You can define variables at different levels: globally, in inventory files, or directly in playbooks.

  1. Global Variables: These are defined in Ansible's configuration files, making them available to all playbooks and roles.

    Inventory Variables: You can assign variables to specific hosts or groups in your inventory file. For example:

     [web]
     webserver1 ansible_host=192.168.1.10 my_var=42
     webserver2 ansible_host=192.168.1.11 another_var="Hello, Ansible!"
    

    Playbook Variables: You can also define variables directly within your playbooks, like this:

     ---
     - name: Using Variables in Playbooks
       hosts: web
       vars:
         app_port: 8080
       tasks:
         - name: Ensure the web app is running
           command: start_my_app.sh --port={{ app_port }}
    

    In this example, we've defined the app_port variable in the playbook and used it in a task.

Dynamic Inventories

  1. What Are Dynamic Inventories?

    Dynamic inventories are a more advanced way of managing your infrastructure with Ansible. Unlike static inventories, dynamic inventories are generated on the fly, often by querying your cloud provider or other data sources.

  2. Benefits of Dynamic Inventories

    Dynamic inventories are particularly useful in cloud environments where servers are created and destroyed dynamically. They ensure your Ansible inventory is always up to date without manual updates.

  3. Using Dynamic Inventories

    Ansible supports various plugins for dynamic inventories, including AWS, Azure, and more. You'll typically configure these plugins to connect to your cloud provider's API and retrieve the current list of servers.

    For instance, you can create a dynamic inventory script that fetches hosts from your cloud provider's API.

    Here's a simple example of using a dynamic inventory script:

     ansible-inventory --list
    

    This command will generate a JSON output containing your inventory information.

Well done, You have successfully completed Day 3 of the Ansible series and learned how to create inventory files and variables. These empower you to manage your infrastructure efficiently, make your playbooks more adaptable, and handle dynamic environments seamlessly.