cloudera-labs / cloudera-deploy

A general purpose framework for automating Cloudera Products
Apache License 2.0
63 stars 61 forks source link

#94 Load definition_file with include vars #96

Closed anisf closed 1 year ago

anisf commented 1 year ago

Allows to use variables in definition_file, e.g. :

clusters:

- name: "{{ cdp_cluster_name }}"
  type: base
  services: "{{ cdp_services }}"
  databases: "{{ cdp_databases }}"
  configs: "{{ fresh_install_configs }}"
  host_templates: "{{ cdp_host_templates }}"
...
Chaffelson commented 1 year ago

So there is a reason for this, and it is a trade off. The definition.yml is loaded with variable evaluation, but the cluster.yml is not, so that those variables are evaluated later in the process. The initial import of the cluster template is just to import information like the repositories, however for many cluster templates it's necessary to evaluate the template once the rest of the services are built so you can pick up things like hostnames and service names which don't exist until that point of the process. This isn't ideal for templating parts of the cluster definition as you are doing, so there may be a better way to do it that can be found, but importing cluster.yml with variables evaluated at the start of the deployment would break in those late-evaluation use-cases, so this was the compromise in the name of backwards compatibility.

anisf commented 1 year ago

Just to be clear, the variable _pre_template_cluster is only used to do the following check :

- name: Find the correct host template
  block:
  - fail:
      msg: "Unable to host template {{ host_template }} in the cluster definition"
    when: content | length == 0
  - set_fact:
      host_template_content: "{{ content | first }}"
  vars:
    query: "clusters[].host_templates[].\"{{ host_template }}\""
    content: "{{ _pre_template_cluster | json_query(query) }}"
  when: host_template is defined

You can do a search on all cloudera-labs repositories to find the modified variable here

This PR does not touch to any other variable that _pre_template_cluster

The include_vars task in this PR is stored under the _pre_template_cluster variable. Just like the replace lookup actually does :

- name: Load Definition file
  ansible.builtin.include_vars:
    file: "{{ init__cluster_definition_file }}"
    name: _pre_template_cluster
...

And if you take a look at the distribute_facts_to_inventory.yml file a few lines after, you'll see almost the same task, but this time, loading variables directly instead of under a variable :

- name: Include Cluster Definition override
  ansible.builtin.include_vars:
    file: "{{ init__cluster_definition_file }}"
  delegate_to: "{{ __play_host }}"
  delegate_facts: true
  loop: "{{ groups.all }}"
  loop_control:
    loop_var: __play_host
    label : __play_host

Notice the name parameter for the include_vars is not present in this task