Usage as a role dependency

The debops.rabbitmq_server role can be used as a dependency by other Ansible roles to manage RabbitMQ main configuration file idempotently. Configuration options from multiple roles can be merged together and included in the configuration file, or removed conditionally.

Dependent role variables

The role exposes three default variables that can be used by other Ansible roles as dependent variables:

rabbitmq_server__dependent_role
Required. Name of the role that uses the debops.rabbitmq_server as a dependency. This will be used to store the configuration in its own YAML dictionary. The selected name shouldn't be changed, otherwise configuration will be desynchronized.
rabbitmq_server__dependent_config
Required. List of the RabbitMQ configuration options defined in the same format as the main configuration. See rabbitmq_server__config for more details.
rabbitmq_server__dependent_state
Optional. If not specified or present, the configuration will be included in the /etc/rabbitmq/rabbitmq.config configuration file and stored as Ansible local fact. if absent, the configuration will be removed from the generated configuration file.

Dependent configuration storage and retrieval

The dependent configuration from other roles is stored in the secret/ directory on the Ansible Controller (see debops.secret for more details) in a JSON file, with each role configuration in a separate dictionary. The debops.rabbitmq_server role reads this file when Ansible local facts indicate that the RabbitMQ service is installed, otherwise a new empty file is created. This ensures that the stale configuration is not present on a new or re-installed host.

The YAML dictionaries from different roles are be merged with the main configuration in the rabbitmq_server__combined_config variable that is used to generate the final configuration. The merge order of the different rabbitmq_server__*_config variables allows to further affect the dependent configuration through Ansible inventory if necessary, therefore the Ansible roles that use this method don't need to provide additional variables for this purpose themselves.

Example role default variables

---

# This is a set of default variables in an example 'application' role that uses
# dependent variables to pass configuration to 'debops.rabbitmq_server' role.

# State of the application deployment
application__deploy_state: 'present'

# RabbitMQ configuration defined by the application
application__rabbitmq_server__dependent_config:

  - name: 'application_name'
    options:

      - name: 'config_first_option'
        value: 'value1'

      - config_second_option: 'value2'

Example role playbook

---

# This is a playbook for an example 'application' role which uses
# 'debops.rabbitmq_server' as a dependency and passes its own set of
# configuration options to it.

- name: Manage application
  collections: [ 'debops.debops' ]
  hosts: [ 'debops_service_rabbitmq_application' ]
  become: True

  environment: '{{ inventory__environment | d({})
                   | combine(inventory__group_environment | d({}))
                   | combine(inventory__host_environment  | d({})) }}'

  pre_tasks:

    - name: Prepare rabbitmq_server environment
      import_role:
        name: 'rabbitmq_server'
        tasks_from: 'main_env'
      tags: [ 'role::rabbitmq_server', 'role::secret', 'role::rabbitmq_server:config' ]

  roles:

    - role: secret
      tags: [ 'role::secret', 'role::rabbitmq_server', 'role::rabbitmq_server:config' ]
      secret__directories:
        - '{{ rabbitmq_server__secret__directories }}'

    - role: rabbitmq_server
      tags: [ 'role::rabbitmq_server' ]
      rabbitmq_server__dependent_role: 'application'
      rabbitmq_server__dependent_state: '{{ application__deploy_state }}'
      rabbitmq_server__dependent_config:
        - '{{ application__rabbitmq_server__dependent_config }}'

    - role: application
      tags: [ 'role::application' ]