summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjerome <jerome@xlinfo.fr>2025-08-18 11:22:14 +0200
committerjerome <jerome@xlinfo.fr>2025-08-18 11:22:14 +0200
commit4293df73e636a3bb9097d4cbe80ae8c194862f6b (patch)
treec7cbf89deac7fcbc85850938b7c926778999c9bc
downloadincus-4293df73e636a3bb9097d4cbe80ae8c194862f6b.tar.gz
incus-4293df73e636a3bb9097d4cbe80ae8c194862f6b.zip
commit initial
-rw-r--r--.gitignore5
-rw-r--r--cloud-init/config.yaml29
-rw-r--r--final/cloud-init.yaml27
-rw-r--r--final/debian.tf14
-rw-r--r--final/hosts2
-rwxr-xr-xfinal/local_script8
-rw-r--r--final/main.tf47
-rw-r--r--final/playbook.yml8
-rw-r--r--final/rocky.tf15
-rwxr-xr-xfinal/wrapper5
-rw-r--r--tofu/ansible/hosts1
-rwxr-xr-xtofu/ansible/local_script8
-rw-r--r--tofu/ansible/main.tf37
-rw-r--r--tofu/basic/main.tf25
-rw-r--r--tofu/lots/main.tf30
-rw-r--r--tofu/lots/terraform.tfvars3
-rw-r--r--tofu/lots/variables.tf17
17 files changed, 281 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..fd2cee4
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,5 @@
+terraform.tfstate
+terraform.tfstate.backup
+.terraform/
+.terraform.lock.hcl
+.terraform.tfstate.lock.info
diff --git a/cloud-init/config.yaml b/cloud-init/config.yaml
new file mode 100644
index 0000000..6fd28c2
--- /dev/null
+++ b/cloud-init/config.yaml
@@ -0,0 +1,29 @@
+config:
+ cloud-init.user-data: |+
+ #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/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
diff --git a/tofu/ansible/hosts b/tofu/ansible/hosts
new file mode 100644
index 0000000..e5bdaa1
--- /dev/null
+++ b/tofu/ansible/hosts
@@ -0,0 +1 @@
+webserver1 ansible_host=192.168.122.213 ansible_user=ansible ansible_become=true
diff --git a/tofu/ansible/local_script b/tofu/ansible/local_script
new file mode 100755
index 0000000..14b6121
--- /dev/null
+++ b/tofu/ansible/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/tofu/ansible/main.tf b/tofu/ansible/main.tf
new file mode 100644
index 0000000..0bbc448
--- /dev/null
+++ b/tofu/ansible/main.tf
@@ -0,0 +1,37 @@
+terraform {
+ required_providers {
+ incus = {
+ source = "lxc/incus"
+ version = "0.3.1"
+ }
+ }
+}
+
+provider "incus" {
+ # Configuration options
+}
+
+resource "incus_instance" "debian" {
+ name = "webserver1"
+ project = "lab"
+ image = "images:debian/12/cloud"
+ wait_for {
+ type = "ipv4"
+ }
+ provisioner "local-exec" {
+ command = "./local_script ${self.ipv4_address} ${self.name}"
+ }
+}
+
+resource "incus_instance" "rocky" {
+ name = "webserver2"
+ project = "lab"
+ image = "images:rockylinux/9/cloud"
+ wait_for {
+ type = "ipv4"
+ }
+ provisioner "local-exec" {
+ command = "./local_script ${self.ipv4_address} ${self.name}"
+ }
+}
+
diff --git a/tofu/basic/main.tf b/tofu/basic/main.tf
new file mode 100644
index 0000000..7da8687
--- /dev/null
+++ b/tofu/basic/main.tf
@@ -0,0 +1,25 @@
+terraform {
+ required_providers {
+ incus = {
+ source = "lxc/incus"
+ version = "0.3.1"
+ }
+ }
+}
+
+provider "incus" {
+ # Configuration options
+}
+
+resource "incus_instance" "test" {
+ name = "test"
+ image = "images:ubuntu/22.04"
+ project = "Lab"
+ wait_for {
+ type = "ipv4"
+ }
+}
+
+output "instance_ip" {
+ value = incus_instance.test.ipv4_address
+}
diff --git a/tofu/lots/main.tf b/tofu/lots/main.tf
new file mode 100644
index 0000000..7917a03
--- /dev/null
+++ b/tofu/lots/main.tf
@@ -0,0 +1,30 @@
+terraform {
+ required_providers {
+ incus = {
+ source = "lxc/incus"
+ version = "0.3.1"
+ }
+ }
+}
+
+provider "incus" {
+ # Configuration options
+}
+
+resource "incus_instance" "instance" {
+ name = "${var.nom}${count.index + 1}"
+ project = "Lab"
+ image = var.image
+ count = var.nb
+ wait_for {
+ type = "ipv4"
+ }
+}
+
+output "instance_ip" {
+ value = {
+ for instance in incus_instance.instance :
+ instance.name => instance.ipv4_address
+ }
+}
+
diff --git a/tofu/lots/terraform.tfvars b/tofu/lots/terraform.tfvars
new file mode 100644
index 0000000..3f7d342
--- /dev/null
+++ b/tofu/lots/terraform.tfvars
@@ -0,0 +1,3 @@
+nb = 3
+nom = "rocky"
+image = "images:rockylinux/9/cloud"
diff --git a/tofu/lots/variables.tf b/tofu/lots/variables.tf
new file mode 100644
index 0000000..f983e48
--- /dev/null
+++ b/tofu/lots/variables.tf
@@ -0,0 +1,17 @@
+variable "nom" {
+ description = "nom de la machine"
+ type = string
+ default = "debian"
+}
+
+variable "image" {
+ description = "image source"
+ type = string
+ default = "images:debian/12/cloud"
+}
+
+variable "nb" {
+ description = "nombre de machines"
+ type = number
+ default = 2
+}