diff --git a/Vagrantfile b/Vagrantfile index ff9bbca2b50ae329e9653f406a47acb94684d187..0edbedee6496dac651f4e674f27d1555784a1e0b 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 2eea525d885d5148108f6f3a9a8613863f783d36..0000000000000000000000000000000000000000 --- 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 98014a42b2f60630e00f8b3175b4f87b6269d496..0b5ddb022944c58a58530ddc022eb9818a5df229 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 0000000000000000000000000000000000000000..d33f58d010345e0f809437f26f7d63125a6e32e8 --- /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 0000000000000000000000000000000000000000..623fd67b393bf9a57938f593ed511e0f996fa87e --- /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 292422c0778915deec7ed9a203a34c6fcd2caadc..14e5a3204311aa8d7b455f457fa84348e2c6135e 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 0000000000000000000000000000000000000000..292422c0778915deec7ed9a203a34c6fcd2caadc --- /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 0000000000000000000000000000000000000000..84dd8193f07220e651557bd131ab99fedfd4ae98 --- /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 e7e4661f7cb54aa7891ac058389f85ceb34dac73..c2b65b2f82a8f2db30d6d61741b6ac146add1ed1 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