Pages

Friday, March 10, 2023

How to use group_vars with Ansible role

Scenario:

We have these 6 servers in hosts file:

[web_servers]
10.200.0.2
10.200.0.3

[db_servers]
10.200.0.6
10.200.0.7

[dns_servers]
10.200.0.8
10.200.0.9


We want Bind9 to be installed only on dns_servers
We want MariaDB to be installed only on db_servers
We want Nginx to be installed only on web_servers
We want user 'notadmin' to be created on all servers

For this example I will use Debian based servers.
The hosts file (see above) is copied in current working directory
First we create ansible role named "using-ansible-group-vars-example:

$ ansible-galaxy init using-group-vars-example

Then we create file playbook-using-group-vars-example.yml in the current working directory with the following content:
---
- name: How to use group_vars example
  hosts: all
  remote_user: admin
  become: yes
  roles:
        - ./using-group-vars-example

Note: all remote hosts should be configure according Ansible documentation for this to work (ssh-copy-id and user "admin" in the /etc/sudoers with access to sudo with no password).

Then we create group_vars directory again in the current working directory:

$ mkdir group_vars

Now we have directory structure like this:



Create the following files inside group_vars/ directory with the names of the server groups from our hosts file

group_vars/db_servers.yml

---
install_mariadb: "true"
create_user: "true"

group_vars/dns_servers.yml
---
create_user: "true"
install_dns: "true"

group_vars/web_servers.yml
---
install_nginx: "true"
create_user: "true"

Content of the using-group-vars-example/defaults/main.yml should be this:
---
# defaults file for using-group-vars-example
# we must initialize these otherwise it will pop an error
create_user: none
install_nginx: none
install_mariadb: none
install_dns: none

Content of the using-group-vars-example/tasks/main.yml should be this:

---
# tasks file for using-group-vars-example
- name: install nginx
  include_tasks: nginx.yml
  when: install_nginx == "true"

- name: create user
  include_tasks: user.yml
  when: create_user == "true"

- name: install dns server
  include_tasks: dns.yml
  when: install_dns == "true"

- name: install mariadb server
  include_tasks: db.yml
  when: install_mariadb == "true"

Now we create 4 more files inside the using-group-vars-example/tasks/ as follow:

using-group-vars-example/tasks/dns.yml
---
- name: Install BIND9
  apt:
    name: bind9 bind9-utils
    state: present
    update_cache: yes

using-group-vars-example/tasks/db.yml

---
- name: Install MariaDB server
  apt:
    name: mariadb-server mariadb-server-core
    state: present
    update_cache: yes

using-group-vars-example/tasks/nginx.yml
---
- name: Update the repository cache and update package "nginx"
  apt:
    name: nginx
    state: present
    update_cache: yes

using-group-vars-example/tasks/user.yml
---
- name: Add the user 'notadmin'
  user:
    name: notadmin
    state: present
    comment: notadmin user for testing purposes
    createhome: yes
    home: /home/notadmin

These will be included only when declared in group_vars/ yml files

Now the final structure should look like this:



now we run this from the . directory like this:

$ ansible-playbook playbook-using-group-vars-example.yml -i hosts