我的打包模板如下所示
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"
]
}
}
虽然 github 管道的构建工作流程如下所示,但当识别到主分支中的提交时,工作流程将被触发,到目前为止,我只为 Windows 构建触发构建
# 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 .
当我触发管道时,会发生以下错误 "msg": "未安装 winrm 或 requests: 没有名为 'winrm' 的模块"
Ansible 对于 Linux 机器通过 SSH 工作,对于 Windows 机器通过 WinRM 工作。 因此,对于要为 Windows 配置的任何代理,您首先需要有一个
WinRM
的侦听器。
在 Windows 机器上使用配置程序
ansible
之前,您需要使用另一个配置程序(可能是 powershell)来启动服务(默认情况下应该已经启动)并为 WinRM 模块/应用程序创建一个侦听器,以便 ansible 可以稍后连接到它。
- WinRM 服务在 Windows Server 2008 及更高版本上自动启动。在早期版本的 Windows(客户端或服务器)上,您需要手动启动该服务。
- 默认情况下,未配置 WinRM 侦听器。即使 WinRM 服务正在运行,也无法接收或发送请求数据的 WS-Management 协议消息。 Internet 连接防火墙 (ICF) 阻止对端口的访问。