HiveBrain v1.2.0
Get Started
← Back to all entries
snippetMinor

I don't fully understand how to use variables in Ansible roles

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
rolesunderstandfullyvariableshowuseansibledon

Problem

Original question:

I want to roll out a partitioning layout for a home lab setup of GlusterFS and also want to do it using Ansible so I can re-use the configuration for a more stable setup later on. This is also the first time I work with Ansible so please bear with me ;)

How do I use variables across roles correctly?

I created a role for the basic partitioning of a single data disk and I want to have 3 disks on each node.

My nodes are as follows (path = inventories/staging/hosts.yaml)

all:
  hosts:
    node1:
      ansible_host: node1
    node2:
      ansible_host: node2

storageservers:
  hosts:
    node1:
      ansible_host: node1
    node2:
      ansible_host: node2


The base role used by the aggregating role is as follows (path = roles/gluster-node-partition/tasks/main.yml):

---
# tasks file for gluster-node-partition
- name: 'Create {{ partition_name }} partition'
  parted:
    device: '{{ device_name }}'
    number: 1
    label: gpt
    name: '{{ partition_name }}'
    state: present
  become: true

- name: 'Create filesystem on {{ partition_name }} partition'
  filesystem:
    fstype: btrfs
    dev: '{{ device_name }}1' #should expand to something like '/dev/sdb1'
  become: true

- name: 'Create mount point for {{ partition_name }} partition'
  file:
    path: '/{{ partition_name }}'
    state: directory
  become: true

- name: 'Mount {{ partition_name }} partition'
  mount:
    path: '/{{ partition_name }}'
    src: 'LABEL={{ partition_name }}'
    fstype: btrfs
    state: present
  become: true


The aggregating role is as follows (path = roles/gluster-node/tasks/main.yml):

```
---
# tasks file for gluster-node
  • include_role:


name: gluster-node-partition

  • name: Create data1 partition


include_role:
name: gluster-node-partition
vars:
device_name: '/dev/sdb'
partition_name: 'data1'

  • name: Create data2 partition


include_role:
name: gluster-node-partition
vars:
device_name: '/dev/sdc'
partitio

Solution

Your very first role include does not have variables provided with it, so it will fail.

Here's an excerpt from above showing this. Notice lines 1-2 have no variables after them (unlike the include following that which does correctly provide the variables).

- include_role:
    name: gluster-node-partition

- name: Create data1 partition
  include_role:
    name: gluster-node-partition
  vars:
    device_name: '/dev/sdb'
    partition_name: 'data1'


As roles/gluster-node-partition/tasks/main.yml uses device_name twice, and it is not defined for that first call, it will fail. The two usages are noted on line 2 and on the last line below.

parted:
    device: '{{ device_name }}'
    number: 1
    label: gpt
    name: '{{ partition_name }}'
    state: present
  become: true

- name: 'Create filesystem on {{ partition_name }} partition'
  filesystem:
    fstype: btrfs
    dev: '{{ device_name }}1' #should expand to something like '/dev/sdb1'

Code Snippets

- include_role:
    name: gluster-node-partition

- name: Create data1 partition
  include_role:
    name: gluster-node-partition
  vars:
    device_name: '/dev/sdb'
    partition_name: 'data1'
parted:
    device: '{{ device_name }}'
    number: 1
    label: gpt
    name: '{{ partition_name }}'
    state: present
  become: true

- name: 'Create filesystem on {{ partition_name }} partition'
  filesystem:
    fstype: btrfs
    dev: '{{ device_name }}1' #should expand to something like '/dev/sdb1'

Context

StackExchange DevOps Q#11188, answer score: 1

Revisions (0)

No revisions yet.