FreeKB - Ansible group_vars variables
Ansible - group_vars variables

The group_vars directory can be used to create unique variables for different groups of managed nodes (e.g. target systems). Or, if you have groups of managed nodes in your default hosts file or your own inventory file, you could just as well create unique variables for different groups of managed nodes in your default hosts file or your own inventory file.

Let's say you've a group of Linux servers and a group of Windows servers, like this, and the "foo" variable should contain a value of "Hello" for Linux servers, and a value of "World" for Windows servers.



Create the group_vars directory and the linux.yml and windows.yml files. The name of the YAML files must match the managed node group name.

mkdir /etc/ansible/group_vars
touch /etc/ansible/group_vars/linux.yml
touch /etc/ansible/group_vars/windows.yml


Use Hello in linux.yml.

foo: Hello


Use World in windows.yml.

foo: World


The debug module can be used to print the value in the "foo" variable. Notice in this example that the playbook will be run against the "linux" hosts.

- hosts: linux
    - debug:
        var: foo


In this example, the "foo" variable will contain a value of "Hello".

TASK [debug]
ok: [] => {
    "msg": "Hello"


Add a Comment

We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.

Please enter 7ecbf in the box below so that we can be sure you are a human.


Web design by yours truely - me, myself, and I   |   |