two puppet tricks: combining arrays and local tests

  1. Joining Arrays
    I found myself wanting to join a bunch of arrays in my puppet manifests. I had 3 lists of ip addresses, but wanted to join all 3 lists together into a single list to provide all ips to a template. I found some good tricks for flattening nested arrays in an erb –http://weblog.etherized.com/posts/175 — but I found those solutions to be too “magic” and hard to read.I ended up settling on using an inline_template and split, like so:

    $all_ips = split(inline_template("<%= (worker_ips+entry_point_ips+master_ips).join(',') %>"),',')

  2. Testing Puppet Locally
    To debug things like this, I like to use puppet locally to test a configuration by writing a test.pp file and passing that into puppet, e.g.:File: arrays-test.pp
    define tell_me() { notify{$name:}}

    $worker_ips = [ "192.168.0.1",
    "192.168.0.2",
    ]
    $entry_point_ips = ["10.0.0.1",
    "10.0.0.2",
    ]
    $master_ips = [ "192.168.15.1",
    ]

    $all_ips = split(inline_template("<%= (worker_ips+entry_point_ips+master_ips).join(',') %>"),',')
    tell_me{$all_ips:}

    Running puppet:

    $ puppet arrays-test.pp
    notice: 192.168.0.2
    notice: /Stage[main]//Tell_me[192.168.0.2]/Notify[192.168.0.2]/message: defined 'message' as '192.168.0.2'
    notice: 192.168.15.1
    notice: /Stage[main]//Tell_me[192.168.15.1]/Notify[192.168.15.1]/message: defined 'message' as '192.168.15.1'
    notice: 192.168.0.1
    notice: /Stage[main]//Tell_me[192.168.0.1]/Notify[192.168.0.1]/message: defined 'message' as '192.168.0.1'
    notice: 10.0.0.1
    notice: /Stage[main]//Tell_me[10.0.0.1]/Notify[10.0.0.1]/message: defined 'message' as '10.0.0.1'
    notice: 10.0.0.2
    notice: /Stage[main]//Tell_me[10.0.0.2]/Notify[10.0.0.2]/message: defined 'message' as '10.0.0.2'

    Note that for these kinds of tests, I like to use notify rather than building files.

This entry was posted in Linux and tagged . Bookmark the permalink.

5 Responses to two puppet tricks: combining arrays and local tests

  1. Roman says:

    Thanks Joe,

    This trick proved to be helpful indeed. For some reason, I could not make any other method work for me – I needed to combine two arrays with the list of groups a user should belong to, and these did not work:
    1. $groups = [ $init_groups , $add_groups ]
    - complains about a String not being able to be converted into an Array type
    2. $groups = inline_template("")
    – ends up in a concatenated string combined from array members

    Your method worked wonderfully:
    $groups = split(inline_template(""),',')

  2. Pingback: ECAE — Shopex电子商务云的梦想空间 » [Denny] puppet: 强大的中心化配置管理系统 — SA的利器

  3. Pingback: How to merge puppet array variables - Admins Goodies

  4. Deimosfr says:

    Thanks, it helped me :-)

  5. Callum says:

    A simpler option was posted on etherized.com which uses:
    (array1+array2+arrayX).flatten.join(',')

    Thanks for posting this, helped me out in a bind, the solution of using inline_template works well in my case. :-)

Leave a Reply