From 1172e670c09339c25bead67eb881f7145f09f505 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20L=C3=B6fgren?= Date: Mon, 7 Mar 2016 09:20:18 +0100 Subject: [PATCH 1/2] Add support for ansible-galaxy This adds "galaxy_file" to the options Implement #3301 --- provisioner/ansible-local/provisioner.go | 54 ++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/provisioner/ansible-local/provisioner.go b/provisioner/ansible-local/provisioner.go index 7a7a3f01a..fa4a8db23 100644 --- a/provisioner/ansible-local/provisioner.go +++ b/provisioner/ansible-local/provisioner.go @@ -52,6 +52,12 @@ type Config struct { // The optional inventory groups InventoryGroups []string `mapstructure:"inventory_groups"` + + // The optional ansible-galaxy requirements file + GalaxyFile string `mapstructure:"galaxy_file"` + + // The command to run ansible-galaxy + GalaxyCommand string } type Provisioner struct { @@ -74,6 +80,9 @@ func (p *Provisioner) Prepare(raws ...interface{}) error { if p.config.Command == "" { p.config.Command = "ANSIBLE_FORCE_COLOR=1 PYTHONUNBUFFERED=1 ansible-playbook" } + if p.config.GalaxyCommand == "" { + p.config.GalaxyCommand = "ansible-galaxy" + } if p.config.StagingDir == "" { p.config.StagingDir = DefaultStagingDir @@ -94,6 +103,14 @@ func (p *Provisioner) Prepare(raws ...interface{}) error { } } + // Check that the galaxy file exists, if configured + if len(p.config.GalaxyFile) > 0 { + err = validateFileConfig(p.config.GalaxyFile, "galaxy_file", true) + if err != nil { + errs = packer.MultiErrorAppend(errs, err) + } + } + // Check that the playbook_dir directory exists, if configured if len(p.config.PlaybookDir) > 0 { if err := validateDirConfig(p.config.PlaybookDir, "playbook_dir"); err != nil { @@ -181,6 +198,15 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error { }() } + if len(p.config.GalaxyFile) > 0 { + ui.Message("Uploading galaxy file...") + src = p.config.GalaxyFile + dst = filepath.ToSlash(filepath.Join(p.config.StagingDir, filepath.Base(src))) + if err := p.uploadFile(ui, comm, dst, src); err != nil { + return fmt.Errorf("Error uploading galaxy file: %s", err) + } + } + ui.Message("Uploading inventory file...") src = p.config.InventoryFile dst = filepath.ToSlash(filepath.Join(p.config.StagingDir, filepath.Base(src))) @@ -242,6 +268,27 @@ func (p *Provisioner) Cancel() { os.Exit(0) } +func (p *Provisioner) executeGalaxy(ui packer.Ui, comm packer.Communicator) error { + rolesDir := filepath.ToSlash(filepath.Join(p.config.StagingDir, "roles")) + galaxyFile := filepath.ToSlash(filepath.Join(p.config.StagingDir, filepath.Base(p.config.GalaxyFile))) + + // ansible-galaxy install -r requirements.yml -p roles/ + command := fmt.Sprintf("cd %s && %s install -r %s -p %s", + p.config.StagingDir, p.config.GalaxyCommand, galaxyFile, rolesDir) + ui.Message(fmt.Sprintf("Executing Ansible Galaxy: %s", command)) + cmd := &packer.RemoteCmd{ + Command: command, + } + if err := cmd.StartWithUi(comm, ui); err != nil { + return err + } + if cmd.ExitStatus != 0 { + // ansible-galaxy version 2.0.0.2 doesn't return exit codes on error.. + return fmt.Errorf("Non-zero exit status: %d", cmd.ExitStatus) + } + return nil +} + func (p *Provisioner) executeAnsible(ui packer.Ui, comm packer.Communicator) error { 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))) @@ -251,6 +298,13 @@ func (p *Provisioner) executeAnsible(ui packer.Ui, comm packer.Communicator) err extraArgs = " " + strings.Join(p.config.ExtraArguments, " ") } + // Fetch external dependencies + if len(p.config.GalaxyFile) > 0 { + if err := p.executeGalaxy(ui, comm); err != nil { + return fmt.Errorf("Error executing Ansible Galaxy: %s", err) + } + } + command := fmt.Sprintf("cd %s && %s %s%s -c local -i %s", p.config.StagingDir, p.config.Command, playbook, extraArgs, inventory) ui.Message(fmt.Sprintf("Executing Ansible: %s", command)) From eee54d48f34d19579fdf8cbfeaf7acb80a91bc48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20L=C3=B6fgren?= Date: Thu, 1 Sep 2016 13:51:14 +0200 Subject: [PATCH 2/2] Add documentation for galaxy_file --- website/source/docs/provisioners/ansible-local.html.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/website/source/docs/provisioners/ansible-local.html.md b/website/source/docs/provisioners/ansible-local.html.md index 856e9ec8b..d54a50ef8 100644 --- a/website/source/docs/provisioners/ansible-local.html.md +++ b/website/source/docs/provisioners/ansible-local.html.md @@ -102,6 +102,11 @@ chi-appservers your local system. These will be uploaded to the remote machine under `staging_directory`/playbooks. By default, this is empty. +- `galaxy_file` (string) - A requirements file which provides a way to install + roles with the [ansible-galaxy + cli](http://docs.ansible.com/ansible/galaxy.html#the-ansible-galaxy-command-line-tool) + on the remote machine. By default, this is empty. + - `group_vars` (string) - a path to the directory containing ansible group variables on your local system to be copied to the remote machine. By default, this is empty.