Diogène Mutombo
Diogène Mutombo

Reputation: 33

ansible groups.variable in a tasks

I am trying to use a variable I have defined in a group_vars/file.yml and use it in a taks of my role like groups.my_variable (my_variable is the name of the group define in my inventory)

Here is my role tasks:

  ---

- name: "Create strusted storage pool"
  gluster_peer:
    state: "{{ CURRENT_NODES.state }}"
    nodes: "{{groups.group_name}}"
  tags: STORAGE_POOL

my inventory

[gluster]
glusterfs01
glusterfs02
ly-lab-elk
lpic

when I use it like this it works:

---
    
    - name: "Create strusted storage pool"
      gluster_peer:
        state: "{{ CURRENT_NODES.state }}"
        nodes: "{{ groups.gluster }}"
      tags: STORAGE_POOL

When I use groups.gluster directly in my tasks/pool_storage.yml as above I have expected result

TASK [debug] ******************************************************************************************************************************************************************************************************
ok: [glusterfs01] => {
    "groups.gluster": [
        "glusterfs01",
        "glusterfs02",
        "ly-lab-elk",
        "lpic"
    ]
}
ok: [glusterfs02] => {
    "groups.gluster": [
        "glusterfs01",
        "glusterfs02",
        "ly-lab-elk",
        "lpic"
    ]
}
ok: [ly-lab-elk] => {
    "groups.gluster": [
        "glusterfs01",
        "glusterfs02",
        "ly-lab-elk",
        "lpic"
    ]
}
ok: [lpic] => {
    "groups.gluster": [
        "glusterfs01",
        "glusterfs02",
        "ly-lab-elk",
        "lpic"
    ]
}

But when I use the variable set in group_vars/gluster.yml

group_names: gluster

in my tasks tasks/pool_storage.yml

nodes: "{{groups.group_name}}"

I have this result which is not good

TASK [debug] ******************************************************************************************************************************************************************************************************
ok: [glusterfs01] => {
    "group_names": [
        "gluster"
    ]
}
ok: [glusterfs02] => {
    "group_names": [
        "gluster"
    ]
}
ok: [ly-lab-elk] => {
    "group_names": [
        "gluster"
    ]
}
ok: [lpic] => {
    "group_names": [
        "gluster"
    ]
}

As I have different groups in my inventory file, I want to use a variable in which I will define my group name and then append it to groups.group_name or other way but to have the expected result.

Can someone help ?

Thank you in advance.

Upvotes: 0

Views: 3133

Answers (2)

seshadri_c
seshadri_c

Reputation: 7340

So, to reference a group_name variable in groups you can try using the groups[group_name] syntax.

An example inventory:

[servers]
host-101.xxx.com
host-202.xxx.com

[group1]
g1-server1
g1-server2

A group_vars/servers.yaml:

group_name: servers

A playbook:

- hosts: servers
  connection: local

  tasks:
    - debug:
        var: groups[group_name]

Gives the servers under group1 defined as group_name:

ok: [host-101.xxx.com] => {
    "groups[group_name]": [
        "host-101.xxx.com",
        "host-202.xxx.com"
    ]
}
ok: [host-202.xxx.com] => {
    "groups[group_name]": [
        "host-101.xxx.com",
        "host-202.xxx.com"
    ]
}

One more thing to note is that you don't need to establish the peers on each host of GlusterFS cluster. So, either:

  • target any 1 host of that group

or

  • add run_once: true to the task

Example:

- name: "Create strusted storage pool"
  gluster_peer:
    state: "{{ CURRENT_NODES.state }}"
    nodes: "{{ groups[group_name] }}"
  tags: STORAGE_POOL
  run_once: true

Upvotes: 1

Vladimir Botka
Vladimir Botka

Reputation: 68034

Put the variable into the file group_vars/gluster.yml if you want the hosts of the group gluster to use it. The name of the file must be identical to the name of the group. This way Ansible will recognize to which groups the files in the directory group_vars belong.

If you can't change the name of group_vars/file.yml link it to group_vars/gluster.yml.

For example, given the simplified inventory for testing

shell> cat hosts
[gluster]
glusterfs01
glusterfs02

The group_vars

shell> cat group_vars/gluster.yml 
my_variable: foo

and the playbook

shell> cat playbook.yml 
- hosts: gluster
  gather_facts: false
  tasks:
    - debug:
        var: my_variable

give (abridged)

shell> ansible-playbook -i hosts playbook.yml

TASK [debug] *******************************************************
ok: [glusterfs01] => 
  my_variable: foo
ok: [glusterfs02] => 
  my_variable: foo

Upvotes: 0

Related Questions