From cd14cb701243b84180f1a496f9aef8e812f4306f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Pineau?= Date: Thu, 30 Apr 2015 18:33:15 +0200 Subject: [PATCH] [Provisioner][Ansible] Added support for inventory group --- provisioner/ansible-local/provisioner.go | 20 +++++++++++++++---- .../provisioners/ansible-local.html.markdown | 10 ++++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/provisioner/ansible-local/provisioner.go b/provisioner/ansible-local/provisioner.go index 3ab37c086..28249b346 100644 --- a/provisioner/ansible-local/provisioner.go +++ b/provisioner/ansible-local/provisioner.go @@ -47,6 +47,9 @@ type Config struct { // The optional inventory file InventoryFile string `mapstructure:"inventory_file"` + + // The optional inventory groups + InventoryGroups []string `mapstructure:"inventory_groups"` } type Provisioner struct { @@ -99,9 +102,10 @@ func (p *Provisioner) Prepare(raws ...interface{}) error { } sliceTemplates := map[string][]string{ - "extra_arguments": p.config.ExtraArguments, - "playbook_paths": p.config.PlaybookPaths, - "role_paths": p.config.RolePaths, + "extra_arguments": p.config.ExtraArguments, + "playbook_paths": p.config.PlaybookPaths, + "role_paths": p.config.RolePaths, + "inventory_groups": p.config.InventoryGroups, } for n, slice := range sliceTemplates { @@ -196,7 +200,15 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error { return fmt.Errorf("Error preparing inventory file: %s", err) } defer os.Remove(tf.Name()) - _, err = tf.Write([]byte("127.0.0.1")) + if len(p.config.InventoryGroups) != 0 { + content := "" + for _, group := range p.config.InventoryGroups { + content += fmt.Sprintf("[%s]\n127.0.0.1\n", group) + } + _, err = tf.Write([]byte(content)) + } else { + _, err = tf.Write([]byte("127.0.0.1")) + } if err != nil { tf.Close() return fmt.Errorf("Error preparing inventory file: %s", err) diff --git a/website/source/docs/provisioners/ansible-local.html.markdown b/website/source/docs/provisioners/ansible-local.html.markdown index 63b98ade4..0a12dbc5b 100644 --- a/website/source/docs/provisioners/ansible-local.html.markdown +++ b/website/source/docs/provisioners/ansible-local.html.markdown @@ -41,6 +41,16 @@ Optional: * `extra_arguments` (array of strings) - An array of extra arguments to pass to the ansible command. By default, this is empty. +* `inventory_groups` (string) - You can let Packer generate a temporary inventory + for you. It will contains only `127.0.0.1`. Thanks to `inventory_groups`, + packer will set the current machine into different groups and will + generate an inventory like: + + [my_group_1] + 127.0.0.1 + [my_group_2] + 127.0.0.1 + * `inventory_file` (string) - The inventory file to be used by ansible. This file must exist on your local system and will be uploaded to the remote machine.