XeusNguyen
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee
    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee
  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # DevOps Training Session 12: Cloud - Packer ###### tags: `devops` `reliable` `research` Hello btb again with Xeus-Cooking with DevOps :coffee: and today we will talk about the packer. The tool use for case when you want to reproduce the image on cloud or VM you want for project and storge that in case manifest and you it if you want using the image you want --> Let go [:coffee:](https://docs.google.com/presentation/d/10W5pvmmKtbn6pX3iPAsAwp5U1oNTIcj45n2gQH8Jbfo/edit?usp=sharing) ## Overview ![](https://i.imgur.com/NliAtUI.png) - Packer is a free and open source tool for creating golden images for multiple platforms from a single source configuration - Packer is product from Hashicorp so you can use the same as language for Terraform to working with Packer ## Implement - Scenerio, If you use the linux VM for doing what you want but it will not setup the tool and somewhat you want inside. So you can resolve it with packer. - Packer will use twice language for produce the image is Json and HCL - like terraform. So for easy using packer we will take care both of json and hcl for have flag view with packer - For install packer you can use this article for doing this task [Installer](https://developer.hashicorp.com/packer/tutorials/docker-get-started/get-started-install-cli) - After you got packer, u need to understand the workflow will apply for packer and component inside - First, the component inside include: ![](https://i.imgur.com/NV48K6U.png) - Builder is the important block where you put some kind version of image, resource for place to storing the image, comunicator for doing some job inside the image SSH, WinRM,... - Provisioner: This block for you put the script which you want to doing with image - Post-Processors: This block is the end of packer which store some binary or unicode manifest - quite hardy to understanding - So don't expect something about that. It can be the checksum for validate your Image what ever - Secondly, the workflow of packer ![](https://i.imgur.com/bkupAnw.png) - So go on the script for you want to doing with packer, i will put both of version json and hcl for comparing ``` HCL-verion. variable "os_type" { type = string default = "Linux" } variable "publisher" { type = string default = "Canonical" } variable "offer" { type = string default = "UbuntuServer" } variable "sku" { type = string default = "18.04-LTS" } variable "version" { type = string default = "latest" } variable "render_image_name" { type = string } variable "resource_group_name" { type = string } variable "build_resource_group_name" { type = string } variable "location" { type = string default = "southeastasia" } variable "vm_size" { type = string default = "Standard_B1s" } variable "tags" { type = map(string) default = { managed = "packer" environment = "dev" } } variable "communicator" { type = string default = "ssh" } source "azure-arm" "linux-docker" { use_azure_cli_auth = true os_type = var.os_type image_publisher = var.publisher image_offer = var.offer image_sku = var.sku image_version = var.version managed_image_name = var.render_image_name managed_image_resource_group_name = var.resource_group_name build_resource_group_name = var.build_resource_group_name vm_size = var.vm_size azure_tags = var.tags communicator = var.communicator } build { sources = ["source.azure-arm.linux-docker"] provisioner "shell" { scripts = ["${abspath(path.root)}/script/setup-az.sh", "${abspath(path.root)}/script/setup-docker.sh"] } } ``` ``` { "variables": { "name": "linux-docker", "managed_image_resource_group_name": "", "managed_image_name": "linux-docker", "client_id":"", "subscription_id":"", "os_type": "Linux", "image_publisher": "Canonical", "image_offer": "UbuntuServer", "image_sku": "18.04-LTS", "communicator": "ssh", "managed_by": "packer", "environment": "", "location": "southeastasia", "vm_size": "Standard_B1s" }, "builders": [{ "name": "{{user `name`}}", "type": "azure-arm", "use_azure_cli_auth": true, "managed_image_resource_group_name": "{{user `managed_image_resource_group_name`}}", "managed_image_name": "{{user `managed_image_name`}}", "os_type": "{{user `os_type`}}", "image_publisher": "{{user `image_publisher`}}", "image_offer": "{{user `image_offer`}}", "image_sku": "{{user `image_sku`}}", "communicator": "{{user `communicator`}}", "azure_tags": { "managed_by": "{{user `managed_by`}}", "environment": "{{user `environment`}}" }, "location": "{{user `location`}}", "vm_size": "{{user `vm_size`}}" } ], "provisioners": [{ "type": "shell", "scripts": [ "{{template_dir}}/script/setup-az.sh", "{{template_dir}}/script/setup-docker.sh" ] }] } ``` - So on my vision, i think the both of version have not different between but if you want to do easily to view sucript with not indent to much HCL, is the best but if you want reduce the line you put for script and doing with pipeline, Json is the best choice. - After you apply the packer workflow `init-validate-build` and it complete with your image to deploy on cloud ![](https://i.imgur.com/sPhOEov.png) - So in this task i have receive the mission to run packer on pipeline. So just create a new pipeline for doing packer job ``` ## packer-pipelines.yaml trigger: none pool: name: linuxAgent stages: - stage: setup_packer_and_identity_for_packer jobs: - job: install_packer steps: - task: CmdLine@2 displayName: Install inputs: script: | curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add - sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main" sudo apt-get update && sudo apt-get install packer - job: authentication_packer steps: - task: CmdLine@2 displayName: Authenticate inputs: script: | az login --identity - stage: validate_for_packer dependsOn: [setup_packer_and_identity_for_packer] condition: succeeded('setup_packer_and_identity_for_packer') jobs: - job: validate_for_packer steps: - task: CmdLine@2 displayName: Validate inputs: workingDirectory: "packer/" script: | packer init linux-docker.pkr.hcl packer validate linux-docker.pkr.hcl env: PKR_VAR_render_image_name: $(renderImageName) PKR_VAR_resource_group_name: $(resourceGroup) PKR_VAR_build_resource_group_name: $(buildResourceGroupName) - stage: build_for_packer dependsOn: [validate_for_packer] condition: succeeded('validate_for_packer') jobs: - job: build_for_packer steps: - task: CmdLine@2 displayName: Build inputs: workingDirectory: "packer/" script: | packer build -force linux-docker.pkr.hcl env: PKR_VAR_render_image_name: $(renderImageName) PKR_VAR_resource_group_name: $(resourceGroup) PKR_VAR_build_resource_group_name: $(buildResourceGroupName) ``` - So just put it azure-pipeline, run and you have image - But the next step we will reproduce the VM using the image so go to VM module or VMSS module to config again ``` ## data.tf data "azurerm_image" "main" { name = var.source_image_name resource_group_name = var.resource_group_root_name } ## variables.tf variable "source_image_name" { type = string } ## vmss.tf source_image_id = var.source_image_id ## /dev/main.tf module "vmss" { source = "../modules/vmss" resource_group_name = azurerm_resource_group.main.name resource_group_location = azurerm_resource_group.main.location source_image_id = data.azurerm_image.main.id ssh_public_key_name = data.azurerm_ssh_public_key.main.public_key container_registry_name = data.azurerm_container_registry.main.name user_identity_id = module.iam.user_identity_id subnet_id = module.network.subnet_id application_security_group_ids = module.network.application_security_group_ids load_balancer_backend_address_pool_ids = module.balancer.load_balancer_backend_address_pool_ids storage_account_name = module.storage.storage_account_name storage_container_name = module.storage.storage_container_name storage_blob_name = module.storage.storage_blob_name environment = var.environment tags = var.tags depends_on = [ module.storage ] } ``` - So the infra will change by replace current image by source_id of image create from packer - And so do pipeline like previous session again and you have VMSS or VM with image created by packer ## Conclusion - So this all of session packer, packer is replace me do the job for create a image for purpose like environment and package install inside. Quite easy ! :thinking_face: - So if you meet promblem inside process building image with packer it will be 2 situation: - 1. Using not available syntax from previous version. IDK what version it change but i cost some time to solving this problem - 2. Grant permission agent pipeline because principle of packer is create a new RG and building image, run task via script and compress it into once became Image. ## Ending - Hopefully, you meet something u need in this session and have knowledge for give packer do it what it job. - Reduce time and work like purpose easily --> GG :coffee: ![](https://i.imgur.com/3qZpDVU.png) *anywhere gg* ![](https://i.imgur.com/1bdIaxi.png) ## Reference [Packer for Azure](https://developer.hashicorp.com/packer/plugins/builders/azure/arm) [Packer syntax](https://developer.hashicorp.com/packer/docs/templates/hcl_templates/syntax)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully