Ansible Variables

In Ansible, we use Variables as a way to deal with things that differ between one system and the next.

There's nothing particularly complicated about using Variables inside a Playbook. We use a very similar syntax to that which you've already seen:

- hosts: all
    - variable_name: /our/variable/contents
    - another_variable: "some text here"

As part of the video, we will use the variable we define to tell Ansible where we expect our website content directory to be created - /var/www/ - and take advantage of the Ansible File Module to actually create the expected directory on our target system.

To use a variable in a task is really easy, we just enclose the variable name in some {{ double handlebars }}.

As such, our Playbook becomes (shortened for brevity):

- hosts: all
    - website_dir: /var/www/

    - name: Create website directory
      file: dest={{ website_dir }} mode=775 owner=www-data group=www-data

Pretty easy really. Nothing crazy happening, just declaring a variable and then using it.

Naming Variables

One quick heads up - naming variables is pretty straightforward. Either stick to single words:

  • foo
  • bar
  • fooBar

Or go with underscores:

  • foo_bar
  • baz_qux

Hyphens, spaces, and all numbers are not allowed. More info here if unsure.

Of course, there is more to be done with Variables inside Ansible, but that's enough to get you started.

Default Variables

Occasionally you may wish to have a variable defined in a task which may or may not be provided by the playbook.

In this instance, to avoid errors, you are best setting a default variable:

- name: symfony logs
  file: >
    path="{{ item.log_directory }}"
    owner="{{ item.root_directory_owning_user | default('www-data') }}"
    group="{{ item.root_directory_owning_group | default('www-data') }}"
  sudo: yes
  with_items: "{{ symfony_websites }}"

Seems ok, but a really useful feature here is to have a default which itself is a variable:

- name: symfony logs
  file: >
    path="{{ item.log_directory }}"
    owner="{{ item.root_directory_owning_user | default(symfony_web_server_user) }}"
    group="{{ item.root_directory_owning_group | default(symfony_web_server_group) }}"
  sudo: yes
  with_items: "{{ symfony_websites }}"

Notice the single pipe. This is not an or statement, but rather a pipe into the Jinja2 default filter.

We could then set the symfony_web_server_user inside the your-role/defaults/main.yml file:

symfony_web_server_user: "www-data"
symfony_web_server_group: "www-data"

Which allows anyone using the playbook to override this value as required, without needing us to hardcode a specific user as the default across multiple lines / files in our playbook.

Code For This Course

Get the code for this course.


# Title Duration
1 How To Install Ubuntu Server in Oracle VirtualBox 10:21
2 How to Rename our Ubuntu Server 02:00
3 Installing Ansible on Ubuntu Server 00:33
4 Safety First, Safety Second - Snapshots are like Ctrl+Z 00:11
5 Managing the Ansible Inventory Hosts File 02:16
6 Ansible Ad Hoc Commands 04:27
7 Introduction to Ansible Playbooks 02:14
8 Ansible Handlers 01:39
9 Ansible Variables 03:16
10 Git Your Deploy Just Right 05:35
11 Ansible Roles 05:51
12 Looping in Ansible with_items 04:35
13 Ansible Files For Beginners 06:16
14 Variable Precedence - Where To Put Your Role Vars? 04:13
15 Ansible Templates 05:51
16 Ansible Inventory With Our Own Hosts Files 06:57
17 How to Manage Users with Ansible 08:32
18 Ansible Vault Tutorial 03:48
19 Ansible Galaxy Tutorial 10:03
20 Real World Ansible - Common Role Walkthrough 06:20
21 Ansible MySQL Tutorial 13:44
22 Ansible Symfony and nginx 09:37