add template option for templating the inventory file lines (#9438)

This commit is contained in:
Megan Marsh 2020-06-17 02:05:48 -07:00 committed by GitHub
parent 88c94cc987
commit b5b8f2e308
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 63 additions and 38 deletions

View File

@ -137,6 +137,15 @@ type Config struct {
// inventory directory with `host_vars` `group_vars` that you would like to
// use in the playbook that this provisioner will run.
InventoryDirectory string `mapstructure:"inventory_directory"`
// This template represents the format for the lines added to the temporary
// inventory file that Packer will create to run Ansible against your image.
// The default for recent versions of Ansible is:
// "{{ .HostAlias }} ansible_host={{ .Host }} ansible_user={{ .User }} ansible_port={{ .Port }}\n"
// Available template engines are: This option is a template engine;
// variables available to you include the examples in the default (Host,
// HostAlias, User, Port) as well as any variables available to you via the
// "build" template engine.
InventoryFileTemplate string `mapstructure:"inventory_file_template"`
// The inventory file to use during provisioning.
// When unspecified, Packer will create a temporary inventory file and will
// use the `host_alias`.
@ -207,7 +216,9 @@ func (p *Provisioner) Prepare(raws ...interface{}) error {
Interpolate: true,
InterpolateContext: &p.config.ctx,
InterpolateFilter: &interpolate.RenderFilter{
Exclude: []string{},
Exclude: []string{
"inventory_file_template",
},
},
}, raws...)
if err != nil {
@ -429,14 +440,18 @@ func (p *Provisioner) createInventoryFile() error {
return fmt.Errorf("Error preparing inventory file: %s", err)
}
// If user has defiend their own inventory template, use it.
hostTemplate := p.config.InventoryFileTemplate
if hostTemplate == "" {
// figure out which inventory line template to use
hostTemplate := DefaultSSHInventoryFilev2
hostTemplate = DefaultSSHInventoryFilev2
if p.ansibleMajVersion < 2 {
hostTemplate = DefaultSSHInventoryFilev1
}
if p.config.UseProxy.False() && p.generatedData["ConnType"] == "winrm" {
hostTemplate = DefaultWinRMInventoryFilev2
}
}
// interpolate template to generate host with necessary vars.
ctxData := p.generatedData
@ -449,7 +464,6 @@ func (p *Provisioner) createInventoryFile() error {
p.config.ctx.Data = ctxData
host, err := interpolate.Render(hostTemplate, &p.config.ctx)
if err != nil {
return fmt.Errorf("Error generating inventory file from template: %s", err)
}

View File

@ -31,6 +31,7 @@ type FlatConfig struct {
SkipVersionCheck *bool `mapstructure:"skip_version_check" cty:"skip_version_check" hcl:"skip_version_check"`
UseSFTP *bool `mapstructure:"use_sftp" cty:"use_sftp" hcl:"use_sftp"`
InventoryDirectory *string `mapstructure:"inventory_directory" cty:"inventory_directory" hcl:"inventory_directory"`
InventoryFileTemplate *string `mapstructure:"inventory_file_template" cty:"inventory_file_template" hcl:"inventory_file_template"`
InventoryFile *string `mapstructure:"inventory_file" cty:"inventory_file" hcl:"inventory_file"`
KeepInventoryFile *bool `mapstructure:"keep_inventory_file" cty:"keep_inventory_file" hcl:"keep_inventory_file"`
GalaxyFile *string `mapstructure:"galaxy_file" cty:"galaxy_file" hcl:"galaxy_file"`
@ -74,6 +75,7 @@ func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec {
"skip_version_check": &hcldec.AttrSpec{Name: "skip_version_check", Type: cty.Bool, Required: false},
"use_sftp": &hcldec.AttrSpec{Name: "use_sftp", Type: cty.Bool, Required: false},
"inventory_directory": &hcldec.AttrSpec{Name: "inventory_directory", Type: cty.String, Required: false},
"inventory_file_template": &hcldec.AttrSpec{Name: "inventory_file_template", Type: cty.String, Required: false},
"inventory_file": &hcldec.AttrSpec{Name: "inventory_file", Type: cty.String, Required: false},
"keep_inventory_file": &hcldec.AttrSpec{Name: "keep_inventory_file", Type: cty.Bool, Required: false},
"galaxy_file": &hcldec.AttrSpec{Name: "galaxy_file", Type: cty.String, Required: false},

View File

@ -89,6 +89,15 @@
inventory directory with `host_vars` `group_vars` that you would like to
use in the playbook that this provisioner will run.
- `inventory_file_template` (string) - This template represents the format for the lines added to the temporary
inventory file that Packer will create to run Ansible against your image.
The default for recent versions of Ansible is:
"{{ .HostAlias }} ansible_host={{ .Host }} ansible_user={{ .User }} ansible_port={{ .Port }}\n"
Available template engines are: This option is a template engine;
variables available to you include the examples in the default (Host,
HostAlias, User, Port) as well as any variables available to you via the
"build" template engine.
- `inventory_file` (string) - The inventory file to use during provisioning.
When unspecified, Packer will create a temporary inventory file and will
use the `host_alias`.