Develop > Application Configuration > CML primer > Use case 3 - Complex repeating values in the configuration file

Use case 3 - Complex repeating values in the configuration file

This example models the /etc/hosts file, which is a list of IP addresses followed by a list of host names like this:

127.0.0.1 localhost
192.168.0.1 server1 server1.domain.com
192.168.0.2 server2 server2.domain.com

This example is similar to the previous example, except that this example iterates over a more complex line. The best way to think about this is by first modeling the single instance of the line using CML Replace instructions like this:

@ip-addr;ip@ @sname;unordered-hostname-set@

This line defines two replace instructions, one for the IP address and one for the server name.

Notice that the “ip-addr” replace tag specifies the data type as “ip” because it must be an IP address.

The “sname” replace tag is typed as an “unordered-hostname-set”. This means that it can match a list of host names and it will store them along with the corresponding IP address. This is similar to how the Loop tag works and the values get stored in the same way.

This CML is for one iteration. The next step encloses it in a loop. To do this, use the name space loop, since it is iterating over more than one value on each line, and prepend a “.” to the names of the tags in the loop, as follows:

@*entries;unordered-namespace-set@
@.ip-addr;ip@ @.sname;unordered-hostname-set@

The Loop tag (indicated by the @* characters) defines a loop. The “unordered-hostname-set” indicates that the data are host names, the host names can be in any order, and the values must be unique.

The “.” characters added before the “ip-addr” and “sname” strings in the Replace instruction indicate that these are the target of the Loop instruction.

Final CML

Adding the above loop and replace CML to the required name space and file lines gives the following.

@!namespace=/example/namespace/@
@!filename-key=/files/example@
@!filename-default=/etc/hosts@
@*entries;unordered-namespace-set@
@.ip-addr;ip@ @sname;unordered-hostname-set@

Resulting value set

Below are the values that will be stored in the SA database if the entries in the sample file are read in using the SA Client.

/example/namespace/entries1/ip-addr = 127.0.0.1
/example/namespace/entries1/sname/1 = localhost
/example/namespace/entries2/ip-addr = 192.168.0.1
/example/namespace/entries2/sname/1 = server1
/example/namespace/entries2/sname/2 = server1.domain.com
/example/namespace/entries3/ip-addr = 192.168.0.2
/example/namespace/entries3/sname/1 = server2
/example/namespace/entries3/sname/2 = server2.domain.com