0

I want to write a DSL job build script for jenkins in groovy which automatically make deploy job for our projects. There is a general yml file for ansible roles and hosts parameter in each project which I want to read it and use its contents to configure the job.

The problem is that so far I'm using the snakeyml for reading the yml file, but it returns an arraylist (more like a map) which I cannot use efficiently.

anyone knows a better solution?

my yml sample file:

---
- hosts: app.host
  roles:
  - role: app-db
    db_name: myproje_db
    db_port: "3306"
    migrate_module: "my-proje-api"
  - role: java-app
    app_name: "myproje-api"
    app_artifact_name: "my-proje-api"
    app_links:
    - myproje_db

I read the file from workspace in my main groovy script:

InputStream  configFile = streamFileFromWorkspace('data/config.yml')

and process it in another function of another class:

public String configFileReader(def out, InputStream  configFile){
      def map
      Yaml configFileYml = new Yaml()
      map = configFileYml.load(configFile)
}

it returns map class type as arraylist.

Fezo
  • 183
  • 2
  • 14

1 Answers1

2

It's an expected output, this configuration is starting with a "-" which represent a list. It's "a collection of hosts, and each host have a set of roles".

If you wants to iterate on each host, you can do :

Yaml configFileYml = new Yaml()
configFileYml.load(configFile).each { host -> ... }

When this configuration is read, it's equivalent to the following structure (in groovy format):

[ // collection of map (host)
 [ // 1 map for each host
  hosts:"app.host",
  roles:[ // collection of map (role)
    [ // 1 map for each role
     role: 'app-db',
     db_name: 'myproje_db',
     db_port: "3306",
     migrate_module: "my-proje-api"
    ],
    [
     role: 'java-app',
     app_name: "myproje-api",
     app_artifact_name: "my-proje-api",
     app_links:['myproje_db']
    ]
  ]
 ]
]
Jérémie B
  • 10,611
  • 1
  • 26
  • 43
  • Thanks in advance, I got my mistake. Do you mind to write a complete sample code for this yaml? for example how can I get access to item myproje_db in app_links of java-app role? cause the sample code did not worked for me and it gives me error for casting pointer to string. – Fezo Mar 06 '16 at 13:52
  • also what if I do not know the name of a property? like I do not know there is a property as migrate_module but I'm writing this properties in another config file or I'm making a map of it. I really appreciate any clue you could give me about this. – Fezo Mar 06 '16 at 14:05
  • I have added a mapping to a groovy structure when the configuration is read. It's just a collection of maps. – Jérémie B Mar 06 '16 at 14:15