• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Urban Penguin

The Urban Penguin - Linux Training

  • Home
  • About
  • Live Online Courses
  • Shop
  • Programming
    • Master Editing Text Files Using VIM
    • Learn Shell Scripting with BASH
    • PERL Scripting in Linux
    • Ruby Scripting in Linux
    • Scripting with PowerShell
    • Learn C Programming using Linux and the Raspberry Pi
    • General Java Tutorials
    • Java 7 OCA Exam 1ZO-803
  • OS Tutorials
    • Red Hat and CentOS Training
      • Red Hat Enterprise Linux System Administration 1 – RH124
      • RHCSA – System Admin 2 – RH134
      • RHCE – EX294 – Automation With Ansible
    • Learning Ubuntu
    • LPI Training
      • LPI Linux Essentials
      • LPIC-1 Linux Administrator
      • LPIC-2 Certified Linux Engineer
      • LPIC-3 Senior Level Certification
        • LPIC-3 Exam 300 : Mixed Environments
        • LPIC-3 Exam 303 : Security
        • LPIC-3 Exam 304 : Virtualization and High Availability
    • Linux Technologies
      • Apache HTTPD Server
      • Learning PHP
      • Learning PUPPET
      • Learning SAMBA
      • Linux File-Systems
      • Monitoring with Nagios Core
      • MYSQL
      • openLDAP Directories on Linux
You are here: Home / Linux / RH294 / Using Ansible Variables in Inventory Files

Using Ansible Variables in Inventory Files

August 25, 2019 by The Urban Penguin

Ansible Variables can be used within Playbooks but also within the inventory. When we look at the Apache Web Server package in Ubuntu it is apache2, in Enterprise Linux variants, it is httpd. Adding  Ansible Variables to the inventory helps document the differences and makes the design of Playbooks, play and tasks more simple. Even though we do have to install packages with the yum module on RHEL and the apt module on Debian based systems such as Ubuntu, at least we can keep the package name consistent if we use variables.

INI Inventory with Variables

$ vim inventory
192.168.122.[4:6]
[rhel8]
192.168.122.[4:5]
[ubuntu1804]
192.168.122.6
[peterborough]
192.168.122.[4:6]
[development]
192.168.122.[4:6]
[uk:children]
peterborough
[rhel:children]
rhel8
[rhel:vars]
apache_package=http
[ubuntu:children]
ubuntu1804
[ubuntu:vars]
apache_package=apache2

We add INI blocks with the square bracket that define the group name that the Ansible Variables should be assigned to with the keyword *vars*. The lines following will then be the variables and values. Ansible Variable names must start with a letter, and they can only contain letters, numbers, and underscores. If we are not careful the variables definition blocks can become separated from the groups they support. Even though they will still work it is less easy to read and manage. YAML inventories make the task easier.

YAML Inventory with Variables

all:
children:
development:
hosts:
192.168.122.4: {}
192.168.122.5: {}
192.168.122.6: {}
rhel:
children:
rhel8:
hosts:
192.168.122.4: {}
192.168.122.5: {}
vars:
apache_package: httpd
ubuntu:
children:
ubuntu1804:
hosts:
192.168.122.6: {}
vars:
apache_package: apache2
uk:
children:
peterborough:
hosts:
192.168.122.4: {}
192.168.122.5: {}
192.168.122.6: {}
ungrouped: {}

The Ansible Variables are stored within the definition of the parent group and cannot be separated and lost. If we want to see the use of the variables we can use an ad-hoc command to install the Apache Web Server.

Using Variables in Ad-Hoc Commands

$ ansible ubuntu -m apt -a 'name="{{ apache_package }}" state=latest'
$ ansible rhel -m yum -a 'name="{{ apache_package }}" state=latest'

NOTE: For clarity and brevity the output of the command has been omitted. If the Web Server was not installed it will be installed. For ubuntu we use the apt module and for rhel we use the yum module.

Using the variable we only need to change the module name, we can leave the package name untouched. The name of the package would normally be written as name=httpd or name=apache2; when we use variables we need to quote and use the double brace bracket. Hence it becomes name=”{{ apache_package }}”

Share this:

  • Click to share on Twitter (Opens in new window)
  • Click to share on Facebook (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Reddit (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
  • Click to share on Tumblr (Opens in new window)
  • Click to print (Opens in new window)

Filed Under: RH294

Primary Sidebar

Newest Video

The Urban Penguin On Youtube

Categories

Pages

  • About The Urban Penguin
  • Contact Us
  • Shop
    • Basket
    • Checkout
    • My Account
  • LPI Training from The Urban Penguin
    • Live and Pluralsight hosted courses
    • Complete Linux Essentials
    • LPIC-3 Senior Level Certification
      • LPIC-3 Exam 300 : Mixed Environments
      • LPIC-3 Exam 303 : Security
      • LPIC-3 Exam 304 : Virtualization and High Availability
    • LPIC-2 Certified Linux Engineer
    • LPIC-1 Linux Administrator
    • LPI Linux Essentials for Raspberry Pi
    • LPI Linux Essentials
  • Operating System Tutorials
    • Linux Foundation Training
    • Solaris 11 OCA 1ZO-821
    • Learning Ubuntu
    • Learning SUSE
    • Red Hat and CentOS Training
      • RHCE – EX294 – Automation With Ansible
      • RHCSA – System Admin 1 – RH124
      • RHCSA – System Admin 2 – RH134
  • Scripting – the power of repetition!
    • Java 7 OCA Exam 1ZO-803
    • General Java Tutorials
    • Learn C Programming using Linux and the Raspberry Pi
    • Ruby Scripting in Linux
    • Scripting with PowerShell
    • PERL Scripting in Linux
    • Learn Shell Scripting with BASH
    • Master Editing Text Files Using VIM
  • Linux Technologies
    • Learning PUPPET
    • openLDAP Directories on Linux
    • Monitoring with Nagios Core
    • Linux File-Systems
    • Learning SAMBA
    • Apache HTTPD Server
    • Learning PHP
    • MYSQL
  • OpenStack
    • Pluralsight
    • Udemy
    • Raspberry Pi Tutorials
    • Citrix Videos
  • Online Instructor-led Courses
    • Red Hat Enterprise Linux System Administration 1 – RH124
    • SELinux Masterclass
    • Bash Scripting Masterclass
    • Nftables Masterclass

© 2021 The Urban Penguin · All Rights Reserved