Skip to content

Commit 00e2bf3

Browse files
authored
Merge pull request #2 from arsengspeyan/packer_ansible
Packer ansible
2 parents b8a3838 + 7f675cd commit 00e2bf3

File tree

3 files changed

+201
-0
lines changed

3 files changed

+201
-0
lines changed

.github/workflows/build-amii.yml

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
name: Build AMI with Packer and Ansible
2+
3+
on:
4+
workflow_dispatch:
5+
6+
jobs:
7+
build-ami:
8+
runs-on: ubuntu-latest
9+
env:
10+
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
11+
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
12+
#POSTGRES_USER: ${{ secrets.POSTGRES_USER }}
13+
#POSTGRES_PASSWORD: ${{ secrets.POSTGRES_PASSWORD }}
14+
#POSTGRES_DB: ${{ secrets.POSTGRES_DB }}
15+
16+
steps:
17+
- name: Checkout ansible-packer branch
18+
uses: actions/checkout@v2
19+
with:
20+
ref: ansible-packer
21+
22+
- name: Set up Python
23+
uses: actions/setup-python@v2
24+
with:
25+
python-version: '3.11'
26+
27+
- name: Install Ansible
28+
run: |
29+
sudo apt update
30+
sudo apt install -y ansible
31+
32+
- name: Install Latest Packer and Initialize
33+
run: |
34+
wget -O packer.zip https://releases.hashicorp.com/packer/1.11.1/packer_1.11.1_linux_amd64.zip
35+
unzip -o packer.zip -d packer_install
36+
sudo mv packer_install/packer /usr/local/bin/
37+
rm -rf packer.zip packer_install # Clean up extracted files and zip
38+
packer --version # Verify Packer installation
39+
packer init packer/PosgresawsTemplate.pkr.hcl
40+
41+
- name: Validate Packer Template
42+
run: packer validate packer/PosgresawsTemplate.pkr.hcl
43+
44+
- name: Run Packer build and print AMI ID
45+
run: |
46+
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
47+
ami_id=$(awk -F, '$0 ~/artifact,0,id/ {print $6}' packer_output.txt)
48+
echo "AMI_ID=$ami_id" >> $GITHUB_ENV
49+
echo "AMI_ID=$ami_id" # Print AMI ID to console
50+
51+
- name: Save AMI_ID as artifact
52+
uses: actions/upload-artifact@v2
53+
with:
54+
name: packer-output
55+
path: packer_output.txt
56+

ansible/setup_postgresql.yml

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
---
2+
- name: Set up PostgreSQL 14 locally
3+
hosts: localhost
4+
connection: local
5+
become: yes
6+
vars:
7+
db_name: "mydatabase"
8+
postgres_password: "smartes"
9+
10+
11+
tasks:
12+
13+
14+
15+
- name: Add PostgreSQL APT repository key
16+
apt_key:
17+
url: https://www.postgresql.org/media/keys/ACCC4CF8.asc
18+
state: present
19+
20+
- name: Add PostgreSQL APT repository
21+
apt_repository:
22+
repo: deb http://apt.postgresql.org/pub/repos/apt/ {{ ansible_distribution_release }}-pgdg main
23+
state: present
24+
25+
- name: Update APT cache
26+
apt:
27+
update_cache: yes
28+
29+
- name: Install PostgreSQL 14 and related packages
30+
apt:
31+
name:
32+
- postgresql-14
33+
- postgresql-client-14
34+
- postgresql-contrib-14
35+
state: present
36+
37+
- name: Ensure PostgreSQL service is running and enabled
38+
systemd:
39+
name: postgresql@14-main
40+
state: started
41+
enabled: yes
42+
43+
- name: Set PostgreSQL password
44+
become_user: postgres
45+
postgresql_user:
46+
name: postgres
47+
password: "{ postgres_password }"
48+
49+
- name: Update pg_hba.conf to use md5 authentication for all users
50+
community.postgresql.postgresql_pg_hba:
51+
dest: /etc/postgresql/14/main/pg_hba.conf
52+
contype: host
53+
users: all
54+
databases: all
55+
method: md5
56+
address: 0.0.0.0/0
57+
state: present
58+
59+
60+
- name: Update pg_hba.conf to use md5 authentication for local connections
61+
community.postgresql.postgresql_pg_hba:
62+
dest: /etc/postgresql/14/main/pg_hba.conf
63+
contype: local
64+
users: all
65+
databases: all
66+
method: md5
67+
state: present
68+
69+
- name: Reload PostgreSQL to apply changes
70+
systemd:
71+
name: postgresql
72+
state: reloaded
73+
74+
75+
76+
77+
- name: Create a database
78+
community.postgresql.postgresql_db:
79+
name: "{{ db_name }}"
80+
state: present
81+
become: true
82+
become_user: postgres

packer/PosgresawsTemplate.pkr.hcl

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
packer {
2+
required_plugins {
3+
amazon = {
4+
version = ">= 1.3.1"
5+
source = "github.com/hashicorp/amazon"
6+
}
7+
ansible = {
8+
version = "~> 1"
9+
source = "github.com/hashicorp/ansible"
10+
}
11+
12+
}
13+
}
14+
15+
source "amazon-ebs" "example" {
16+
region = var.region
17+
source_ami = var.source_ami
18+
instance_type = var.instance_type
19+
ssh_username = var.ssh_username
20+
ami_name = "packer-postgresql"
21+
access_key = var.aws_access_key
22+
secret_key = var.aws_secret_key
23+
}
24+
25+
build {
26+
sources = ["source.amazon-ebs.example"]
27+
28+
provisioner "ansible" {
29+
playbook_file = "/home/runner/work/Team1-react-rust-postgres/Team1-react-rust-postgres/packer_ansible/ansible/setup_postgresql.yml"
30+
}
31+
}
32+
33+
variable "region" {
34+
type = string
35+
default = "us-west-1"
36+
}
37+
38+
variable "source_ami" {
39+
type = string
40+
default = "ami-0ecaad63ed3668fca"
41+
}
42+
43+
variable "instance_type" {
44+
type = string
45+
default = "t2.micro"
46+
}
47+
48+
variable "ssh_username" {
49+
type = string
50+
default = "ubuntu"
51+
}
52+
53+
variable "aws_access_key" {
54+
type = string
55+
sensitive = true
56+
default = ""
57+
}
58+
59+
variable "aws_secret_key" {
60+
type = string
61+
sensitive = true
62+
default = ""
63+
}

0 commit comments

Comments
 (0)