Skip to content

Commit 8e4cd30

Browse files
authored
Merge pull request #411 from rundeck-plugins/RUN-3409-Ansible-Model-Sources-return-host-vars-data-in-key-value-format-when-Gather-Facts-is-set-to-no
Run 3409 ansible model sources return host vars data in key value format when gather facts is set to no
2 parents e0559e4 + cf02270 commit 8e4cd30

File tree

3 files changed

+73
-1
lines changed

3 files changed

+73
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ hs_err_pid*
7373
/.project
7474
/.settings/
7575

76+
/functional-test/bin/
77+
7678
# Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored)
7779
!gradle/wrapper/gradle-wrapper.jar
7880

src/main/groovy/com/rundeck/plugins/ansible/plugin/AnsibleResourceModelSource.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import org.rundeck.app.spi.Services;
3333
import org.rundeck.storage.api.PathUtil;
3434
import org.rundeck.storage.api.StorageException;
35+
import org.slf4j.Logger;
36+
import org.slf4j.LoggerFactory;
3537
import org.yaml.snakeyaml.LoaderOptions;
3638
import org.yaml.snakeyaml.Yaml;
3739
import org.yaml.snakeyaml.constructor.SafeConstructor;
@@ -68,8 +70,10 @@
6870
@Slf4j
6971
public class AnsibleResourceModelSource implements ResourceModelSource, ProxyRunnerPlugin {
7072

73+
private static final Logger logger = LoggerFactory.getLogger(AnsibleResourceModelSource.class);
7174
public static final String HOST_TPL_J2 = "host-tpl.j2";
7275
public static final String GATHER_HOSTS_YML = "gather-hosts.yml";
76+
private final Gson gson = new Gson();
7377

7478
@Setter
7579
private Framework framework;
@@ -801,9 +805,14 @@ public NodeEntryImpl createNodeEntry(Map.Entry<String, Object> hostNode) throws
801805
Map<String, Object> nodeValues = InventoryList.getType(hostNode.getValue());
802806

803807
applyNodeTags(node, nodeValues);
808+
804809
nodeValues.forEach((key, value) -> {
805810
if (value != null) {
806-
node.setAttribute(key, value.toString());
811+
if (value instanceof Map || value instanceof List) {
812+
node.setAttribute(key, gson.toJson(value));
813+
} else {
814+
node.setAttribute(key, value.toString());
815+
}
807816
}
808817
});
809818

src/test/groovy/com/rundeck/plugins/ansible/plugin/AnsibleResourceModelSourceSpec.groovy

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,67 @@ class AnsibleResourceModelSourceSpec extends Specification {
163163
then: "throws an exception because data is too big to be precessed"
164164
thrown(ResourceModelSourceException)
165165
}
166+
void "structured data like ports is serialized as JSON when gather facts is false"() {
167+
given:
168+
def nodeName = 'NODE_JSON'
169+
def structuredPorts = [
170+
[port: 22, protocol: 'tcp', service: 'ssh', state: 'open'],
171+
[port: 80, protocol: 'tcp', service: 'http', state: 'open']
172+
]
173+
174+
Framework framework = Mock(Framework) {
175+
getPropertyLookup() >> Mock(IPropertyLookup){
176+
getProperty("framework.tmp.dir") >> '/tmp'
177+
}
178+
getBaseDir() >> Mock(File) {
179+
getAbsolutePath() >> '/tmp'
180+
}
181+
}
182+
ResourceModelSource plugin = new AnsibleResourceModelSource(framework)
183+
Properties config = new Properties()
184+
config.put('project', 'project_1')
185+
config.put(AnsibleDescribable.ANSIBLE_GATHER_FACTS, 'false')
186+
plugin.configure(config)
187+
188+
Services services = Mock(Services) {
189+
getService(KeyStorageTree.class) >> Mock(KeyStorageTree)
190+
}
191+
plugin.setServices(services)
192+
193+
def host = [(nodeName): [
194+
'ports': structuredPorts
195+
]]
196+
def hosts = ['hosts': host]
197+
def groups = ['ungrouped': hosts]
198+
def children = ['children': groups]
199+
def all = ['all': children]
200+
201+
Yaml yaml = new Yaml()
202+
String result = yaml.dump(all)
203+
204+
AnsibleInventoryListBuilder inventoryListBuilder = Mock(AnsibleInventoryListBuilder) {
205+
build() >> Mock(AnsibleInventoryList) {
206+
getNodeList() >> result
207+
}
208+
}
209+
210+
plugin.ansibleInventoryListBuilder = inventoryListBuilder
211+
212+
when:
213+
INodeSet nodes = plugin.getNodes()
214+
INodeEntry node = nodes.getNode(nodeName)
215+
216+
then:
217+
node != null
218+
node.getAttributes().containsKey("ports")
219+
220+
and:
221+
def portsJson = node.getAttributes().get("ports")
222+
portsJson.startsWith("[")
223+
portsJson.contains("\"port\":22")
224+
portsJson.contains("\"service\":\"ssh\"")
225+
}
226+
166227

167228
void "tag hosts is empty"() {
168229
given:

0 commit comments

Comments
 (0)