diff --git a/README.md b/README.md index 8c4b4ea7..1f9503f7 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,7 @@ Note that Node attributes are only evaluated for Node Executor jobs, Workflow Jo The following configuration attributes can be set on the Node, or in the project.properties or framework.properties. To add them to project.properties, prefix them with "project." and for framework.properties prefix them with "framework.": -* `ansible-inventory` - Specifies the ansible inventory to use, can define a global inventory file at the project level without requiring setting the same variable for each job. (default /etc/ansible/hosts) +* `ansible-inventory` - Specifies the ansible inventory to use, can define a global inventory file at the project level without requiring setting the same variable for each job. It is also possible to provide an inventory _inline_ to a job. The default is /etc/ansible/hosts. * `ansible-executable` - The executable to use for node Node Executor. (default /bin/sh) * `ansible-limit` - Global groups limits can be set at the project level to filter hosts/groups from the Ansible inventory. See http://docs.ansible.com/ansible/intro_patterns.html for syntax help. * `ansible-vault-path` - Default vault file path to use for Playbook Jobs. diff --git a/src/main/groovy/com/rundeck/plugins/ansible/ansible/AnsibleDescribable.java b/src/main/groovy/com/rundeck/plugins/ansible/ansible/AnsibleDescribable.java index 9b7c602f..ae3c8e7c 100644 --- a/src/main/groovy/com/rundeck/plugins/ansible/ansible/AnsibleDescribable.java +++ b/src/main/groovy/com/rundeck/plugins/ansible/ansible/AnsibleDescribable.java @@ -93,6 +93,7 @@ public static String[] getValues() { public static final String SERVICE_PROVIDER_TYPE = "ansible-service"; public static final String ANSIBLE_PLAYBOOK_PATH = "ansible-playbook"; public static final String ANSIBLE_PLAYBOOK_INLINE = "ansible-playbook-inline"; + public static final String ANSIBLE_INVENTORY_INLINE = "ansible-inventory-inline"; public static final String ANSIBLE_INVENTORY = "ansible-inventory"; public static final String ANSIBLE_GENERATE_INVENTORY = "ansible-generate-inventory"; public static final String ANSIBLE_MODULE = "ansible-module"; @@ -181,6 +182,15 @@ public static String[] getValues() { .renderingOption(StringRenderingConstants.CODE_SYNTAX_SELECTABLE, false) .build(); + public static Property INVENTORY_INLINE_PROP = PropertyBuilder.builder() + .string(ANSIBLE_INVENTORY_INLINE) + .required(false) + .title("Inline inventory") + .description("Provide an inline inventory.") + .renderingOption(StringRenderingConstants.DISPLAY_TYPE_KEY, StringRenderingConstants.DisplayType.CODE) + .renderingOption(StringRenderingConstants.CODE_SYNTAX_SELECTABLE, true) + .build(); + public static Property MODULE_PROP = PropertyUtil.string( ANSIBLE_MODULE, diff --git a/src/main/groovy/com/rundeck/plugins/ansible/ansible/AnsibleInlineInventoryBuilder.java b/src/main/groovy/com/rundeck/plugins/ansible/ansible/AnsibleInlineInventoryBuilder.java new file mode 100644 index 00000000..6c0014be --- /dev/null +++ b/src/main/groovy/com/rundeck/plugins/ansible/ansible/AnsibleInlineInventoryBuilder.java @@ -0,0 +1,31 @@ +package com.rundeck.plugins.ansible.ansible; + +import com.dtolabs.rundeck.core.common.INodeEntry; +import com.dtolabs.rundeck.core.plugins.configuration.ConfigurationException; + +import java.io.File; +import java.io.PrintWriter; +import java.util.Collection; +import java.util.HashMap; + +public class AnsibleInlineInventoryBuilder { + + private final String inline_inventory; + + public AnsibleInlineInventoryBuilder(String inline_inventory) { + this.inline_inventory = inline_inventory; + } + + public File buildInventory() throws ConfigurationException { + try { + File file = File.createTempFile("ansible-inventory", ".inventory"); + file.deleteOnExit(); + PrintWriter writer = new PrintWriter(file); + writer.write(inline_inventory); + writer.close(); + return file; + } catch (Exception e) { + throw new ConfigurationException("Could not write temporary inventory: " + e.getMessage()); + } + } +} diff --git a/src/main/groovy/com/rundeck/plugins/ansible/ansible/AnsibleRunnerBuilder.java b/src/main/groovy/com/rundeck/plugins/ansible/ansible/AnsibleRunnerBuilder.java index c433b9b9..76fbe945 100644 --- a/src/main/groovy/com/rundeck/plugins/ansible/ansible/AnsibleRunnerBuilder.java +++ b/src/main/groovy/com/rundeck/plugins/ansible/ansible/AnsibleRunnerBuilder.java @@ -697,22 +697,45 @@ public Boolean generateInventory() { public String getInventory() throws ConfigurationException { String inventory; + String inline_inventory; Boolean isGenerated = generateInventory(); + if (isGenerated !=null && isGenerated) { File tempInventory = new AnsibleInventoryBuilder(this.nodes).buildInventory(); tempFiles.add(tempInventory); inventory = tempInventory.getAbsolutePath(); return inventory; } + inline_inventory = PropertyResolver.resolveProperty( + AnsibleDescribable.ANSIBLE_INVENTORY_INLINE, + null, + getFrameworkProject(), + getFramework(), + getNode(), + getjobConf() + ); + + if (inline_inventory != null) { + /* Create tmp file with inventory */ + /* + the builder gets the nodes from rundeck in rundeck node format and converts to ansible inventory + we don't want that, we simply want the list we provided in ansible format + */ + File tempInventory = new AnsibleInlineInventoryBuilder(inline_inventory).buildInventory(); + tempFiles.add(tempInventory); + inventory = tempInventory.getAbsolutePath(); + return inventory; + } + inventory = PropertyResolver.resolveProperty( - AnsibleDescribable.ANSIBLE_INVENTORY, - null, - getFrameworkProject(), - getFramework(), - getNode(), - getjobConf() - ); + AnsibleDescribable.ANSIBLE_INVENTORY, + null, + getFrameworkProject(), + getFramework(), + getNode(), + getjobConf() + ); if (null != inventory && inventory.contains("${")) { return DataContextUtils.replaceDataReferences(inventory, getContext().getDataContext()); diff --git a/src/main/groovy/com/rundeck/plugins/ansible/plugin/AnsibleFileCopier.java b/src/main/groovy/com/rundeck/plugins/ansible/plugin/AnsibleFileCopier.java index 602bda22..880c19bb 100644 --- a/src/main/groovy/com/rundeck/plugins/ansible/plugin/AnsibleFileCopier.java +++ b/src/main/groovy/com/rundeck/plugins/ansible/plugin/AnsibleFileCopier.java @@ -34,6 +34,7 @@ public class AnsibleFileCopier implements FileCopier, AnsibleDescribable { builder.title("Ansible File Copier"); builder.description("Sends a file to a node via the copy module."); builder.property(BINARIES_DIR_PATH_PROP); + builder.property(INVENTORY_INLINE_PROP); builder.property(CONFIG_FILE_PATH); builder.property(SSH_AUTH_TYPE_PROP); builder.property(SSH_USER_PROP); diff --git a/src/main/groovy/com/rundeck/plugins/ansible/plugin/AnsibleModuleWorkflowStep.java b/src/main/groovy/com/rundeck/plugins/ansible/plugin/AnsibleModuleWorkflowStep.java index a70a805a..4f5e5c7d 100644 --- a/src/main/groovy/com/rundeck/plugins/ansible/plugin/AnsibleModuleWorkflowStep.java +++ b/src/main/groovy/com/rundeck/plugins/ansible/plugin/AnsibleModuleWorkflowStep.java @@ -30,6 +30,7 @@ public class AnsibleModuleWorkflowStep implements StepPlugin, AnsibleDescribable builder.property(BINARIES_DIR_PATH_PROP); builder.property(BASE_DIR_PROP); + builder.property(INVENTORY_INLINE_PROP); builder.property(MODULE_PROP); builder.property(MODULE_ARGS_PROP); builder.property(SSH_AUTH_TYPE_PROP); diff --git a/src/main/groovy/com/rundeck/plugins/ansible/plugin/AnsiblePlaybookInlineWorkflowStep.java b/src/main/groovy/com/rundeck/plugins/ansible/plugin/AnsiblePlaybookInlineWorkflowStep.java index f12ea686..39abcd71 100644 --- a/src/main/groovy/com/rundeck/plugins/ansible/plugin/AnsiblePlaybookInlineWorkflowStep.java +++ b/src/main/groovy/com/rundeck/plugins/ansible/plugin/AnsiblePlaybookInlineWorkflowStep.java @@ -32,6 +32,7 @@ public class AnsiblePlaybookInlineWorkflowStep implements StepPlugin, AnsibleDes builder.property(BASE_DIR_PROP); builder.property(PLAYBOOK_INLINE_PROP); builder.property(EXTRA_VARS_PROP); + builder.property(INVENTORY_INLINE_PROP); builder.property(VAULT_KEY_FILE_PROP); builder.property(VAULT_KEY_STORAGE_PROP); builder.property(EXTRA_ATTRS_PROP); diff --git a/src/main/groovy/com/rundeck/plugins/ansible/plugin/AnsiblePlaybookWorkflowStep.java b/src/main/groovy/com/rundeck/plugins/ansible/plugin/AnsiblePlaybookWorkflowStep.java index 757c94b5..9ae15392 100644 --- a/src/main/groovy/com/rundeck/plugins/ansible/plugin/AnsiblePlaybookWorkflowStep.java +++ b/src/main/groovy/com/rundeck/plugins/ansible/plugin/AnsiblePlaybookWorkflowStep.java @@ -32,6 +32,7 @@ public class AnsiblePlaybookWorkflowStep implements StepPlugin, AnsibleDescribab builder.property(BASE_DIR_PROP); builder.property(PLAYBOOK_PATH_PROP); builder.property(EXTRA_VARS_PROP); + builder.property(INVENTORY_INLINE_PROP); builder.property(VAULT_KEY_FILE_PROP); builder.property(VAULT_KEY_STORAGE_PROP); builder.property(EXTRA_ATTRS_PROP);