Skip to content

Packer ansible #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 34 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
bdae3f7
packer and ansible for postgresdb
arsengspeyan Jul 5, 2024
2af70a5
Update PosgresawsTemplate.json
arsengspeyan Jul 5, 2024
76f3f51
Update PosgresawsTemplate.json
arsengspeyan Jul 5, 2024
ab54d28
Update PosgresawsTemplate.json
arsengspeyan Jul 5, 2024
5516eb2
Update PosgresawsTemplate.json
arsengspeyan Jul 5, 2024
a290bce
Update and rename PosgresawsTemplate.json to PosgresawsTemplate.hcl
arsengspeyan Jul 5, 2024
9d2b818
Update and rename PosgresawsTemplate.hcl to PosgresawsTemplate.pkr.hcl
arsengspeyan Jul 5, 2024
8bb842f
Update PosgresawsTemplate.pkr.hcl
arsengspeyan Jul 5, 2024
96b1069
Update PosgresawsTemplate.pkr.hcl
arsengspeyan Jul 5, 2024
9e47e14
Update PosgresawsTemplate.pkr.hcl
arsengspeyan Jul 5, 2024
f49afec
Update PosgresawsTemplate.pkr.hcl
arsengspeyan Jul 5, 2024
2722dd0
Update PosgresawsTemplate.pkr.hcl
arsengspeyan Jul 5, 2024
230eeb4
Update PosgresawsTemplate.pkr.hcl
arsengspeyan Jul 5, 2024
4705fbd
Update PosgresawsTemplate.pkr.hcl
arsengspeyan Jul 5, 2024
0a481a6
Update and rename PosgresawsTemplate.pkr.hcl to PosgresawsTemplate.json
arsengspeyan Jul 5, 2024
1f3c3ea
Rename PosgresawsTemplate.json to PosgresawsTemplate.pkr.json
arsengspeyan Jul 5, 2024
cb225a5
Update PosgresawsTemplate.pkr.json
arsengspeyan Jul 5, 2024
9f38f19
Update PosgresawsTemplate.pkr.json
arsengspeyan Jul 5, 2024
1b7bf56
Update setup_postgresql.yml
arsengspeyan Jul 9, 2024
913c6a8
Update PosgresawsTemplate.pkr.json
arsengspeyan Jul 9, 2024
aa86c72
Rename PosgresawsTemplate.pkr.json to PosgresawsTemplate.pkr.hcl
arsengspeyan Jul 9, 2024
5caa21f
Update PosgresawsTemplate.pkr.hcl
arsengspeyan Jul 9, 2024
394750f
Update PosgresawsTemplate.pkr.hcl
arsengspeyan Jul 9, 2024
c570e81
Update PosgresawsTemplate.pkr.hcl
arsengspeyan Jul 9, 2024
59a7686
Update PosgresawsTemplate.pkr.hcl
arsengspeyan Jul 9, 2024
665b4c8
Add new files:
arsengspeyan Jul 9, 2024
e1c3d75
Rename build-ami.yml to build-amii.yml
arsengspeyan Jul 9, 2024
8140b74
Update build-amii.yml
arsengspeyan Jul 9, 2024
7f383e2
Update PosgresawsTemplate.pkr.hcl
arsengspeyan Jul 9, 2024
e961c46
Update PosgresawsTemplate.pkr.hcl
arsengspeyan Jul 9, 2024
d70fb3d
Update PosgresawsTemplate.pkr.hcl
arsengspeyan Jul 9, 2024
c59f005
Update setup_postgresql.yml
arsengspeyan Jul 9, 2024
e32b3aa
Update setup_postgresql.yml
arsengspeyan Jul 9, 2024
7f675cd
Update setup_postgresql.yml
arsengspeyan Jul 10, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions .github/workflows/build-amii.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: Build AMI with Packer and Ansible

on:
workflow_dispatch:

jobs:
build-ami:
runs-on: ubuntu-latest
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
#POSTGRES_USER: ${{ secrets.POSTGRES_USER }}
#POSTGRES_PASSWORD: ${{ secrets.POSTGRES_PASSWORD }}
#POSTGRES_DB: ${{ secrets.POSTGRES_DB }}

steps:
- name: Checkout ansible-packer branch
uses: actions/checkout@v2
with:
ref: ansible-packer

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.11'

- name: Install Ansible
run: |
sudo apt update
sudo apt install -y ansible

- name: Install Latest Packer and Initialize
run: |
wget -O packer.zip https://releases.hashicorp.com/packer/1.11.1/packer_1.11.1_linux_amd64.zip
unzip -o packer.zip -d packer_install
sudo mv packer_install/packer /usr/local/bin/
rm -rf packer.zip packer_install # Clean up extracted files and zip
packer --version # Verify Packer installation
packer init packer/PosgresawsTemplate.pkr.hcl

- name: Validate Packer Template
run: packer validate packer/PosgresawsTemplate.pkr.hcl

- name: Run Packer build and print AMI ID
run: |
packer build -machine-readable -var aws_access_key=${{ secrets.AWS_ACCESS_KEY_ID }} -var aws_secret_key=${{ secrets.AWS_SECRET_ACCESS_KEY }} packer/PosgresawsTemplate.pkr.hcl | tee packer_output.txt
ami_id=$(awk -F, '$0 ~/artifact,0,id/ {print $6}' packer_output.txt)
echo "AMI_ID=$ami_id" >> $GITHUB_ENV
echo "AMI_ID=$ami_id" # Print AMI ID to console

- name: Save AMI_ID as artifact
uses: actions/upload-artifact@v2
with:
name: packer-output
path: packer_output.txt

82 changes: 82 additions & 0 deletions ansible/setup_postgresql.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
---
- name: Set up PostgreSQL 14 locally
hosts: localhost
connection: local
become: yes
vars:
db_name: "mydatabase"
postgres_password: "smartes"


tasks:



- name: Add PostgreSQL APT repository key
apt_key:
url: https://www.postgresql.org/media/keys/ACCC4CF8.asc
state: present

- name: Add PostgreSQL APT repository
apt_repository:
repo: deb http://apt.postgresql.org/pub/repos/apt/ {{ ansible_distribution_release }}-pgdg main
state: present

- name: Update APT cache
apt:
update_cache: yes

- name: Install PostgreSQL 14 and related packages
apt:
name:
- postgresql-14
- postgresql-client-14
- postgresql-contrib-14
state: present

- name: Ensure PostgreSQL service is running and enabled
systemd:
name: postgresql@14-main
state: started
enabled: yes

- name: Set PostgreSQL password
become_user: postgres
postgresql_user:
name: postgres
password: "{ postgres_password }"

- name: Update pg_hba.conf to use md5 authentication for all users
community.postgresql.postgresql_pg_hba:
dest: /etc/postgresql/14/main/pg_hba.conf
contype: host
users: all
databases: all
method: md5
address: 0.0.0.0/0
state: present


- name: Update pg_hba.conf to use md5 authentication for local connections
community.postgresql.postgresql_pg_hba:
dest: /etc/postgresql/14/main/pg_hba.conf
contype: local
users: all
databases: all
method: md5
state: present

- name: Reload PostgreSQL to apply changes
systemd:
name: postgresql
state: reloaded




- name: Create a database
community.postgresql.postgresql_db:
name: "{{ db_name }}"
state: present
become: true
become_user: postgres
63 changes: 63 additions & 0 deletions packer/PosgresawsTemplate.pkr.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
packer {
required_plugins {
amazon = {
version = ">= 1.3.1"
source = "github.com/hashicorp/amazon"
}
ansible = {
version = "~> 1"
source = "github.com/hashicorp/ansible"
}

}
}

source "amazon-ebs" "example" {
region = var.region
source_ami = var.source_ami
instance_type = var.instance_type
ssh_username = var.ssh_username
ami_name = "packer-postgresql"
access_key = var.aws_access_key
secret_key = var.aws_secret_key
}

build {
sources = ["source.amazon-ebs.example"]

provisioner "ansible" {
playbook_file = "/home/runner/work/Team1-react-rust-postgres/Team1-react-rust-postgres/packer_ansible/ansible/setup_postgresql.yml"
}
}

variable "region" {
type = string
default = "us-west-1"
}

variable "source_ami" {
type = string
default = "ami-0ecaad63ed3668fca"
}

variable "instance_type" {
type = string
default = "t2.micro"
}

variable "ssh_username" {
type = string
default = "ubuntu"
}

variable "aws_access_key" {
type = string
sensitive = true
default = ""
}

variable "aws_secret_key" {
type = string
sensitive = true
default = ""
}
Loading