Ask Your Question

One liner for access split output?

asked 2015-02-18 14:11:47 -0500

Red Cricket gravatar image

Is there a better way to write this sort of stuff:

    $allnics = split( $interfaces, "," )
    $first_nic = $allnics[0]
    notify {"this system has interfaces $interfaces":}
    notify {"the first nic is $first_nic":}

I tried ...

   $first_nic = split( $interfaces, ",")[0]

... but got a syntax error.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2015-02-18 14:19:53 -0500

GregLarkin gravatar image

Unfortunately, no. You will have to assign the result of a function call to a variable before indexing into it due to the syntax of the Puppet DSL.

Because of the code that you posted above, can I assume that you are trying to loop through the interfaces and create a resource for each one? If so, be aware that Puppet does not work the same as typical imperative programming languages, and that was a big leap for me when I was learning it.

Instead, you can pass a structure that you've built into the create_resources function to declare many of the same type of resource at once. Here is a good example for how to do that:

Of course, if I've read too much into your question, please forgive me! :)

edit flag offensive delete link more


Thank you for your answer. You may be reading a little too much in but that's because I left something out. I am finding that in RHEL7 the interface names have changed and appear to be inconsistent. I am assuming that first element in the split output is the one I want, but that could be wrong.

Red Cricket gravatar imageRed Cricket ( 2015-02-18 15:28:05 -0500 )edit

Since I don't have access to a RHEL7 machine at the moment, what is a typical value for $interfaces? On a RHEL6 machine, you might see "eth0,eth1,lo", for instance.

GregLarkin gravatar imageGregLarkin ( 2015-02-18 17:42:52 -0500 )edit

Yes that is what I see on RHEL6 too. I am wondering if that is merely alphabetical or if puppet know which interfaces are considered "primary" or "secondary"?

Red Cricket gravatar imageRed Cricket ( 2015-02-18 20:44:33 -0500 )edit

Looking at the source code for the interfaces fact, it appears that the interface order will be the same as what is emitted by "ifconfig -a". Based on, I think "ifconfig -a" is also called on RHEL7, even though it's deprecated.

GregLarkin gravatar imageGregLarkin ( 2015-02-19 00:48:56 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2015-02-18 14:11:47 -0500

Seen: 203 times

Last updated: Feb 18 '15