Exclude an element from array

1,070 views
Skip to first unread message

Vikas Kumar

unread,
Oct 10, 2015, 11:47:41 AM10/10/15
to Puppet Users
Hello Everyone,

I have a very basic code to stop and disable few services which I am running for CentOS/RHEL 6/7 servers.


$stop_services = [ "ip6tables", "iptables", "auditd", "cups" ]

service
{ $stop_services
   
ensure => stopped
   enable
=> false
}


Is it possible to not run this code when the OS is CentOS/RHEL 7 and the stop_services value is auditd.


Regards,

Vikas

Julian Meier

unread,
Oct 10, 2015, 11:00:42 PM10/10/15
to puppet...@googlegroups.com
Hi!


Untested Example:
```
if ($::osfamily != 'RedHat') and (! member($stop_services, 'auditd')) {
  service {...}
}
```

Perhaps you can use the fact $::operatingsystemrelease or $::operatingsystemmajrelease to check if it's RedHat or CentOS 7.

Julian
--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/8f5a3e93-6a4e-4875-a4dc-cda501d4e5ec%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Henrik Lindberg

unread,
Oct 11, 2015, 1:57:14 AM10/11/15
to puppet...@googlegroups.com
On 2015-10-10 4:47, Vikas Kumar wrote:
> Hello Everyone,
>
> I have a very basic code to stop and disable few services which I am
> running for CentOS/RHEL 6/7 servers.
>
>
> |
> $stop_services =["ip6tables","iptables","auditd","cups"]
>
> service {$stop_services
> ensure=>stopped
> enable =>false
> }
> |
>
>
> Is it possible to not run this code when the OS is CentOS/RHEL 7 and the
> stop_services value is auditd.
>
>

Here are a couple of ways to do this (all untested):

If you are on 3.x and using the future parser, or using 4.x you can add
and remove from arrays directly in the language with '+' and '-'. If on
earlier versions you need to use the 'delete' function from stdlib.

# for 3.x (no future parser)
#
$stop_services = ["ip6tables","iptables","auditd","cups"]
if $::osfamily == 'RedHat {
$excluded_services = ['auditd']
}
else {
$excluded_services = []
}
$services_to_stop = delete($stop_services, $excluded_services)

service { $services_to_stop:
ensure=>stopped,
enable =>false,
}

With the 4.x (3.x future) parser you can also use a case expression to
produce a value (which may be more convenient if you will have more than
a single special case.

$stop_services = ["ip6tables","iptables","auditd","cups"]
$excluded_services = case $::osfamily {
'RedHat': { 'auditd' }
default: { }
}
service { $stop_services - $excluded_services :
ensure=>stopped,
enable =>false,
}

Or you can iterate:

# using conditional logic
#
["ip6tables","iptables","auditd","cups"].each |$service| {
unless $::osfamily == 'RedHat' and $service == 'auditd' {
service { $service :
ensure => stopped,
enable => false,
}
}
}


# using 'filter'
#
["ip6tables", "iptables", "auditd", "cups"].filter |$service| {
$::osfamily != 'RedHat' and $service != 'auditd'
}.each |$service| {
service { $service :
ensure => stopped,
enable => false,
}
}

Regards
- henrik

--

Visit my Blog "Puppet on the Edge"
http://puppet-on-the-edge.blogspot.se/

Vikas Kumar

unread,
Oct 11, 2015, 2:42:50 AM10/11/15
to Puppet Users
Hello Henrik,

Thanks for your time on this.This is my final code on puppet v4, hope it helps someone.

case $::operatingsystem {
 
'RedHat', 'CentOS' : {

    $stop_services
= [ "ip6tables", "iptables", "auditd", "cups" ]



    $exclude_services
= $::osfamily ? {
     
'RedHat' => [ 'auditd' ]
   
}


   
if ($::operatingsystemmajrelease == '6') {
      service
{ $stop_services:

       
ensure => stopped,
        enable
=> false,
     
}
   
}

   
if ($::operatingsystemmajrelease == '7') {
      service
{ $stop_services - $exclude_services:

       
ensure => stopped,
        enable
=> false,
     
}
   
}
 
}
}

Regards,
Vikas

Dan White

unread,
Oct 11, 2015, 2:43:22 AM10/11/15
to puppet...@googlegroups.com
Has anyone considered taking the array of services out of the code and putting it into Hiera ?
Much easier to vary the array from there.
> --
> You received this message because you are subscribed to the Google Groups "Puppet Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/mvcfki%248no%241%40ger.gmane.org.

Vikas Kumar

unread,
Oct 11, 2015, 2:46:00 AM10/11/15
to Puppet Users
Hello Dan,

Would it be possible for you to point me to an example (may be a link) ? Thanks in advance.

Regards,
Vikas

Dan White

unread,
Oct 12, 2015, 12:32:46 PM10/12/15
to puppet...@googlegroups.com
The puppet manifest would simplify to :
class foo (
$services_to_stop = [],
) {

include 'stdlib'

if size($services_to_stop) > 0 {
service { $services_to_stop :
ensure => stopped,
enable => false,
}
}
}

You need to set up hiera (http://docs.puppetlabs.com/hiera/1/) to let you choose settings using the "operatingsystem", "osfamily", and/or "operatingsystemmajrelease" facts.
Personally, I am a bit confused as to what services you want stopped under what conditions, so I cannot offer more detail.
The hiera data for your default case would be :
---
foo::services_to_stop :
  - ip6tables
  - iptables
  - auditd
  - cups

“Sometimes I think the surest sign that intelligent life exists elsewhere in the universe is that none of it has tried to contact us.”  (Bill Waterson: Calvin & Hobbes)

Vikas Kumar

unread,
Oct 12, 2015, 1:18:19 PM10/12/15
to Puppet Users
Hi Dan,

Thanks for hint. I am already using hiera, yes the code looks much cleaner when using it.

Regards,
Vikas
Reply all
Reply to author
Forward
0 new messages