Skip to content

Commit

Permalink
Merge pull request #6 from robsonsnt/feat/provisioner_ansible
Browse files Browse the repository at this point in the history
Feat/provisioner ansible
  • Loading branch information
robsonsnt authored Oct 10, 2024
2 parents 109a014 + b4fa070 commit ea3cebd
Show file tree
Hide file tree
Showing 14 changed files with 564 additions and 14 deletions.
77 changes: 65 additions & 12 deletions .github/workflows/build_template.yml
Original file line number Diff line number Diff line change
@@ -1,32 +1,40 @@
name: Build Template Oracle Linux 8
name: Build Templates

on:
pull_request:
branches:
- main
workflow_dispatch:
inputs:
template_name:
description: 'Template to Build'
required: true
type: choice
options:
- oracle_linux
- rocky_linux

jobs:
run_scripts:
build_oracle_linux:
if: ${{ github.event.inputs.template_name == 'oracle_linux' }}
runs-on:
- self-hosted
- packer

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Remove Template

- name: Remove Oracle Linux Template
if: ${{ github.event.inputs.template_name == 'oracle_linux' }}
run: |
pwd
ansible-playbook -vv oracle_linux_8/ansible/remove_template.yaml \
-e "api_user=${{ vars.PROXMOX_API_USER }}" \
-e "api_token_id=${{ vars.PROXMOX_TOKEN_ID }}" \
-e "api_token_secret=${{ secrets.PROXMOX_TOKEN_TESTE }}" \
-e "proxmox_host=${{ secrets.PROXMOX_API_HOST }}" \
-e "vmid=${{ vars.PROXMOX_VM_ID }}" \
-e "proxmox_node=${{ vars.PROXMOX_NODE }}"
-e "proxmox_node=${{ vars.PROXMOX_NODE }}"
- name: Init Template
- name: Init Oracle Linux Template
if: ${{ github.event.inputs.template_name == 'oracle_linux' }}
run:
packer init -upgrade oracle_linux_8/
env:
Expand All @@ -37,8 +45,9 @@ jobs:
PKR_VAR_proxmox_storage_pool: ${{ vars.PROXMOX_STORAGE_POOL }}
PKR_VAR_ssh_username: ${{ vars.SSH_USERNAME }}
PKR_VAR_ssh_password: ${{ secrets.SSH_PASSWORD }}

- name: Build Template

- name: Build Oracle Linux Template
if: ${{ github.event.inputs.template_name == 'oracle_linux' }}
run:
packer build oracle_linux_8/
env:
Expand All @@ -49,3 +58,47 @@ jobs:
PKR_VAR_proxmox_storage_pool: ${{ vars.PROXMOX_STORAGE_POOL }}
PKR_VAR_ssh_username: ${{ vars.SSH_USERNAME }}
PKR_VAR_ssh_password: ${{ secrets.SSH_PASSWORD }}

build_rocky_linux:
if: ${{ github.event.inputs.template_name == 'rocky_linux' }}
runs-on:
- self-hosted
- packer

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Remove Rocky Linux Template
run: |
ansible-playbook -vv rocky_linux_9/ansible/remove_template.yaml \
-e "api_user=${{ vars.PROXMOX_API_USER }}" \
-e "api_token_id=${{ vars.PROXMOX_TOKEN_ID }}" \
-e "api_token_secret=${{ secrets.PROXMOX_TOKEN_TESTE }}" \
-e "proxmox_host=${{ secrets.PROXMOX_API_HOST }}" \
-e "vmid=500" \
-e "proxmox_node=${{ vars.PROXMOX_NODE }}"
- name: Init Rocky Linux Template
run:
packer init -upgrade rocky_linux_9/
env:
PKR_VAR_proxmox_url: ${{ secrets.PROXMOX_URL }}
PKR_VAR_proxmox_username: ${{ secrets.PROXMOX_USERNAME }}
PKR_VAR_proxmox_token: ${{ secrets.PROXMOX_TOKEN }}
PKR_VAR_proxmox_node: ${{ vars.PROXMOX_NODE }}
PKR_VAR_proxmox_storage_pool: ${{ vars.PROXMOX_STORAGE_POOL }}
PKR_VAR_ssh_username: ${{ vars.SSH_USERNAME }}
PKR_VAR_ssh_password: ${{ secrets.SSH_PASSWORD }}

- name: Build Rocky Linux Template
run:
packer build rocky_linux_9/
env:
PKR_VAR_proxmox_url: ${{ secrets.PROXMOX_URL }}
PKR_VAR_proxmox_username: ${{ secrets.PROXMOX_USERNAME }}
PKR_VAR_proxmox_token: ${{ secrets.PROXMOX_TOKEN }}
PKR_VAR_proxmox_node: ${{ vars.PROXMOX_NODE }}
PKR_VAR_proxmox_storage_pool: ${{ vars.PROXMOX_STORAGE_POOL }}
PKR_VAR_ssh_username: ${{ vars.SSH_USERNAME }}
PKR_VAR_ssh_password: ${{ secrets.SSH_PASSWORD }}
44 changes: 43 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,43 @@
# proxmox-packer
# Proxmox Packer

Este projeto é usado para criar imagens de máquinas virtuais (VMs) para o Proxmox usando o Packer. As imagens criadas são compatíveis com o cloud-init, o que permite que você configure VMs no momento da criação usando arquivos de configuração do cloud-init.

## Pré-requisitos

- [Packer](https://www.packer.io/downloads)
- [Ansible](https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html)
- [Proxmox](https://www.proxmox.com/en/downloads)

Antes de executar o Packer, você precisará configurar algumas variáveis no arquivo `variables.pkrvars.hcl`. Este arquivo contém várias variáveis que o Packer usa para criar a imagem. As variáveis que você precisa configurar incluem:

- `proxmox_url`: O URL do seu servidor Proxmox.
- `proxmox_user`: O nome de usuário para se conectar ao Proxmox.
- `proxmox_password`: A senha para se conectar ao Proxmox.
- `template_name`: O nome do template que será criado.


## Como usar

1. Clone este repositório para o seu local de trabalho.
2. Navegue até o diretório do projeto.
3. Execute o playbook Ansible para verificar e remover o template existente (se necessário):

```bash
ansible-playbook playbook.yml
```

4. Execute o Packer para criar a nova imagem:

```bash
packer build packer.pkr.hcl
```

5. No Proxmox, você pode agora criar uma nova VM usando a imagem criada. Durante a criação da VM, você pode fornecer um arquivo de configuração do cloud-init para configurar a VM.

## Contribuindo

Contribuições para este projeto são bem-vindas. Por favor, abra um problema ou uma solicitação de pull.

## Licença

Este projeto está licenciado sob a licença MIT. Veja o arquivo LICENSE para mais detalhes.
14 changes: 14 additions & 0 deletions ansible.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[defaults]
timeout = 60
gathering = smart
inventory = ansible/inventory/
library = ansible/library
roles_path = ansible/roles
host_key_checking = False
retry_files_enabled = False
callback_whitelist = profile_tasks, timer
deprecation_warnings = False

[ssh_connection]
pipelining = False
ssh_args = -o UserKnownHostsFile=/dev/null -o ControlMaster=auto -o ControlPersist=60m
14 changes: 14 additions & 0 deletions oracle_linux_8/ansible/ansible.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[defaults]
timeout = 60
gathering = smart
inventory = ansible/inventory/
library = ansible/library
roles_path = ansible/roles
host_key_checking = False
retry_files_enabled = False
callback_whitelist = profile_tasks, timer
deprecation_warnings = False

[ssh_connection]
pipelining = False
ssh_args = -o UserKnownHostsFile=/dev/null -o ControlMaster=auto -o ControlPersist=60m
10 changes: 10 additions & 0 deletions oracle_linux_8/ansible/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@
become: true

tasks:
- name: Configure Repo EPEL for Oracle Linux 8
yum_repository:
name: EPEL-OL8
description: Subscription Manager for Oracle Linux 8
baseurl: "https://download.copr.fedorainfracloud.org/results/dmann/subscription-manager/epel-8-x86_64/"
enabled: yes
gpgcheck: 0
when: ansible_distribution == "OracleLinux" and ansible_distribution_major_version == "8"

- name: Install base packages
ansible.builtin.yum:
name: "{{ item }}"
Expand All @@ -15,3 +24,4 @@
- vim
- net-tools
- telnet
- python38
1 change: 1 addition & 0 deletions oracle_linux_8/packer.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ source "proxmox-iso" "autogenerated_1" {
disk_size = var.vm_disk_size
storage_pool = var.proxmox_storage_pool
type = var.vm_disk_type
format = var.disk_format
}
cloud_init = var.cloud_init
cloud_init_storage_pool = var.proxmox_storage_pool
Expand Down
7 changes: 6 additions & 1 deletion oracle_linux_8/variables.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ variable "ssh_password" {

variable "ol8_image" {
type = string
default = "OracleLinux-R8-U9-x86_64-dvd.iso"
default = "OracleLinux-R8-U10-x86_64-dvd.iso"
}

variable "proxmox_iso_pool" {
Expand Down Expand Up @@ -143,6 +143,11 @@ variable "vm_scsi_controller" {
default = "virtio-scsi-single"
}

variable "disk_format" {
type = string
default = "raw"
}

variable "vm_network_adapters_bridge" {
type = string
default = "vmbr0"
Expand Down
14 changes: 14 additions & 0 deletions rocky_linux_9/ansible/ansible.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[defaults]
timeout = 60
gathering = smart
inventory = ansible/inventory/
library = ansible/library
roles_path = ansible/roles
host_key_checking = False
retry_files_enabled = False
callback_whitelist = profile_tasks, timer
deprecation_warnings = False

[ssh_connection]
pipelining = False
ssh_args = -o UserKnownHostsFile=/dev/null -o ControlMaster=auto -o ControlPersist=60m
22 changes: 22 additions & 0 deletions rocky_linux_9/ansible/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
- name: Base linux configuration for packer images
hosts: linux
become: true

tasks:
- name: Install base packages
ansible.builtin.yum:
name: "{{ item }}"
state: present
with_items:
- curl
- wget
- unzip
- git
- vim
- net-tools
- telnet

# - name: System update
# ansible.builtin.yum:
# name: "*"
# state: present
27 changes: 27 additions & 0 deletions rocky_linux_9/ansible/remove_template.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
- name: Remove Template
hosts: localhost
connection: local
tasks:
- name: Get template informations
community.general.proxmox_kvm:
api_user: "{{ api_user }}"
api_token_id: "{{ api_token_id }}"
api_token_secret: "{{ api_token_secret }}"
api_host: "{{ proxmox_host }}"
vmid: "{{ vmid }}"
node: "{{ proxmox_node }}"
state: current
register: vm_info
ignore_errors: true

- name: Remove template especific ID
community.general.proxmox_kvm:
api_user: "{{ api_user }}"
api_token_id: "{{ api_token_id }}"
api_token_secret: "{{ api_token_secret }}"
api_host: "{{ proxmox_host }}"
vmid: "{{ vmid }}"
node: "{{ proxmox_node }}"
state: absent
when: vm_info.vmid is defined
12 changes: 12 additions & 0 deletions rocky_linux_9/config.pkr.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
packer {
required_plugins {
proxmox = {
version = ">= 1.1.8"
source = "github.com/hashicorp/proxmox"
}
ansible = {
version = ">= 1.1.1"
source = "github.com/hashicorp/ansible"
}
}
}
Loading

0 comments on commit ea3cebd

Please sign in to comment.