Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,26 @@ public void testHello() throws Exception {
assertLog(".*\"msg\":.*Hello, world.*", ab);
}

@Test
public void testConfigFile() throws Exception {
URI dir = AnsibleIT.class.getResource("ansibleConfigFile").toURI();
byte[] payload = archive(dir);

// ---

StartProcessResponse spr = start(payload);

// ---

ProcessEntry pir = waitForCompletion(getApiClient(), spr.getInstanceId());
assertEquals(ProcessEntry.StatusEnum.FINISHED, pir.getStatus());

// ---

byte[] ab = getLog(pir.getInstanceId());
assertLog(".*\"msg\":.*Hello, world.*", ab);
}

@Test
public void testSkipTags() throws Exception {
URI dir = AnsibleIT.class.getResource("ansibleSkipTags").toURI();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
configuration:
dependencies:
- mvn://com.walmartlabs.concord.plugins.basic:ansible-tasks:PROJECT_VERSION
runtime: "concord-v2"

flows:
default:
- task: ansible
in:
playbook: playbook/hello.yml
configFile: user.cfg
extraVars:
greetings: "Hello, world"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[local]
127.0.0.1 ansible_connection=local
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
- hosts: local
tasks:
- debug:
msg: "{{ greetings }}"
verbosity: 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[defaults]
inventory = ./myInventory.ini
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@

public class AnsibleInventory {

public static void process(AnsibleContext context, PlaybookScriptBuilder playbook) throws IOException {
public static void process(AnsibleContext context, AnsibleConfig cfg, PlaybookScriptBuilder playbook) throws IOException {
List<String> inventories = new AnsibleInventory(context.workDir(), context.tmpDir(), context.debug())
.write(context.args());
.write(context.args(), cfg);

playbook.withInventories(inventories);
}
Expand All @@ -54,8 +54,8 @@ public AnsibleInventory(Path workDir, Path tmpDir, boolean debug) {
this.debug = debug;
}

public List<String> write(Map<String, Object> args) throws IOException {
List<Path> paths = writeInventory(args);
public List<String> write(Map<String, Object> args, AnsibleConfig cfg) throws IOException {
List<Path> paths = writeInventory(args, cfg);

if (debug) {
for (Path p : paths) {
Expand All @@ -70,7 +70,7 @@ public List<String> write(Map<String, Object> args) throws IOException {
}

@SuppressWarnings("unchecked")
private List<Path> writeInventory(Map<String, Object> args) throws IOException {
private List<Path> writeInventory(Map<String, Object> args, AnsibleConfig cfg) throws IOException {
// try an "inline" inventory
Object v = MapUtils.get(args, TaskParams.INVENTORY_KEY.getKey(), null);

Expand Down Expand Up @@ -144,6 +144,17 @@ private List<Path> writeInventory(Map<String, Object> args) throws IOException {
return Collections.singletonList(p);
}

// try the defaults from the configuration file
v = cfg.getDefaults().getString("inventory");
if (v != null) {
p = workDir.resolve(v.toString());
if (Files.exists(p)) {
Utils.updateScriptPermissions(p);
log.info("Using the inventory specified in the configuration file: {}", p);
return Collections.singletonList(p);
}
}

// we can't continue without an inventory
throw new IOException("'" + TaskParams.INVENTORY_KEY.getKey() + "', '" + TaskParams.INVENTORY_FILE_KEY.getKey()
+ "' or '" + TaskParams.DYNAMIC_INVENTORY_FILE_KEY.getKey() + "' parameter is required");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public TaskResult.SimpleResult run(AnsibleContext context,

PlaybookScriptBuilder b = new PlaybookScriptBuilder(context, playbook);

AnsibleInventory.process(context, b);
AnsibleInventory.process(context, cfg, b);

AnsibleVaultId.process(context, b);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@

import java.util.Map;

import static com.walmartlabs.concord.sdk.MapUtils.getString;

public class ConfigSection {

private final Map<String, Object> items;
Expand All @@ -33,7 +31,7 @@ public ConfigSection(Map<String, Object> items) {
}

public ConfigSection prependPath(String key, String path) {
String current = getString(items, key);
String current = getString(key);
if (current != null) {
items.put(key, path + ":" + current);
} else {
Expand All @@ -46,4 +44,15 @@ public ConfigSection put(String key, String value) {
items.put(key, value);
return this;
}

public String getString(String key) {
Object v = items.get(key);
if (v == null) {
return null;
}
if (v instanceof String s) {
return s;
}
throw new RuntimeException("Expected a string value in '%s', got %s (%s)".formatted(key, v, v.getClass()));
}
}