Skip to content

Commit 51b4e98

Browse files
committed
ansible-tasks: support inventories specified in configFile
1 parent 2034ae3 commit 51b4e98

File tree

8 files changed

+72
-9
lines changed

8 files changed

+72
-9
lines changed

it/server/src/test/java/com/walmartlabs/concord/it/server/AnsibleIT.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,26 @@ public void testHello() throws Exception {
6262
assertLog(".*\"msg\":.*Hello, world.*", ab);
6363
}
6464

65+
@Test
66+
public void testConfigFile() throws Exception {
67+
URI dir = AnsibleIT.class.getResource("ansibleConfigFile").toURI();
68+
byte[] payload = archive(dir);
69+
70+
// ---
71+
72+
StartProcessResponse spr = start(payload);
73+
74+
// ---
75+
76+
ProcessEntry pir = waitForCompletion(getApiClient(), spr.getInstanceId());
77+
assertEquals(ProcessEntry.StatusEnum.FINISHED, pir.getStatus());
78+
79+
// ---
80+
81+
byte[] ab = getLog(pir.getInstanceId());
82+
assertLog(".*\"msg\":.*Hello, world.*", ab);
83+
}
84+
6585
@Test
6686
public void testSkipTags() throws Exception {
6787
URI dir = AnsibleIT.class.getResource("ansibleSkipTags").toURI();
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
configuration:
2+
dependencies:
3+
- mvn://com.walmartlabs.concord.plugins.basic:ansible-tasks:PROJECT_VERSION
4+
runtime: "concord-v2"
5+
6+
flows:
7+
default:
8+
- task: ansible
9+
in:
10+
playbook: playbook/hello.yml
11+
configFile: user.cfg
12+
extraVars:
13+
greetings: "Hello, world"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[local]
2+
127.0.0.1 ansible_connection=local
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
- hosts: local
3+
tasks:
4+
- debug:
5+
msg: "{{ greetings }}"
6+
verbosity: 0
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[defaults]
2+
inventory = ./myInventory.ini

plugins/tasks/ansible/src/main/java/com/walmartlabs/concord/plugins/ansible/AnsibleInventory.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@
3535

3636
public class AnsibleInventory {
3737

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

4242
playbook.withInventories(inventories);
4343
}
@@ -54,8 +54,8 @@ public AnsibleInventory(Path workDir, Path tmpDir, boolean debug) {
5454
this.debug = debug;
5555
}
5656

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

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

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

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

147+
// try the defaults from the configuration file
148+
v = cfg.getDefaults().getString("inventory");
149+
if (v != null) {
150+
p = workDir.resolve(v.toString());
151+
if (Files.exists(p)) {
152+
Utils.updateScriptPermissions(p);
153+
log.info("Using the inventory specified in the configuration file: {}", p);
154+
return Collections.singletonList(p);
155+
}
156+
}
157+
147158
// we can't continue without an inventory
148159
throw new IOException("'" + TaskParams.INVENTORY_KEY.getKey() + "', '" + TaskParams.INVENTORY_FILE_KEY.getKey()
149160
+ "' or '" + TaskParams.DYNAMIC_INVENTORY_FILE_KEY.getKey() + "' parameter is required");

plugins/tasks/ansible/src/main/java/com/walmartlabs/concord/plugins/ansible/AnsibleTask.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public TaskResult.SimpleResult run(AnsibleContext context,
8585

8686
PlaybookScriptBuilder b = new PlaybookScriptBuilder(context, playbook);
8787

88-
AnsibleInventory.process(context, b);
88+
AnsibleInventory.process(context, cfg, b);
8989

9090
AnsibleVaultId.process(context, b);
9191

plugins/tasks/ansible/src/main/java/com/walmartlabs/concord/plugins/ansible/ConfigSection.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222

2323
import java.util.Map;
2424

25-
import static com.walmartlabs.concord.sdk.MapUtils.getString;
26-
2725
public class ConfigSection {
2826

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

3533
public ConfigSection prependPath(String key, String path) {
36-
String current = getString(items, key);
34+
String current = getString(key);
3735
if (current != null) {
3836
items.put(key, path + ":" + current);
3937
} else {
@@ -46,4 +44,15 @@ public ConfigSection put(String key, String value) {
4644
items.put(key, value);
4745
return this;
4846
}
47+
48+
public String getString(String key) {
49+
Object v = items.get(key);
50+
if (v == null) {
51+
return null;
52+
}
53+
if (v instanceof String s) {
54+
return s;
55+
}
56+
throw new RuntimeException("Expected a string value in '%s', got %s (%s)".formatted(key, v, v.getClass()));
57+
}
4958
}

0 commit comments

Comments
 (0)