---
# Creat the scripts that will be used to deploy the undercloud
# environment.
- name: Check the dns information provided by the virthost
  shell: >
    awk 'match($0, /nameserver\s+(([0-9]{1,3}.?){4})/,address){ print address[1] }' /etc/resolv.conf
  delegate_to: virthost
  register: virthost_nameservers_output
  when: nameserver_from_virthost|bool

- name: set name server facts
  set_fact:
    virthost_nameservers: "{{ virthost_nameservers_output.stdout_lines }}"
  when: nameserver_from_virthost|bool

- when: undercloud_hieradata_override_file is defined
  block:
    - name: deprecation message
      vars:
        deprecation_warning_msg: |
             WARNING: The variable undercloud_hieradata_override_file
             is deprecated for removal since Pike.
             Reason: Both the undercloud and overcloud can use
             hieradata overrides and we must distinguish between the two.
             The variable will be removed in the "R" release.
             Please revise your code to use the new variable,
             and file name.

      debug:
        msg: "{{ deprecation_warning_msg.split('\n') }}"

    - name: Create the deprecated undercloud hieradata overrides
      template:
        src: "{{ undercloud_hieradata_override_file }}"
        dest: "./hieradata-overrides-classic-undercloud.yaml"
        mode: 0600

- name: Create t-h-t driven undercloud hieradata overrides
  when: undercloud_install_cli_options == '--use-heat' or containerized_undercloud|bool
  block:
    - name: Generate undercloud hieradata overrides file from template
      template:
        src: "{{ hieradata_override_file_t_h_t_undercloud }}"
        dest: "./hieradata-overrides-t-h-t-undercloud.yaml"
        mode: 0600

    - name: Set fact for undercloud hieradata overrides file
      set_fact:
        undercloud_hieradata_override: "./hieradata-overrides-t-h-t-undercloud.yaml"

- name: Create classic undercloud hieradata overrides
  when: not containerized_undercloud|bool
  block:
    - name: Generate undercloud hieradata overrides file from template
      template:
        src: "{{ hieradata_override_file_classic_undercloud }}"
        dest: "./hieradata-overrides-classic-undercloud.yaml"
        mode: 0600

    - name: Set fact for undercloud hieradata overrides file
      set_fact:
        undercloud_hieradata_override: "./hieradata-overrides-classic-undercloud.yaml"

- name: Create undercloud configuration
  template:
    src: "{{ undercloud_config_file }}"
    dest: "./undercloud.conf"
    mode: 0600

- name: Create undercloud install script
  template:
    src: "{{ undercloud_install_script }}"
    dest: "{{ working_dir }}/undercloud-install.sh"
    mode: 0755

- name: Prepare undercloud custom t-h-t branch/refspec
  include_role:
    name: tht-prep-config
  vars:
    custom_tht_log: "{{ undercloud_custom_tht_log }}"
    custom_tht_script: "{{ undercloud_custom_tht_script }}"
    tht_templates_repo: "{{ undercloud_templates_repo|default('') }}"
    tht_templates_refspec: "{{ undercloud_templates_refspec|default('') }}"
    tht_templates_branch: "{{ undercloud_templates_branch|default('') }}"
    tht_templates_path: "{{ undercloud_templates_path }}"
    composable_scenario: "{{ undercloud_composable_scenario }}"
    upgrade_composable_scenario: "{{ undercloud_upgrade_composable_scenario }}"
    prep_post_hook_script: "{{ undercloud_prep_post_hook_script }}"
    download_templates_rpm: "{{ download_undercloud_templates_rpm }}"
    tht_rpm_url: "{{ undercloud_tht_rpm_url }}"
  when:
    - undercloud_templates_branch is defined or undercloud_templates_refspec is defined
    - undercloud_templates_repo is defined
    - undercloud_templates_path is defined

- name: Create the undercloud parameter defaults file.
  template:
    src: undercloud-parameter-defaults.yaml.j2
    dest: "{{ working_dir }}/undercloud-parameter-defaults.yaml"
    mode: 0600

- name: Create containers-prepare-parameter.yaml
  when: containerized_undercloud|bool or containerized_overcloud|bool
  block:

    - name: Write containers-prepare-parameter.yaml
      template:
        src: containers-prepare-parameter.yaml.j2
        dest: "{{ working_dir }}/containers-prepare-parameter.yaml"
