Skip to content
Snippets Groups Projects
Commit b7130ea6 authored by Benguria Elguezabal, Gorka's avatar Benguria Elguezabal, Gorka
Browse files

adds host to the etc/hosts

parent 3fe8cf26
No related branches found
No related tags found
No related merge requests found
...@@ -148,8 +148,13 @@ Vagrant.configure("2") do |config| ...@@ -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. # 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__) id_rsa_path = File.expand_path('../.ssh/id_rsa', __FILE__)
if File.exists?(id_rsa_path) 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 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 else
# this script saves pki information in guest and in the path of the vagrantfile # 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 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| ...@@ -181,7 +186,9 @@ Vagrant.configure("2") do |config|
end end
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 end
vms.nodes.each do |node_hash| vms.nodes.each do |node_hash|
......
.env
\ No newline at end of file
# https://docs.ansible.com/ansible/latest/reference_appendices/config.html # https://docs.ansible.com/ansible/latest/reference_appendices/config.html
[defaults] [defaults]
host_key_checking = False host_key_checking = False
inventory = {{CWD}}/hosts.yaml ; This points to the file that lists your hosts inventory = hosts.yaml ; This is relative to ansible.cfg folder
remote_user = esilab remote_user = vagrant
#!/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
- 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
all: all:
children:
management:
hosts: hosts:
localhost: management:
ansible_connection: local 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
all:
hosts:
localhost:
ansible_connection: local
#!/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
File moved
...@@ -5,33 +5,5 @@ set -e ...@@ -5,33 +5,5 @@ set -e
hash ansible-playbook > /dev/null 2>&1 && exit 0 || true hash ansible-playbook > /dev/null 2>&1 && exit 0 || true
echo Provisioning ansible... echo Provisioning ansible...
function fileAge sudo dnf install epel-release -y
{ sudo dnf install ansible -y
if fileMod=$(stat -c %Y -- "$1") \ No newline at end of file
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment