Ansible – Inventory variables in Ansible (Part-11)

Posted by

What are Inventory Variables in Ansible?

Inventory variables in Ansible are key-value pairs that provide additional context and configuration for hosts and groups defined in your inventory. These variables can be used to customize the behavior of your playbooks for specific hosts or groups, making your automation scripts more flexible and powerful.

Types of Inventory Variables

  1. Host Variables: Variables specific to an individual host.
  2. Group Variables: Variables specific to a group of hosts.
  3. All Variables: Variables that apply to all hosts in the inventory.

Structure of Inventory Variables

Inventory variables can be organized in different ways, such as:

  • Defined directly within the inventory file (INI or YAML format).
  • Stored in separate files under group_vars and host_vars directories.

Example Directory Structure

inventory/
├── inventory.ini
├── group_vars/
│   └── web.yml
└── host_vars/
    └── 172.22.240.232.yml

Defining Inventory Variables

1. Inventory File with Variables (INI Format)

You can define variables directly within your inventory file.

Example: inventory.ini

[web]
172.22.240.232 ansible_user=ubuntu ansible_port=22

[db]
172.22.240.233 ansible_user=root ansible_port=2222

2. Inventory File with Group and Host Variables (YAML Format)

You can define variables in a YAML-formatted inventory file.

Example: inventory.yml

all:
  children:
    web:
      hosts:
        172.22.240.232:
          ansible_user: ubuntu
          ansible_port: 22
    db:
      hosts:
        172.22.240.233:
          ansible_user: root
          ansible_port: 2222

Using group_vars and host_vars

For better organization, especially with larger inventories, you can use group_vars and host_vars directories to store variables.

3. Group Variables (group_vars)

Group variables apply to all hosts within a group. These variables are stored in files named after the group under the group_vars directory.

Example: group_vars/web.yml

---
nginx_port: 80
server_name: example.com

4. Host Variables (host_vars)

Host variables apply to individual hosts. These variables are stored in files named after the host under the host_vars directory.

Example: host_vars/172.22.240.232.yml

---
host_specific_var: some_value

Combining All Methods

You can combine variables defined directly in the inventory file with those in group_vars and host_vars directories. Ansible merges these variables at runtime.

Example Playbook Using Inventory Variables

Directory Structure:

inventory/
├── inventory.ini
├── group_vars/
│   └── web.yml
└── host_vars/
    └── 172.22.240.232.yml
playbook.yml
templates/
└── nginx.conf.j2

Inventory File: inventory.ini

[web]
172.22.240.232

[db]
172.22.240.233

Group Variables File: group_vars/web.yml

---
nginx_port: 80
server_name: example.com

Host Variables File: host_vars/172.22.240.232.yml

---
host_specific_var: some_value

Playbook: playbook.yml

---
- name: Configure web server
  hosts: web
  become: yes
  tasks:
    - name: Install Nginx
      ansible.builtin.apt:
        name: nginx
        state: present

    - name: Configure Nginx
      ansible.builtin.template:
        src: templates/nginx.conf.j2
        dest: /etc/nginx/sites-available/default
      notify:
        - Restart Nginx

    - name: Ensure Nginx is running
      ansible.builtin.service:
        name: nginx
        state: started

  handlers:
    - name: Restart Nginx
      ansible.builtin.service:
        name: nginx
        state: restarted

Template File: templates/nginx.conf.j2

server {
    listen {{ nginx_port }};
    server_name {{ server_name }};

    location / {
        proxy_pass http://localhost;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

Running the Playbook

To run the playbook using the defined inventory and variables:

ansible-playbook -i inventory/inventory.ini playbook.yml

Summary

  • Host Variables: Specific to individual hosts, defined in the inventory file or under host_vars.
  • Group Variables: Specific to groups of hosts, defined in the inventory file or under group_vars.
  • All Variables: Apply to all hosts if defined at the top level in the inventory file.
  • Organizing Variables: Using group_vars and host_vars directories helps organize variables, making it easier to manage large inventories.

Live Practical

Example for Inventory file under host

create task in web.yaml

added variable portion

– name: Task with variables
debug:
msg: “my variable: {{ myname }}”

web.yaml

---
- name: Update web servers
  hosts: web
  tasks:
    - name: Install Apache httpd (state=present is optional)
      ansible.builtin.apt:
        name: apache2
        state: present

    - name: Copy file with owner and permissions
      ansible.builtin.copy:
        src: /home/jami/index.html
        dest: /var/www/html/index.html

    - name: Start service httpd, if not started
      ansible.builtin.service:
        name: apache2
        state: started
    - name: Task with variables
      debug:
        msg: "my variable: {{ myname }}"

Now go to inventory

add variable beside host myname =Jami in host

[web]
51.8.106.65 myname =Jami in host1
51.8.106.109 myname =Jami in host2

Run ansible

ansible-playbook -i inventory web.yaml -u jami -k -b

Example for Inventory file under group

Add [web:vars] in inventory file and after that add the variable

[web:vars]
myname=Jamiingroup

inventory file

[web]
51.8.106.65
51.8.106.109

[web:vars]
myname=Jamiingroup

Run ansible command

ansible-playbook -i inventory web.yaml -u jami -k -b

Example :Using creating directory group_vars and host_vars

mkdir host_vars

mkdir group_vars

vi group_vars/web

myname: jamiingrupvar

run ansible command

now using mkdir group_vars

vi host_vars/51.8.106.109 (it should always match with host)

myname: jamiinhostfile
~

guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x