diff options
| -rw-r--r-- | .gitignore | 6 | ||||
| -rw-r--r-- | cloud_init.cfg | 28 | ||||
| -rw-r--r-- | libvirt.tf | 79 | ||||
| -rw-r--r-- | main.tf | 13 | ||||
| -rw-r--r-- | playbooks/files/default.conf | 8 | ||||
| -rw-r--r-- | playbooks/hosts | 2 | ||||
| -rw-r--r-- | playbooks/lamp.yml | 3 | ||||
| -rw-r--r-- | playbooks/lamp/.travis.yml | 29 | ||||
| -rw-r--r-- | playbooks/lamp/README.md | 38 | ||||
| -rw-r--r-- | playbooks/lamp/defaults/main.yml | 2 | ||||
| -rw-r--r-- | playbooks/lamp/handlers/main.yml | 7 | ||||
| -rw-r--r-- | playbooks/lamp/meta/main.yml | 53 | ||||
| -rw-r--r-- | playbooks/lamp/tasks/Debian-phpmyadmin.yml | 23 | ||||
| -rw-r--r-- | playbooks/lamp/tasks/RedHat-phpmyadmin.yml | 23 | ||||
| -rw-r--r-- | playbooks/lamp/tasks/clean.yml | 15 | ||||
| -rw-r--r-- | playbooks/lamp/tasks/main.yml | 57 | ||||
| -rw-r--r-- | playbooks/lamp/tasks/test.yml | 19 | ||||
| -rw-r--r-- | playbooks/lamp/templates/index.j2 | 20 | ||||
| -rw-r--r-- | playbooks/lamp/tests/inventory | 2 | ||||
| -rw-r--r-- | playbooks/lamp/tests/test.yml | 5 | ||||
| -rw-r--r-- | playbooks/lamp/vars/Debian.yml | 14 | ||||
| -rw-r--r-- | playbooks/lamp/vars/RedHat.yml | 14 | ||||
| -rw-r--r-- | playbooks/lamp/vars/main.yml | 2 | ||||
| -rw-r--r-- | playbooks/lxc.yml | 62 | ||||
| -rwxr-xr-x | script_local | 11 | ||||
| -rw-r--r-- | terraform.tfvars | 14 | ||||
| -rw-r--r-- | variables.tf | 16 |
27 files changed, 565 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..013e00a --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +terraform.tfstate +terraform.tfstate.backup +.terraform/ +.terraform.lock.hcl +.terraform.tfstate.lock.info + diff --git a/cloud_init.cfg b/cloud_init.cfg new file mode 100644 index 0000000..6ec4701 --- /dev/null +++ b/cloud_init.cfg @@ -0,0 +1,28 @@ +#cloud-config +hostname: ${nom} +manage_etc_hosts: true +fqdn: ${nom} + +users: + - name: ansible + lock_passwd: false + # mkpasswd -m sha-512 + passwd: $6$Yh5Eu0fkQYlk83Fm$wVQBS1yWfKN6ph4QfrrvCiJjOaz7Al0bFBXsDIDrrWXPBOSb/yqTuVYHikgbxKxG8EqCOaVRjL694IRmcjrwQ1 + ssh_authorized_keys: + - ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCZJG/XcIIvW0JOJb6ftEpopS4szOo8dRehASGIOAswPRko6JFMT9QuAvl9YkmzgSorn0KpyQrqAxNTklADocMGPW2BJzKb/1fQyZYKY9bLXxyKaYZKbDZNaUJmW92ThDmUrIWPgjh5BhUxOTwRbDhTsRu/pvulnGw+8yOp7Tz8nUgAoJEZ/7fGkm7AaJPLmF/szQEhL/WSNqTtNdCHNYpQqgRIUZh5zqcb2jXa0pZ7GMnPmoSUMlz1OfAxMOIuziaP3i1J/KHVhXdxj4nrOtUjrUULfqk9vyfKkf7BLYKO3fO3BLR9H5HgeTlaB2aXNuDgRAQposNZ0FEK/VkWj+DQuqjuj9nYo57GbfMfhWr/dTKxTVj3xsbFdThDWtlp7sVI2jguqntwwlmhhexJp1fAYZn92KYkaxGHWLbR0bxLEWVjHXciVW2D12IUZfGWXh5wInoQN1gs1i6NUqgf1uDZhAax5H9G07YySR2fnM9TB5c5apyf7PFCg1kZAnqVHrE= jerome@parrot + sudo: ['ALL=(ALL) NOPASSWD:ALL'] + shell: /bin/bash + + +runcmd: + - cp /etc/skel/.* /home/ansible/ + - chown ansible:ansible /home/ansible/.* + - ${package} + +write_files: + - path: /home/ansible/helloworld + content: | + #!/bin/bash + echo "Hello World !" + permissions: 0755 + diff --git a/libvirt.tf b/libvirt.tf new file mode 100644 index 0000000..64bf161 --- /dev/null +++ b/libvirt.tf @@ -0,0 +1,79 @@ +# Defining VM Volume +resource "libvirt_volume" "base" { + name = "${var.nom}-base" + pool = "default" + source = var.image + format = "qcow2" +} + +resource "libvirt_volume" "image" { + name = "${var.nom}.qcow2" + base_volume_id = libvirt_volume.base.id + pool = "default" + size = 10 * 1024 * 1024 * 1024 +} + +# get user data info +data "template_file" "user_data" { + template = file("${path.module}/cloud_init.cfg") + vars = { + nom = var.nom + package = var.package + } +} + +# Use CloudInit to add the instance +resource "libvirt_cloudinit_disk" "commoninit" { + name = "commoninit.iso" + pool = "default" # List storage pools using virsh pool-list + user_data = data.template_file.user_data.rendered +} + +# Define KVM domain to create +resource "libvirt_domain" "vm_domain" { + name = var.nom + memory = "2048" + vcpu = 2 + qemu_agent = true + + + cpu { + mode = "host-passthrough" + } + + network_interface { + network_name = "default" + #bridge = "bridge0" + wait_for_lease = true + } + + disk { + volume_id = libvirt_volume.image.id + } + + cloudinit = libvirt_cloudinit_disk.commoninit.id + + console { + type = "pty" + target_type = "serial" + target_port = "0" + } + + graphics { + type = "spice" + listen_type = "address" + autoport = true + } + + provisioner "local-exec" { + command = "./script_local ${self.network_interface.0.addresses[0]}" + } +} + +# Output Server IP +# Nécessite qemu-guest-agent sinon bloque la fin de l'installation !!! +output "ip" { + value = libvirt_domain.vm_domain.network_interface.0.addresses +} + + @@ -0,0 +1,13 @@ +terraform { + required_providers { + libvirt = { + source = "dmacvicar/libvirt" + } + } +} + +provider "libvirt" { + ## Configuration options + uri = "qemu:///system" + #uri = "qemu+ssh://root@adresse_ip/system" +} diff --git a/playbooks/files/default.conf b/playbooks/files/default.conf new file mode 100644 index 0000000..bc5449b --- /dev/null +++ b/playbooks/files/default.conf @@ -0,0 +1,8 @@ +lxc.net.0.type = veth +lxc.net.0.link = lxcbr0 +lxc.net.0.flags = up +lxc.net.0.hwaddr = xx:xx:xx:xx:xx:xx + +lxc.apparmor.profile = generated +lxc.apparmor.allow_nesting = 1 +lxc.start.auto = 1 diff --git a/playbooks/hosts b/playbooks/hosts new file mode 100644 index 0000000..c476980 --- /dev/null +++ b/playbooks/hosts @@ -0,0 +1,2 @@ +10.0.3.153 +10.0.3.68
\ No newline at end of file diff --git a/playbooks/lamp.yml b/playbooks/lamp.yml new file mode 100644 index 0000000..bbfb572 --- /dev/null +++ b/playbooks/lamp.yml @@ -0,0 +1,3 @@ +- hosts: all + roles: + - lamp diff --git a/playbooks/lamp/.travis.yml b/playbooks/lamp/.travis.yml new file mode 100644 index 0000000..36bbf62 --- /dev/null +++ b/playbooks/lamp/.travis.yml @@ -0,0 +1,29 @@ +--- +language: python +python: "2.7" + +# Use the new container infrastructure +sudo: false + +# Install ansible +addons: + apt: + packages: + - python-pip + +install: + # Install ansible + - pip install ansible + + # Check ansible version + - ansible --version + + # Create ansible.cfg with correct roles_path + - printf '[defaults]\nroles_path=../' >ansible.cfg + +script: + # Basic role syntax check + - ansible-playbook tests/test.yml -i tests/inventory --syntax-check + +notifications: + webhooks: https://galaxy.ansible.com/api/v1/notifications/
\ No newline at end of file diff --git a/playbooks/lamp/README.md b/playbooks/lamp/README.md new file mode 100644 index 0000000..225dd44 --- /dev/null +++ b/playbooks/lamp/README.md @@ -0,0 +1,38 @@ +Role Name +========= + +A brief description of the role goes here. + +Requirements +------------ + +Any pre-requisites that may not be covered by Ansible itself or the role should be mentioned here. For instance, if the role uses the EC2 module, it may be a good idea to mention in this section that the boto package is required. + +Role Variables +-------------- + +A description of the settable variables for this role should go here, including any variables that are in defaults/main.yml, vars/main.yml, and any variables that can/should be set via parameters to the role. Any variables that are read from other roles and/or the global scope (ie. hostvars, group vars, etc.) should be mentioned here as well. + +Dependencies +------------ + +A list of other roles hosted on Galaxy should go here, plus any details in regards to parameters that may need to be set for other roles, or variables that are used from other roles. + +Example Playbook +---------------- + +Including an example of how to use your role (for instance, with variables passed in as parameters) is always nice for users too: + + - hosts: servers + roles: + - { role: username.rolename, x: 42 } + +License +------- + +BSD + +Author Information +------------------ + +An optional section for the role authors to include contact information, or a website (HTML is not allowed). diff --git a/playbooks/lamp/defaults/main.yml b/playbooks/lamp/defaults/main.yml new file mode 100644 index 0000000..56675e7 --- /dev/null +++ b/playbooks/lamp/defaults/main.yml @@ -0,0 +1,2 @@ +--- +# vars file for lamp diff --git a/playbooks/lamp/handlers/main.yml b/playbooks/lamp/handlers/main.yml new file mode 100644 index 0000000..b583d01 --- /dev/null +++ b/playbooks/lamp/handlers/main.yml @@ -0,0 +1,7 @@ +--- +# handlers file for lamp +- name: restarting_apache + service: + name: '{{apache.service}}' + state: restarted + diff --git a/playbooks/lamp/meta/main.yml b/playbooks/lamp/meta/main.yml new file mode 100644 index 0000000..227ad9c --- /dev/null +++ b/playbooks/lamp/meta/main.yml @@ -0,0 +1,53 @@ +galaxy_info: + author: your name + description: your role description + company: your company (optional) + + # If the issue tracker for your role is not on github, uncomment the + # next line and provide a value + # issue_tracker_url: http://example.com/issue/tracker + + # Choose a valid license ID from https://spdx.org - some suggested licenses: + # - BSD-3-Clause (default) + # - MIT + # - GPL-2.0-or-later + # - GPL-3.0-only + # - Apache-2.0 + # - CC-BY-4.0 + license: license (GPL-2.0-or-later, MIT, etc) + + min_ansible_version: 2.9 + + # If this a Container Enabled role, provide the minimum Ansible Container version. + # min_ansible_container_version: + + # + # Provide a list of supported platforms, and for each platform a list of versions. + # If you don't wish to enumerate all versions for a particular platform, use 'all'. + # To view available platforms and versions (or releases), visit: + # https://galaxy.ansible.com/api/v1/platforms/ + # + # platforms: + # - name: Fedora + # versions: + # - all + # - 25 + # - name: SomePlatform + # versions: + # - all + # - 1.0 + # - 7 + # - 99.99 + + galaxy_tags: [] + # List tags for your role here, one per line. A tag is a keyword that describes + # and categorizes the role. Users find roles by searching for tags. Be sure to + # remove the '[]' above, if you add tags to this list. + # + # NOTE: A tag is limited to a single word comprised of alphanumeric characters. + # Maximum 20 tags per role. + +dependencies: [] + # List your role dependencies here, one per line. Be sure to remove the '[]' above, + # if you add dependencies to this list. +
\ No newline at end of file diff --git a/playbooks/lamp/tasks/Debian-phpmyadmin.yml b/playbooks/lamp/tasks/Debian-phpmyadmin.yml new file mode 100644 index 0000000..8bfb478 --- /dev/null +++ b/playbooks/lamp/tasks/Debian-phpmyadmin.yml @@ -0,0 +1,23 @@ +--- +- name: configuration debconf + block: + - debconf: + name: phpmyadmin + question: 'phpmyadmin/reconfigure-webserver' + value: 'apache2' + vtype: select + - debconf: + name: phpmyadmin + question: 'phpmyadmin/mysql/admin-pass' + value: '(password omitted)' + vtype: string + - debconf: + name: phpmyadmin + question: 'phpmyadmin/dbconfig-install' + value : 'true' + vtype: boolean + +- name: installation phpmyadmin + package: + name: phpmyadmin + state: present diff --git a/playbooks/lamp/tasks/RedHat-phpmyadmin.yml b/playbooks/lamp/tasks/RedHat-phpmyadmin.yml new file mode 100644 index 0000000..569ddd2 --- /dev/null +++ b/playbooks/lamp/tasks/RedHat-phpmyadmin.yml @@ -0,0 +1,23 @@ +--- +- name: installation epel-release + package: + name: epel-release + state: present + +- name: installation phpmyadmin + package: + name: phpmyadmin + state: present + +- name: phpmyadmin.conf + replace: + path: /etc/httpd/conf.d/phpMyAdmin.conf + regexp: "Require local" + replace: "Require all granted" + after: "<Directory /usr/share/phpMyAdmin/>" + before: "</Directory>" + notify: restarting_apache + +- name: database + shell: mysql < /usr/share/phpMyAdmin/sql/create_tables.sql + diff --git a/playbooks/lamp/tasks/clean.yml b/playbooks/lamp/tasks/clean.yml new file mode 100644 index 0000000..7e3571e --- /dev/null +++ b/playbooks/lamp/tasks/clean.yml @@ -0,0 +1,15 @@ +--- +- name: supprimer la base de données test + community.mysql.mysql_db: + name: toto + state: absent + login_unix_socket: '{{mariadb.socket}}' + +- name: supprimer l'utilisateur test + community.mysql.mysql_user: + name: toto + password: secret + priv: 'toto.*:ALL' + state: absent + login_unix_socket: '{{mariadb.socket}}' + diff --git a/playbooks/lamp/tasks/main.yml b/playbooks/lamp/tasks/main.yml new file mode 100644 index 0000000..17cf4d8 --- /dev/null +++ b/playbooks/lamp/tasks/main.yml @@ -0,0 +1,57 @@ +--- +# tasks file for lamp +- include_vars: '{{ansible_os_family}}.yml' + tags: + - test + - clean + +- name: installation de lamp + package: + update_cache: true + name: "{{item}}" + state: present + with_items: '{{lamp_packages}}' + +- name: activation des services sur Redhat + service: + state: started + name: '{{item}}' + with_items: + - httpd + - mariadb + when: ansible_os_family == 'RedHat' + +- name : activation du mod_rewrite d'apache sur Debian + apache2_module: + state: present + name: rewrite + when: ansible_os_family == 'Debian' + notify: + - restarting_apache + +- name: mise en place de index.html + template: + src: templates/index.j2 + dest: /var/www/html/index.html + owner: "{{apache.user}}" + group: "{{apache.user}}" + mode: 0644 + +- name: installation de phpmyadmin + include_tasks: '{{ansible_os_family}}-phpmyadmin.yml' + vars: + - apache: '{{apache}}' + tags: phpmyadmin + +- name: test user + import_tasks: test.yml + tags: + - never + - test + +- name: clean user + import_tasks: clean.yml + tags: + - never + - clean + diff --git a/playbooks/lamp/tasks/test.yml b/playbooks/lamp/tasks/test.yml new file mode 100644 index 0000000..c3d9c32 --- /dev/null +++ b/playbooks/lamp/tasks/test.yml @@ -0,0 +1,19 @@ +--- +- name: install dépendances python + package: + name: '{{ mariadb.pythonlib }}' + state: present + +- name: Créer la base de données test + community.mysql.mysql_db: + name: toto + state: present + login_unix_socket: '{{mariadb.socket}}' + +- name: Créer un utilisateur test + community.mysql.mysql_user: + name: toto + password: secret + priv: 'toto.*:ALL' + state: present + login_unix_socket: '{{mariadb.socket}}' diff --git a/playbooks/lamp/templates/index.j2 b/playbooks/lamp/templates/index.j2 new file mode 100644 index 0000000..29619c2 --- /dev/null +++ b/playbooks/lamp/templates/index.j2 @@ -0,0 +1,20 @@ +<!DOCTYPE html> +<html> +<head> +<title>{{ansible_distribution}} Apache Web Server</title> +<meta charset="utf-8" /> +</head> +<body> +<h1>{{ansible_distribution}} Apache Web Server</h1> +<h2>Site en Constuction</h2> +{% if lamp_packages is defined %} +<h3>Packages :</h3> +<ul> +{% for item in lamp_packages %} +<li>{{item}}</li> +{% endfor %} +</ul> +{% endif %} +<p>Généré par {{ ansible_distribution }} {{ ansible_distribution_version }}</p> +</body> +</html> diff --git a/playbooks/lamp/tests/inventory b/playbooks/lamp/tests/inventory new file mode 100644 index 0000000..878877b --- /dev/null +++ b/playbooks/lamp/tests/inventory @@ -0,0 +1,2 @@ +localhost + diff --git a/playbooks/lamp/tests/test.yml b/playbooks/lamp/tests/test.yml new file mode 100644 index 0000000..10107f3 --- /dev/null +++ b/playbooks/lamp/tests/test.yml @@ -0,0 +1,5 @@ +--- +- hosts: localhost + remote_user: root + roles: + - lamp
\ No newline at end of file diff --git a/playbooks/lamp/vars/Debian.yml b/playbooks/lamp/vars/Debian.yml new file mode 100644 index 0000000..1c7659f --- /dev/null +++ b/playbooks/lamp/vars/Debian.yml @@ -0,0 +1,14 @@ +lamp_packages: + - apache2 + - mariadb-server + - php + - php-mysql + +apache: + service: apache2 + user: www-data + +mariadb: + socket: /var/run/mysqld/mysqld.sock + pythonlib: python3-pymysql + diff --git a/playbooks/lamp/vars/RedHat.yml b/playbooks/lamp/vars/RedHat.yml new file mode 100644 index 0000000..b8033ee --- /dev/null +++ b/playbooks/lamp/vars/RedHat.yml @@ -0,0 +1,14 @@ +lamp_packages: + - httpd + - mariadb-server + - php + - php-mysqlnd + +apache: + service: httpd + user: apache + +mariadb: + socket: /var/lib/mysql/mysql.sock + pythonlib: python3-PyMySQL + diff --git a/playbooks/lamp/vars/main.yml b/playbooks/lamp/vars/main.yml new file mode 100644 index 0000000..56675e7 --- /dev/null +++ b/playbooks/lamp/vars/main.yml @@ -0,0 +1,2 @@ +--- +# vars file for lamp diff --git a/playbooks/lxc.yml b/playbooks/lxc.yml new file mode 100644 index 0000000..8c40007 --- /dev/null +++ b/playbooks/lxc.yml @@ -0,0 +1,62 @@ +--- +- hosts: all + tasks: + - name: install lxc + package: + name: '{{item}}' + state: present + update_cache: yes + with_items: + - lxc + - python3-lxc + + - name: Configuration de lxc + copy: + src: default.conf + dest: /etc/lxc/default.conf + + - name: Creation d'un container rocky + community.general.lxc_container: + name: rocky-lxc + template: download + template_options: --dist rockylinux --release 9 --arch amd64 + state: started + container_command: | + sleep 5 + dnf install -y openssh-server python3 + systemctl enable --now sshd + + + - name: Creation d'un container debian + community.general.lxc_container: + name: debian-lxc + template: download + template_options: --dist debian --release bookworm --arch amd64 + state: started + container_command: | + sleep 5 + apt install -y openssh-server python3 + systemctl enable --now ssh + + - name: copie des clefs ssh + copy: + src: "~/.ssh/id_rsa.pub" + dest: "{{item}}" + mode: "0600" + with_items: + - /var/lib/lxc/debian-lxc/rootfs/root/.ssh/authorized_keys + - /var/lib/lxc/rocky-lxc/rootfs/root/.ssh/authorized_keys + + - name: recuperation ip rocky + shell: "lxc-info -iH rocky-lxc" + register: ip_rocky + + - name: recuperation ip debian + shell: "lxc-info -iH debian-lxc" + register: ip_debian + + - name: inventaire + remote_user: "jerome" + become: no + local_action: copy content="{{ip_rocky.stdout}}\n{{ip_debian.stdout}}" dest="hosts" + diff --git a/script_local b/script_local new file mode 100755 index 0000000..d344d5f --- /dev/null +++ b/script_local @@ -0,0 +1,11 @@ +#!/bin/bash +ssh-keygen -f ~/.ssh/known_hosts -R $1 +ssh-keyscan -H $1 >> ~/.ssh/known_hosts +ansible-playbook -u ansible --become -i "$1," playbooks/lxc.yml +sudo ip route del 10.0.3.0/24 +sudo ip route add 10.0.3.0/24 via $1 +ssh-keyscan -H -f playbooks/hosts >> ~/.ssh/known_hosts +ansible-playbook -u root -i playbooks/hosts playbooks/lamp.yml + + + diff --git a/terraform.tfvars b/terraform.tfvars new file mode 100644 index 0000000..950a326 --- /dev/null +++ b/terraform.tfvars @@ -0,0 +1,14 @@ +# Assignation des variables + +########## machine rocky ########## +#nom = "rocky" +#image = "/home/jerome/Téléchargements/isos/Rocky-9-GenericCloud-Base.latest.x86_64.qcow2" +#image = "https://download.rockylinux.org/pub/rocky/9/images/x86_64/Rocky-9-GenericCloud-Base.latest.x86_64.qcow2" +#package = "# rien à faire : qemu-guest-agent pré-installé" + +########## machine debian ########## +nom = "debian" +image = "/home/jerome/Téléchargements/isos/debian-12-generic-amd64.qcow2" +#image = "https://cloud.debian.org/images/cloud/bookworm/latest/debian-12-generic-amd64.qcow2" +package = "apt update && apt install -y qemu-guest-agent && systemctl start qemu-guest-agent" + diff --git a/variables.tf b/variables.tf new file mode 100644 index 0000000..0dbb12f --- /dev/null +++ b/variables.tf @@ -0,0 +1,16 @@ +# définition des variables + +variable "nom" { + description = "nom distribution" + type = string +} + +variable "image" { + description = "source de l'image disque" + type = string +} + +variable "package" { + description = "nom du package manager (dnf/apt) pour installer qemu-guest-agent le cas échéant)" + type = string +} |
