From b7130ea6b6f5f5e14a03776f76aa1dd37bbbe670 Mon Sep 17 00:00:00 2001
From: Gorka Benguria Elguezabal <gorka.benguria@tecnalia.com>
Date: Mon, 24 May 2021 00:08:54 +0200
Subject: [PATCH] adds host to the etc/hosts

---
 Vagrantfile                             | 11 +++++++--
 ansible/.gitignore                      |  1 -
 ansible/ansible.cfg                     |  4 ++--
 ansible/hosts-from-inventory/install.sh | 13 ++++++++++
 ansible/hosts-from-inventory/site.yaml  | 25 +++++++++++++++++++
 ansible/hosts.yaml                      | 22 ++++++++++++++---
 ansible/kubernetes/hosts.yaml           |  4 ++++
 ansible/kubernetes/install.sh           | 13 ++++++++++
 ansible/{ => kubernetes}/site.yaml      |  0
 scripts/ansible/install.sh              | 32 ++-----------------------
 10 files changed, 87 insertions(+), 38 deletions(-)
 delete mode 100755 ansible/.gitignore
 create mode 100755 ansible/hosts-from-inventory/install.sh
 create mode 100755 ansible/hosts-from-inventory/site.yaml
 create mode 100755 ansible/kubernetes/hosts.yaml
 create mode 100755 ansible/kubernetes/install.sh
 rename ansible/{ => kubernetes}/site.yaml (100%)

diff --git a/Vagrantfile b/Vagrantfile
index ff9bbca..0edbede 100755
--- a/Vagrantfile
+++ b/Vagrantfile
@@ -148,8 +148,13 @@ Vagrant.configure("2") do |config|
 			# In case there is a private key in the .ssh we add it to the management as id_rsa, in case it is not we will retrieve the key generated by vagrant. The public key is available inside the machine in the authorized_keys file.
 			id_rsa_path = File.expand_path('../.ssh/id_rsa', __FILE__) 
 			if File.exists?(id_rsa_path)
-				subconfig.vm.provision "file", source: keys_folder_path, destination: "/home/vagrant/.ssh/id_rsa" 
+				subconfig.vm.provision "file", source: id_rsa_path, destination: "/home/vagrant/.ssh/id_rsa" 
 				subconfig.vm.provision "shell", name: "fix ssh keys permissions", path: File.expand_path('../scripts/multimachine/fixIdRsaPermissions.sh', __FILE__), privileged: false
+
+				# we copy the public key
+				id_rsa_pub_path = File.expand_path('../.ssh/id_rsa.pub', __FILE__) 
+				subconfig.vm.provision "file", source: id_rsa_pub_path, destination: "/tmp/id_rsa.pub", run: 'once'
+				subconfig.vm.provision "shell", name: "Add public key", inline: "cat /tmp/id_rsa.pub >> .ssh/authorized_keys", privileged: false, run: 'once'
 			else
 				# this script saves pki information in guest and in the path of the vagrantfile
 				subconfig.vm.provision "shell", name: "Get public ssh keys if id_rsa case no present", path: File.expand_path('../scripts/multimachine/getLastPublicRsa.sh', __FILE__), privileged: false
@@ -181,7 +186,9 @@ Vagrant.configure("2") do |config|
 			end
 		end
 
-		# subconfig.vm.provision "shell", name: "Install ansible", path: File.expand_path('../scripts/ansible/install.sh', __FILE__), run: 'once'
+		subconfig.vm.provision "shell", name: "Install ansible", path: File.expand_path('../scripts/ansible/install.sh', __FILE__), run: 'once'
+
+        subconfig.vm.provision "shell", name: "ansible", inline: "/bin/sh /kubernetes-vagrant/ansible/install.sh", run: 'never'
 	end
 
 	vms.nodes.each do |node_hash|
diff --git a/ansible/.gitignore b/ansible/.gitignore
deleted file mode 100755
index 2eea525..0000000
--- a/ansible/.gitignore
+++ /dev/null
@@ -1 +0,0 @@
-.env
\ No newline at end of file
diff --git a/ansible/ansible.cfg b/ansible/ansible.cfg
index 98014a4..0b5ddb0 100755
--- a/ansible/ansible.cfg
+++ b/ansible/ansible.cfg
@@ -1,5 +1,5 @@
 # https://docs.ansible.com/ansible/latest/reference_appendices/config.html
 [defaults]
 host_key_checking = False
-inventory = {{CWD}}/hosts.yaml  ; This points to the file that lists your hosts
-remote_user = esilab
+inventory = hosts.yaml  ; This is relative to ansible.cfg folder
+remote_user = vagrant
diff --git a/ansible/hosts-from-inventory/install.sh b/ansible/hosts-from-inventory/install.sh
new file mode 100755
index 0000000..d33f58d
--- /dev/null
+++ b/ansible/hosts-from-inventory/install.sh
@@ -0,0 +1,13 @@
+#!/bin/bash
+set -e
+
+# check if docker-compose is available if it is installed exist
+hash docker-compose > /dev/null 2>&1 && exit 0 || true
+echo Provisioning ansible...
+
+SCRIPT_DIR=$(dirname "$0")
+cd $SCRIPT_DIR
+
+export ANSIBLE_CONFIG=$SCRIPT_DIR/ansible.cfg
+# ansible-playbook -u $user site.yaml
+ansible-playbook site.yaml --extra-vars "$1"
\ No newline at end of file
diff --git a/ansible/hosts-from-inventory/site.yaml b/ansible/hosts-from-inventory/site.yaml
new file mode 100755
index 0000000..623fd67
--- /dev/null
+++ b/ansible/hosts-from-inventory/site.yaml
@@ -0,0 +1,25 @@
+- hosts: all
+  pre_tasks:
+    - name: Print all available facts
+      debug:
+        var: groups['all']
+    - name: Print management facts
+      debug:
+        var: hostvars['management']['ip']
+    - name: Check inventory
+      debug:
+        msg: "{{ hostvars[item]['ip'] }} {{item}}"
+      when: hostvars[item]['ip'] is defined
+      with_items:
+        - "{{ groups['all'] }}"
+  tasks:
+    - name: Add the inventory into /etc/hosts
+      become: yes
+      lineinfile:
+        dest: /etc/hosts
+        regexp: '.*{{ item }}$'
+        line: "{{ hostvars[item]['ip'] }} {{item}}"
+        state: present
+      when: hostvars[item]['ip'] is defined
+      with_items:
+        - "{{ groups['all'] }}"
\ No newline at end of file
diff --git a/ansible/hosts.yaml b/ansible/hosts.yaml
index 292422c..14e5a32 100755
--- a/ansible/hosts.yaml
+++ b/ansible/hosts.yaml
@@ -1,4 +1,20 @@
 all:
-  hosts:
-    localhost:
-      ansible_connection: local
+  children:
+    management:
+      hosts:
+        management:
+          ip: 192.168.56.10
+    masters:
+      hosts:
+        kubernetes01:
+          ip: 192.168.56.11
+        kubernetes02:
+          ip: 192.168.56.12
+    workers:
+      hosts:
+        kubernetes03:
+          ip: 192.168.56.13
+        kubernetes04:
+          ip: 192.168.56.14
+        kubernetes05:
+          ip: 192.168.56.15
\ No newline at end of file
diff --git a/ansible/kubernetes/hosts.yaml b/ansible/kubernetes/hosts.yaml
new file mode 100755
index 0000000..292422c
--- /dev/null
+++ b/ansible/kubernetes/hosts.yaml
@@ -0,0 +1,4 @@
+all:
+  hosts:
+    localhost:
+      ansible_connection: local
diff --git a/ansible/kubernetes/install.sh b/ansible/kubernetes/install.sh
new file mode 100755
index 0000000..84dd819
--- /dev/null
+++ b/ansible/kubernetes/install.sh
@@ -0,0 +1,13 @@
+#!/bin/bash
+set -e
+
+# check if docker-compose is available if it is installed exist
+hash docker-compose > /dev/null 2>&1 && exit 0 || true
+echo Provisioning ansible...
+
+SCRIPT_DIR=$(dirname "$0")
+cd $SCRIPT_DIR
+
+export ANSIBLE_CONFIG=$SCRIPT_DIR/ansible.cfg
+
+ansible-playbook $SCRIPT_DIR/hosts-from-inventory/site.yaml
diff --git a/ansible/site.yaml b/ansible/kubernetes/site.yaml
similarity index 100%
rename from ansible/site.yaml
rename to ansible/kubernetes/site.yaml
diff --git a/scripts/ansible/install.sh b/scripts/ansible/install.sh
index e7e4661..c2b65b2 100755
--- a/scripts/ansible/install.sh
+++ b/scripts/ansible/install.sh
@@ -5,33 +5,5 @@ set -e
 hash ansible-playbook > /dev/null 2>&1 && exit 0 || true
 echo Provisioning ansible...
 
-function fileAge
-{
-    if fileMod=$(stat -c %Y -- "$1")
-    then
-        echo $(( $(date +%s) - $fileMod ))
-    else
-        echo 0
-    fi
-}
-
-function updateAptGetRepo
-{
-    LASTUPDATE=$(fileAge /var/lib/apt/periodic/update-success-stamp)
-    echo apt-get update if $LASTUPDATE \< 604800 \(one week\)
-    if [ "$LASTUPDATE" -gt "604800" ] ||[ "$LASTUPDATE" -eq "0" ]; 
-    then
-        echo updating
-        apt-get update
-    fi
-}
-
-updateAptGetRepo
-
-DEBIAN_FRONTEND=noninteractive
-
-echo installing python3-pip
-apt-get install -y python3-pip
-echo updating pip
-python3 -m pip install -U pip
-pip3 install ansible
+sudo dnf install epel-release -y
+sudo dnf install ansible -y
\ No newline at end of file
-- 
GitLab