# Copyright (c) 2018 Intel Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
---
- name: Include input file
  include_vars:
    file: "{{ rs_file }}"
    name: infra_deploy_vars

- name: Install setuptools
  apt:
    name: python-setuptools

- name: Install pip
  shell: easy_install pip
  environment: "{{ proxy_env }}"

- name: Install dependency for dns dig
  pip:
    name: dnspython
    state: latest

- set_fact:
    block_str: "{{ block_str | default('') + item.interfaces[0].ip + ' ' + item.hostname + '\n'}}"
  with_items: "{{ infra_deploy_vars.nodes }}"

- name: Delete hosts between markers
  blockinfile:
    path: /etc/hosts
    marker: "# {mark} generated hosts file"
    content: ""

- name: Update /etc/hosts
  blockinfile:
    path: /etc/hosts
    block: |
      {{ block_str }}
    marker: "# {mark} generated hosts file"

- name: Clear known hosts
  shell: >
    ssh-keygen -f /root/.ssh/known_hosts -R "{{ item.interfaces[0].ip }}";
    ssh-keygen -f /root/.ssh/known_hosts -R "{{ item.hostname }}"
  with_items: "{{ infra_deploy_vars.nodes }}"

- set_fact:
    controllers: "{{ controllers | default([]) + [item.hostname] }}"
  when:
    - item.openstack_node is defined
    - item.openstack_node == 'controller'
  with_items: "{{ infra_deploy_vars.nodes }}"

- name: Add host controller as deploy
  add_host:
    hostname: "{{ item.hostname }}"
    host_ip: "{{ item.interfaces[0].ip }}"
    groups: deploy, ostack
    ansible_host: "{{ item.hostname }}"
    ansible_user: "{{ item.user }}"
    ansible_ssh_pass: "{{ item.password }}"
    node_type: "{{ item.openstack_node }}"
    secondary_ip: "{{ item.interfaces[1].ip }}"
  when: item.hostname == controllers[0]
  with_items: "{{ infra_deploy_vars.nodes }}"

- name: Add hosts others as controller, compute
  add_host:
    hostname: "{{ item.hostname }}"
    host_ip: "{{ item.interfaces[0].ip }}"
    groups: regular,ostack
    ansible_host: "{{ item.hostname }}"
    ansible_user: "{{ item.user }}"
    ansible_ssh_pass: "{{ item.password }}"
    node_type: "{{ item.openstack_node }}"
    secondary_ip: "{{ item.interfaces[1].ip }}"
  when:
    - item.openstack_node is defined
    - item.openstack_node == 'controller' or item.openstack_node == 'compute'
    - item.hostname != controllers[0]
  with_items: "{{ infra_deploy_vars.nodes }}"

- name: Add yardstick host to group
  add_host:
    hostname: "{{ item.hostname }}"
    host_ip: "{{ item.interfaces[0].ip }}"
    groups: yardstickG
    ansible_host: "{{ item.hostname }}"
    ansible_user: "{{ item.user }}"
    ansible_ssh_pass: "{{ item.password }}"
    secondary_ip: "{{ item.interfaces[1].ip }}"
  when: item.hostname == 'yardstickvm'
  with_items: "{{ infra_deploy_vars.nodes }}"