|
| 1 | +# Provision |
| 2 | + |
| 3 | +A role that provisions Cloudera-specific inventory. |
| 4 | + |
| 5 | +The role requires the following two files that are locatable by the enclosing play: |
| 6 | + |
| 7 | +* *hostvars.j2* - a Jinja macro that outputs a host's variables in a static inventory file |
| 8 | +* *instance_vars.j2* - a Jinja macro that outputs an instance's metadata, i.e. tags, in the provider |
| 9 | + |
| 10 | +These two Jinja macros _expect variables on the host_ which are assigned via the `add_host` call |
| 11 | +within the role. To set these variables, use the `module_defaults` assignment within the enclosing |
| 12 | +play of the role. |
| 13 | + |
| 14 | +## Examples |
| 15 | + |
| 16 | +### module_defaults |
| 17 | + |
| 18 | +The `node` variable is in scope of the `add_host` module and contains the output of the Terraform |
| 19 | +node provisioning configuration. |
| 20 | + |
| 21 | +```yaml |
| 22 | +- name: Provision resources |
| 23 | + hosts: localhost |
| 24 | + connection: local |
| 25 | + gather_facts: no |
| 26 | + module_defaults: |
| 27 | + ansible.builtin.add_host: |
| 28 | + groups: "{{ node.groups | default(omit) }}" |
| 29 | + host_template: "{{ node.metadata.host_template | default(omit) }}" |
| 30 | + storage_volumes: "{{ node.storage_volumes | default([]) }}" |
| 31 | + tls: "{{ node.metadata.tls | default(omit) }}" |
| 32 | + tasks: ... |
| 33 | +``` |
| 34 | +
|
| 35 | +### hostvars.j2 |
| 36 | +
|
| 37 | +```jinja |
| 38 | +{# Collect and output individual host variables #} |
| 39 | +{% macro host_variables(host) %} |
| 40 | +{% set fields = [] %} |
| 41 | +{% set _ = fields.append("ansible_user=" + host['ansible_user']) if 'ansible_user' in host %} |
| 42 | +{% set _ = fields.append("host_template=" + host['host_template']) if 'host_template' in host %} |
| 43 | +{% set _ = fields.append("label=" + host['label']) if 'label' in host %} |
| 44 | +{% set _ = fields.append("tls=" + host['tls'] | string) if 'tls' in host %} |
| 45 | +{{ host['inventory_hostname'] }} {{ fields | join(' ') }} |
| 46 | +{%- endmacro %} |
| 47 | +``` |
| 48 | + |
| 49 | +### instance_vars.j2 |
| 50 | + |
| 51 | +```jinja |
| 52 | +{# Define the metadata tags for the individual Openstack instances #} |
| 53 | +{# Output should be TF map _entries_, not a map itself #} |
| 54 | +
|
| 55 | +{% macro instance_tags(host) %} |
| 56 | +{% set tags = {} %} |
| 57 | +{% set _ = tags.update({ 'ansible_user': host.ansible_user }) if host.ansible_user is defined %} |
| 58 | +{% set _ = tags.update({ 'host_template': host.host_template }) if host.host_template is defined %} |
| 59 | +{% set _ = tags.update({ 'groups': host.groups | join(', ') }) if host.groups is defined %} |
| 60 | +{% set _ = tags.update({ 'tls': host.tls | string }) if host.tls is defined %} |
| 61 | +{% for k, v in tags.items() %} |
| 62 | + {{ k }} = "{{ v }}"{{ "," if not loop.last else "" }} |
| 63 | +{% endfor %} |
| 64 | +{%- endmacro %} |
| 65 | +``` |
0 commit comments