my packer template looks as follows
packer {
#plugins
required_plugins {
amazon = {
version = ">= 1.2.8"
source = "github.com/hashicorp/amazon"
}
ansible = {
version = ">= 1.1.1"
source = "github.com/hashicorp/ansible"
}
}
}
locals { timestamp = regex_replace(timestamp(), "[- TZ:]", "") }
#source account details
source "amazon-ebs" "ubuntu" {
ami_name = "learn-packer-linux-aws"
instance_type = "t2.micro"
region = "us-east-1"
source_ami_filter {
filters = {
name = "ubuntu/images/*ubuntu-jammy-22.04-amd64-server-*"
root-device-type = "ebs"
virtualization-type = "hvm"
}
most_recent = true
owners = ["099720109477"]
}
ssh_username = "ubuntu"
}
#windows source block
source "amazon-ebs" "windows" {
ami_name = "packer-windows-demo-${local.timestamp}"
instance_type = "t2.micro"
communicator = "winrm"
region = "us-east-1"
source_ami_filter {
filters = {
name = "Windows_Server-2022-English-Full-Base-2024.02.14"
root-device-type = "ebs"
virtualization-type = "hvm"
}
most_recent = true
owners = ["amazon"]
}
user_data_file = "./bootstrap_win.txt"
winrm_username = "Administrator"
winrm_password = "SuperS3cr3t!!!!"
}
#build section of ubuntu
build {
name = "learn-packer"
sources = [
"source.amazon-ebs.ubuntu"
]
provisioner "ansible" {
playbook_file = "./playbook.yml"
user = "ubuntu"
}
}
#build section of windows
build {
name = "learn-packer-windows"
sources = [
"source.amazon-ebs.windows"
]
provisioner "ansible" {
playbook_file = "./win_playbook.yml"
user = "Administrator"
use_proxy = false
extra_arguments = [
"-e","ansible_winrm_transport=ntlm ansible_winrm_server_cert_validation=ignore",
"-vvvv"
]
}
}
while build workfolw of github pipeline looks like below, the workflow will be triggered when it identifies the commit in main branch, as of now i am triggring build for windows build only
# This is a basic workflow to help you get started with Actions
name: CI
# Controls when the workflow will run
on:
# Triggers the workflow on push or pull request events but only for the "main" branch
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
build:
# The type of runner that the job will run on
runs-on: ubuntu-latest
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v3
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: us-east-1
# Runs a single command using the runners shell
- name: Run a one-line script
run: echo Hello, world!
- name: Packer Init
run: packer init .
# Runs a set of commands using the runners shell
- name: Packer Build - Branches
if: "startsWith(github.ref, 'refs/heads/')"
run: packer build -only=learn-packer-windows.amazon-ebs.windows .
when i triggred the pipeline the following error occurs "msg": "winrm or requests is not installed: No module named 'winrm'"
Ansible works via SSH for linux machines and WinRM for Windows. So for any agent that is going to be configured for Windows, you need to have a listener for
WinRMfirst.Before you use the provisioner
ansibleon the windows machine, you need to use another provisioner (probably powershell) that starts the service (should already be started by default) and creates a listener for the WinRM module/app so that ansible can later connect to it.As taken from https://learn.microsoft.com/en-us/windows/win32/winrm/installation-and-configuration-for-windows-remote-management