Skip to content

Commit

Permalink
Merge pull request #318 from rundeck-plugins/merged-inline-inventory
Browse files Browse the repository at this point in the history
Inline inventory
  • Loading branch information
ltamaster authored Jun 7, 2022
2 parents dc463d2 + 7e65858 commit ca99ed7
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit ca99ed7

Please sign in to comment.