From 4293df73e636a3bb9097d4cbe80ae8c194862f6b Mon Sep 17 00:00:00 2001 From: jerome Date: Mon, 18 Aug 2025 11:22:14 +0200 Subject: commit initial --- final/cloud-init.yaml | 27 +++++++++++++++++++++++++++ final/debian.tf | 14 ++++++++++++++ final/hosts | 2 ++ final/local_script | 8 ++++++++ final/main.tf | 47 +++++++++++++++++++++++++++++++++++++++++++++++ final/playbook.yml | 8 ++++++++ final/rocky.tf | 15 +++++++++++++++ final/wrapper | 5 +++++ 8 files changed, 126 insertions(+) create mode 100644 final/cloud-init.yaml create mode 100644 final/debian.tf create mode 100644 final/hosts create mode 100755 final/local_script create mode 100644 final/main.tf create mode 100644 final/playbook.yml create mode 100644 final/rocky.tf create mode 100755 final/wrapper (limited to 'final') diff --git a/final/cloud-init.yaml b/final/cloud-init.yaml new file mode 100644 index 0000000..8d6e863 --- /dev/null +++ b/final/cloud-init.yaml @@ -0,0 +1,27 @@ +#cloud-config +users: + - name: jerome + shell: /bin/bash + lock_passwd: false + passwd: "$6$KcUwVgsEi1tnNLfn$2kRWPp7kbZ19vB6J/L46fbulcOOcuw54ttOMXJtfrznlyXDdnepr2.pvYuzOs97tPK0aHr4bab1RQIUGtr8vc/" + sudo: ALL=(ALL) ALL + - name: ansible + shell: /bin/bash + ssh_authorized_keys: "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCZJG/XcIIvW0JOJb6ftEpopS4szOo8dRehASGIOAswPRko6JFMT9QuAvl9YkmzgSorn0KpyQrqAxNTklADocMGPW2BJzKb/1fQyZYKY9bLXxyKaYZKbDZNaUJmW92ThDmUrIWPgjh5BhUxOTwRbDhTsRu/pvulnGw+8yOp7Tz8nUgAoJEZ/7fGkm7AaJPLmF/szQEhL/WSNqTtNdCHNYpQqgRIUZh5zqcb2jXa0pZ7GMnPmoSUMlz1OfAxMOIuziaP3i1J/KHVhXdxj4nrOtUjrUULfqk9vyfKkf7BLYKO3fO3BLR9H5HgeTlaB2aXNuDgRAQposNZ0FEK/VkWj+DQuqjuj9nYo57GbfMfhWr/dTKxTVj3xsbFdThDWtlp7sVI2jguqntwwlmhhexJp1fAYZn92KYkaxGHWLbR0bxLEWVjHXciVW2D12IUZfGWXh5wInoQN1gs1i6NUqgf1uDZhAax5H9G07YySR2fnM9TB5c5apyf7PFCg1kZAnqVHrE= jerome@parrot" + sudo: ALL=(ALL) NOPASSWD:ALL + +package_upgrade: true +packages: + - openssh-server +write_files: + - path: /home/jerome/helloworld + content: | + #!/bin/bash + echo "Hello World !" + permissions: "0755" +runcmd: + - [sed, -i, s/PasswordAuthentication no/PasswordAuthentication yes/, /etc/ssh/sshd_config] + - [systemctl, start, sshd] + #bug rocky9 ??? + - passwd -d ansible + diff --git a/final/debian.tf b/final/debian.tf new file mode 100644 index 0000000..013977f --- /dev/null +++ b/final/debian.tf @@ -0,0 +1,14 @@ +resource "incus_instance" "debian" { + name = "webserver1" + project = "lamp" + image = "images:debian/12/cloud" + wait_for { + type = "ipv4" + } + provisioner "local-exec" { + command = "./local_script ${self.ipv4_address} ${self.name}" + } + depends_on = [ + incus_profile.lamp + ] +} diff --git a/final/hosts b/final/hosts new file mode 100644 index 0000000..7c37320 --- /dev/null +++ b/final/hosts @@ -0,0 +1,2 @@ +webserver1 ansible_host=192.168.122.150 ansible_user=ansible ansible_become=true +webserver2 ansible_host=192.168.122.28 ansible_user=ansible ansible_become=true diff --git a/final/local_script b/final/local_script new file mode 100755 index 0000000..14b6121 --- /dev/null +++ b/final/local_script @@ -0,0 +1,8 @@ +#!/bin/bash +ssh-keygen -f ~/.ssh/known_hosts -R $1 +# on attend que cloud-init ait fini d'installer le serveur SSH +while ! ssh-keyscan -H $1 >> ~/.ssh/known_hosts; do + sleep 1 +done +ansible -u ansible --become -i "$1," -m ping all +echo $2 ansible_host=$1 ansible_user=ansible ansible_become=true >> hosts diff --git a/final/main.tf b/final/main.tf new file mode 100644 index 0000000..8c90bfc --- /dev/null +++ b/final/main.tf @@ -0,0 +1,47 @@ +terraform { + required_providers { + incus = { + source = "lxc/incus" + version = "0.3.1" + } + } +} + +provider "incus" { + # Configuration options +} + +data "template_file" "cloud-init" { + template = file("${path.module}/cloud-init.yaml") +} + +resource "incus_project" "lamp" { + name = "lamp" + description = "terraform - cloud-init - ansible" +} + +resource "incus_profile" "lamp" { + project = "lamp" + name = "default" + config = { + "cloud-init.user-data" = data.template_file.cloud-init.rendered + } + device { + type = "nic" + name = "eth0" + properties = { + nictype = "bridged" + parent = "br0" + } + } + device { + type = "disk" + name = "root" + properties = { + pool = "default" + path = "/" + } + } +} + + diff --git a/final/playbook.yml b/final/playbook.yml new file mode 100644 index 0000000..bf82fef --- /dev/null +++ b/final/playbook.yml @@ -0,0 +1,8 @@ +- hosts: all + tasks: + - name: uptime des serveurs + command: uptime + register: uptime_result + - name: debug + debug: + msg: "uptime de {{ansible_hostname}} : {{uptime_result.stdout}}" diff --git a/final/rocky.tf b/final/rocky.tf new file mode 100644 index 0000000..a608e42 --- /dev/null +++ b/final/rocky.tf @@ -0,0 +1,15 @@ +resource "incus_instance" "rocky" { + name = "webserver2" + project = "lamp" + image = "images:rockylinux/9/cloud" + wait_for { + type = "ipv4" + } + provisioner "local-exec" { + command = "./local_script ${self.ipv4_address} ${self.name}" + } + depends_on = [ + incus_profile.lamp + ] +} + diff --git a/final/wrapper b/final/wrapper new file mode 100755 index 0000000..8047680 --- /dev/null +++ b/final/wrapper @@ -0,0 +1,5 @@ +#!/bin/bash + +tofu apply --auto-approve + +ansible-playbook -i hosts playbook.yml -- cgit v1.2.3