From 999f2da499382132fee47a88247b6883d80554f3 Mon Sep 17 00:00:00 2001 From: Rickard von Essen Date: Sat, 22 Apr 2017 17:24:37 +0200 Subject: [PATCH] ansible-local/ansible: Add extra-vars packer_* Added: - `packer_build_name` - `packer_builder_type` - `packer_http_addr` - `ansible-local` only Closes: #4820 --- provisioner/ansible-local/provisioner.go | 5 +++-- provisioner/ansible/provisioner.go | 6 ++++-- .../docs/provisioners/ansible-local.html.md | 21 +++++++++++++++++++ .../source/docs/provisioners/ansible.html.md | 16 ++++++++++++++ 4 files changed, 44 insertions(+), 4 deletions(-) diff --git a/provisioner/ansible-local/provisioner.go b/provisioner/ansible-local/provisioner.go index 88138e67f..c3484381a 100644 --- a/provisioner/ansible-local/provisioner.go +++ b/provisioner/ansible-local/provisioner.go @@ -294,9 +294,10 @@ func (p *Provisioner) executeAnsible(ui packer.Ui, comm packer.Communicator) err playbook := filepath.ToSlash(filepath.Join(p.config.StagingDir, filepath.Base(p.config.PlaybookFile))) inventory := filepath.ToSlash(filepath.Join(p.config.StagingDir, filepath.Base(p.config.InventoryFile))) - extraArgs := "" + extraArgs := fmt.Sprintf(" --extra-vars \"packer_build_name=%s packer_builder_type=%s packer_http_addr=%s\" ", + p.config.PackerBuildName, p.config.PackerBuilderType, common.GetHTTPAddr()) if len(p.config.ExtraArguments) > 0 { - extraArgs = " " + strings.Join(p.config.ExtraArguments, " ") + extraArgs = extraArgs + strings.Join(p.config.ExtraArguments, " ") } // Fetch external dependencies diff --git a/provisioner/ansible/provisioner.go b/provisioner/ansible/provisioner.go index c6e055e01..f64a58fb9 100644 --- a/provisioner/ansible/provisioner.go +++ b/provisioner/ansible/provisioner.go @@ -305,7 +305,9 @@ func (p *Provisioner) executeAnsible(ui packer.Ui, comm packer.Communicator, pri inventory := p.config.inventoryFile var envvars []string - args := []string{playbook, "-i", inventory} + args := []string{"--extra-vars", fmt.Sprintf("packer_build_name=%s packer_builder_type=%s", + p.config.PackerBuildName, p.config.PackerBuilderType), + "-i", inventory, playbook} if len(privKeyFile) > 0 { args = append(args, "--private-key", privKeyFile) } @@ -354,7 +356,7 @@ func (p *Provisioner) executeAnsible(ui packer.Ui, comm packer.Communicator, pri go repeat(stdout) go repeat(stderr) - log.Printf("Executing Ansible: %s", strings.Join(cmd.Args, " ")) + ui.Say(fmt.Sprintf("Executing Ansible: %s", strings.Join(cmd.Args, " "))) if err := cmd.Start(); err != nil { return err } diff --git a/website/source/docs/provisioners/ansible-local.html.md b/website/source/docs/provisioners/ansible-local.html.md index 2863f3baa..670c3a1e9 100644 --- a/website/source/docs/provisioners/ansible-local.html.md +++ b/website/source/docs/provisioners/ansible-local.html.md @@ -136,3 +136,24 @@ chi-appservers able to create directories and write into this folder. If the permissions are not correct, use a shell provisioner prior to this to configure it properly. + +## Default Extra Variables + +In addition to being able to specify extra arguments using the +`extra_arguments` configuration, the provisioner automatically defines certain +commonly useful Ansible variables: + +- `packer_build_name` is set to the name of the build that Packer is running. + This is most useful when Packer is making multiple builds and you want to + distinguish them slightly when using a common playbook. + +- `packer_builder_type` is the type of the builder that was used to create the + machine that the script is running on. This is useful if you want to run + only certain parts of the playbook on systems built with certain builders. + +- `packer_http_addr` If using a builder that provides an http server for file + transfer (such as hyperv, parallels, qemu, virtualbox, and vmware), this + will be set to the address. You can use this address in your provisioner to + download large files over http. This may be useful if you're experiencing + slower speeds using the default file provisioner. A file provisioner using + the `winrm` communicator may experience these types of difficulties. diff --git a/website/source/docs/provisioners/ansible.html.md b/website/source/docs/provisioners/ansible.html.md index 316263538..db642f7cf 100644 --- a/website/source/docs/provisioners/ansible.html.md +++ b/website/source/docs/provisioners/ansible.html.md @@ -113,6 +113,22 @@ Optional Parameters: - `user` (string) - The `ansible_user` to use. Defaults to the user running packer. + +## Default Extra Variables + +In addition to being able to specify extra arguments using the +`extra_arguments` configuration, the provisioner automatically defines certain +commonly useful Ansible variables: + +- `packer_build_name` is set to the name of the build that Packer is running. + This is most useful when Packer is making multiple builds and you want to + distinguish them slightly when using a common playbook. + +- `packer_builder_type` is the type of the builder that was used to create the + machine that the script is running on. This is useful if you want to run + only certain parts of the playbook on systems built with certain builders. + + ## Limitations ### Redhat / CentOS