Ansible include_role variable scope

Viewed 180

I have a group of database servers and I need to create specific database users on some of them.

For example, I need the following:

  • 1 readonly users on all DB servers
  • 2 readwrite users on only group1
  • 1 readwrite user on only group2

The idea is to have a single role and apply this role depending on the variables configuration.

This is how I do at the moment:

playbook.yml:

- hosts: dbservers
  tasks:
    - name: Create database users
      include_role:
        name: create_db_users
      loop: "{{ db_users }}"

hosts

all:
  hosts:
    children:
      group1:
        hosts:
          server1
      group2:
        hosts:
          server2
      group3:
        hosts:
          server3
      dbservers:
        children:
          server1:
          server2:
          server3:

group_vars/dbservers.yml

db_users:
- db_username: ro_user1 

group_vars/group1.yml

db_users:
- db_username: rw_user1
- db_username: rw_user2

group_vars/group2.yml

db_users:
- db_username: rw_user1

This is not working because the group1 and group2 variables will overwite the dbservers config, and then I will not have my readonly users on group1 and 2.

How can I use the same behaviour on all servers and only a specific one on others.

Is the include_role + loop solution really the solution here ?

Thank you very much for your help and advises !

0 Answers
Related