Ansible – Part 7 – Managing Variables

Ansible Variables

You can use variables in ansible plays to store values like users to create, packages to install etc. By using variables, you can manage your plays with dynamic input values.

See Automation with Ansible full chapters.

Example Variable usages:

  • Users to create
  • Packages to install
  • Services to restart
  • Files to more or create

Refer : Ansible Documentation

Variables Naming

You can use any custom naming for your variable but remember to start with a letter and only include letters, numbers and underscores.

Valid Variable NamesInvalid Names
file_namefile name
webserver_11st webserver

Defining Variables

You can define variables at different levels in Ansible project and in a simple view, we must learn below scopes.

  • Global Scope – when you set variables in Ansible configuration or via command line.
  • Play Scope – set in the play
  • Host Scope – when you set variables for hosts or groups inside inventory, fact gathering or registered tasks.

Variables in Playbook

You can define variables simply inside playbook by vars block or by vars_files directive.

- name: Install and configure lamp
  hosts: lamp
    firewall_service: firewalld
    web_package: httpd

If you are using vars_files, you need to define variables inside a yaml file and call it inside playbook.

$ cat vars/myvars.yml
web_package: httpd
firewall_service: firewalld

And use vars_files directive as below

    - name: Include the variable file
      include_vars: vars/myvars.yml

So, how can you use these variables inside playbook ?

You can simply call the variable as below.

    # This line will read as : "Install httpd"
    - name: Install {{ web_package }}
        name: "{{ web_package }}"
        state: latest

Please note, when you call variable as first element, make sure you have used quotes as shown above.

Host Variables and Group Variables

You can define variable on host level or group level via inventory; but please note, variable defined in playbook will take precedence of both.

# example for host variable
[servers] ansible_user=devops ansible_user=sysadmin

# example for group variable

Defining variables inside inventory will make the inventory file messy. So the recommended way is to use group_vars and host_vars directories where you can save files with variables for each hosts or groups.

$ cat group_vars/servers
# This is the group variable for hostgroup servers.
user: devops

$ cat group_vars/webservers
# This is the group variable for hostgroup webservers.
web_package: httpd

$ cat host_vars/
# This is the host variable for
ansible_user: devops

$ cat host_vars/
# This is the host variable for
ansible_user: sysadmin

How to override variable from command line ?

Okay, you have defined variables in playbook or host_vars or group_vars. But, what if you need to test with different value for a variable ? Simple, you don’t need change anything in playbook or host_vars; you can simply override the variable while executing playbook as below; using option -e.

$ ansible-playbook web.yml -e "package=apache"

Usage of Variable Arrays

When you have multiple variables which are related, you can easily define them as arrays instead of individual variables. See below, the variables on left side can easily define as array as right side.

user1_firstname: John
user1_lastname: Brandon
user1_designation: SysAdmin
user2_firstname: Marry
user2_lastname: Brown
user2_designation: Developer
    firstname: John
    lastname: Brandon
    designation: SysAdmin
    firstname: Marry
    lastname: Brown
    designation: Developer

And you can call them conveniently as below


# or in Python style dictionary, which is better than dot notation.

Registered Variables

We can capture the output of a task in a variable using register statement; which is called registered variable. The content of registered variable can use for checking some conditions or for debugging purposes.

   - name: install the web package
      name: nginx-new
      state: latest
     register: package
   - name: print jason output
     debug: var=package
   - name: print package failed
      msg: "Package Failed"
     when: package.failed == true

See Automation with Ansible full chapters.

Gineesh Madapparambath

(aka Gini Gangadharan) - Gineesh has worked as a Systems Engineer, Automation Specialist, and content author. His primary focus is on Ansible Automation, Containerization (OpenShift & Kubernetes), and Infrastructure as Code (Terraform).

You may also like...

%d bloggers like this: