# 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: Set hostname
  shell: hostname {{ ansible_hostname }}

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

- set_fact:
    block_str: "{{ block_str | default('') + hostvars[item].host_ip + ' ' + hostvars[item].ansible_host + '\n'}}"
  with_items: "{{ groups['ostack'] }}"

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

- name: Update /etc/hosts
  lineinfile:
    path: /etc/hosts
    regexp: ".*{{ hostvars[groups['jumphost'][0]].proxy_host }}.*"
    line: "{{ hostvars[groups['jumphost'][0]].proxy_host_ip }} {{ hostvars[groups['jumphost'][0]].proxy_host }}"

- name: Turn off IPv6
  lineinfile:
    path: /etc/sysctl.conf
    regexp: '^{{ item }}.*'
    line: "{{ item }} = 1"
  with_items:
    - 'net.ipv6.conf.all.disable_ipv6'
    - 'net.ipv6.conf.default.disable_ipv6'
    - 'net.ipv6.conf.lo.disable_ipv6'

- name: Update IP configuration
  shell: sysctl -p

- name: Update resolv.conf
  shell: echo "{{ 'nameserver ' + hostvars[ansible_host].ansible_default_ipv4.gateway }}" > /etc/resolvconf/resolv.conf.d/base

- name: Update name servers
  shell: resolvconf -u

- name: Update /etc/environment
  lineinfile:
    path: /etc/environment
    regexp: "{{ item.find }}"
    line: "{{ item.add }}"
  with_items:
   - { find: 'http_proxy=', add: "{{ 'export http_proxy=' + lookup('env', 'http_proxy') }}" }
   - { find: 'https_proxy=', add: "{{ 'export https_proxy=' + lookup('env', 'https_proxy') }}" }
   - { find: 'ftp_proxy=', add: "{{ 'export ftp_proxy=' + lookup('env', 'ftp_proxy') }}" }
   - { find: 'no_proxy=', add: "{{ 'export no_proxy=' + lookup('env', 'no_proxy') + ',' + ansible_host + ',' + hostvars[ansible_host].ansible_default_ipv4.address }}" }