fr33jc / bang

The beginning of the universe...
GNU General Public License v3.0
22 stars 7 forks source link

Aliasing config_scopes in local scope #3

Closed sjmc7 closed 10 years ago

sjmc7 commented 11 years ago

It'd be useful to be able to rename a global config in the local scope. This would allow multiple server roles to share a local config_scope without copy and pasting; a mixture of reusing globally defined config sections with the flexibility of local config_scopes.

For example:

- name: logging_servers
  # Inside a server role definition:
  config_scopes:
      # Could put 'elasticsearch' here but would need to copy
      # it for each role that requires it
      - ....
  config_aliases:
      # Playbook can refer to {{elasticsearch.something}}
      - elasticsearch: elasticsearch_logging

- name: monitoring
  config_aliases:
      - elasticsearch: elasticsearch_logging

# Global scope
elasticsearch_logging:
    cluster_name: logging

# Second set of elasticsearch config options
elasticsearch_documents:
    cluster_name: documents
fr33jc commented 10 years ago

YAML already provides an aliasing mechanism using & to create an anchor and * as an alias for the value/blob of the matching anchor.

Would something like this work for you?

E.g.

# Config Scopes
# =============
# Note that these are defined in the config file *before* they are referenced
# in the server definitions below.
elasticsearch_logging: &elasticsearch_logging
  cluster_name: logging

# Second set of elasticsearch config options
elasticsearch_documents:
  cluster_name: documents

# Resource Definitions
# ====================
servers:
  logging_servers:
    config_scopes:
    - elasticsearch: *elasticsearch_logging

  monitoring:
    config_scopes:
    - elasticsearch: *elasticsearch_logging

which would get parsed into the following Python object:

{'elasticsearch_documents': {'cluster_name': 'documents'},
 'elasticsearch_logging': {'cluster_name': 'logging'},
 'servers': [{'config_scopes': [{'elasticsearch': {'cluster_name': 'logging'}}],
              'name': 'logging_servers'},
             {'config_scopes': [{'elasticsearch': {'cluster_name': 'logging'}}],
              'name': 'monitoring'}]}
sjmc7 commented 10 years ago

Yep, that'll do it - I subsequently read up on my yaml and did as you suggest.