{"id":32525,"date":"2023-01-23T04:32:56","date_gmt":"2023-01-23T04:32:56","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=32525"},"modified":"2023-01-23T05:05:55","modified_gmt":"2023-01-23T05:05:55","slug":"hashicorp-packer-what-are-the-location-we-can-set-variable","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/hashicorp-packer-what-are-the-location-we-can-set-variable\/","title":{"rendered":"Hashicorp Packer: What are the Location we can set variable"},"content":{"rendered":"\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>There are two kinds of variables in HCL Packer templates: Input variables, sometimes simply called &#8220;variables&#8221;, and Local variables, also known as &#8220;locals&#8221;. Input variables may have defaults, but those defaults can be overridden from the command line or special variable files. Local variables can be thought of as constants, and are not able to be overridden at runtime.<\/p>\n\n\n\n<p>Once a variable is declared in your configuration, you can set it:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Individually, with the&nbsp;<code>-var foo=bar<\/code>&nbsp;command line option.<\/li>\n\n\n\n<li>In variable definitions files, either specified on the command line with the&nbsp;<code>-var-files values.pkrvars.hcl<\/code>&nbsp;or automatically loaded (<code>*.auto.pkrvars.hcl<\/code>).<\/li>\n\n\n\n<li>As environment variables, for example:&nbsp;<code>PKR_VAR_foo=bar<\/code><\/li>\n<\/ul>\n\n\n\n<p>In Hashicorp Packer, variables can be set in several locations:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Command-line:<\/strong> Variables can be passed as command-line arguments to the Packer build command. This is useful for quickly overriding variables without modifying the template.<\/li>\n\n\n\n<li><strong>Templates: <\/strong>Variables can be defined directly in the Packer template file, typically in a &#8220;variables&#8221; section at the top of the file. These variables will be used as the default values when building the image.<\/li>\n\n\n\n<li><strong>Variable files: <\/strong>Variables can be stored in a separate file, in JSON or HashiCorp Configuration Language (HCL) format, and passed to Packer with the -var-file option. This can be useful for storing sensitive information that should not be committed to version control.<\/li>\n\n\n\n<li><strong>Environment variables: <\/strong>Variables can also be set as environment variables, which Packer will automatically detect and use. This can be useful for setting variables in a script or CI\/CD pipeline.<\/li>\n\n\n\n<li><strong>Consul and Vault: <\/strong>Variables can be stored in HashiCorp Consul or Vault and passed to Packer using the -var option.<\/li>\n\n\n\n<li><\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Command-line<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"364\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2023\/01\/image-1-1024x364.png\" alt=\"\" class=\"wp-image-32526\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2023\/01\/image-1-1024x364.png 1024w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2023\/01\/image-1-300x107.png 300w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2023\/01\/image-1-768x273.png 768w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2023\/01\/image-1.png 1141w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Templates<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"838\" height=\"709\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2023\/01\/image-3.png\" alt=\"\" class=\"wp-image-32533\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2023\/01\/image-3.png 838w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2023\/01\/image-3-300x254.png 300w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2023\/01\/image-3-768x650.png 768w\" sizes=\"auto, (max-width: 838px) 100vw, 838px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Variable files<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"838\" height=\"535\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2023\/01\/image-4.png\" alt=\"\" class=\"wp-image-32535\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2023\/01\/image-4.png 838w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2023\/01\/image-4-300x192.png 300w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2023\/01\/image-4-768x490.png 768w\" sizes=\"auto, (max-width: 838px) 100vw, 838px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Environment variables<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2023\/01\/image-5-1024x345.png\" alt=\"\" class=\"wp-image-32536\" width=\"790\" height=\"266\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2023\/01\/image-5-1024x345.png 1024w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2023\/01\/image-5-300x101.png 300w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2023\/01\/image-5-768x259.png 768w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2023\/01\/image-5.png 1167w\" sizes=\"auto, (max-width: 790px) 100vw, 790px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Packer Variable Precedence<\/h2>\n\n\n\n<p>The above mechanisms for setting variables can be used together in any combination.<\/p>\n\n\n\n<p>Packer loads variables in the following order, with later sources taking precedence over earlier ones:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Environment variables (lowest priority)<\/li>\n\n\n\n<li>Any&nbsp;<code>*.auto.pkrvars.hcl<\/code>&nbsp;or&nbsp;<code>*.auto.pkrvars.json<\/code>&nbsp;files, processed in lexical order of their filenames.<\/li>\n\n\n\n<li>Any&nbsp;<code>-var<\/code>&nbsp;and&nbsp;<code>-var-file<\/code>&nbsp;options on the command line, in the order they are provided. (highest priority)<\/li>\n<\/ul>\n\n\n\n<p>If the same variable is assigned multiple values using different mechanisms, Packer uses the&nbsp;<em>last<\/em>&nbsp;value it finds, overriding any previous values. Note that the same variable cannot be assigned multiple values within a single source.<\/p>\n\n\n\n<p>~&gt;&nbsp;<strong>Important:<\/strong>&nbsp;Variables with map and object values behave the same way as other variables: the last value found overrides the previous values.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are two kinds of variables in HCL Packer templates: Input variables, sometimes simply called &#8220;variables&#8221;, and Local variables, also known as &#8220;locals&#8221;. Input variables may have defaults, but those defaults can be overridden from the command line or special variable files. Local variables can be thought of as constants, and are not able to&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","_joinchat":[],"footnotes":""},"categories":[2],"tags":[],"class_list":["post-32525","post","type-post","status-publish","format-standard","hentry","category-uncategorised"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/32525","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=32525"}],"version-history":[{"count":3,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/32525\/revisions"}],"predecessor-version":[{"id":32539,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/32525\/revisions\/32539"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=32525"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=32525"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=32525"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}