Code Monkey home page Code Monkey logo

puppet-augeasproviders_shellvar's Introduction

Build Status Release Puppet Forge Puppet Forge - downloads Puppet Forge - endorsement Puppet Forge - scores puppetmodule.info docs Coverage Status Apache-2 License

shellvar: type/provider for shell files for Puppet

This module provides a new type/provider for Puppet to read and modify shell config files using the Augeas configuration library.

The advantage of using Augeas over the default Puppet parsedfile implementations is that Augeas will go to great lengths to preserve file formatting and comments, while also failing safely when needed.

This provider will hide all of the Augeas commands etc., you don't need to know anything about Augeas to make use of it.

Requirements

Ensure both Augeas and ruby-augeas 0.3.0+ bindings are installed and working as normal.

See Puppet/Augeas pre-requisites.

Installing

The module can be installed easily (documentation):

puppet module install puppet/augeasproviders_shellvar

Documentation and examples

Type documentation can be generated with puppet doc -r type or viewed on the Puppet Forge page.

manage simple entry

shellvar { "HOSTNAME":
  ensure => present,
  target => "/etc/sysconfig/network",
  value  => "host.example.com",
}

shellvar { "disable rsyncd":
  ensure   => present,
  target   => "/etc/default/rsync",
  variable => "RSYNC_ENABLE",
  value    => "false",
}

shellvar { "ntpd options":
  ensure   => present,
  target   => "/etc/sysconfig/ntpd",
  variable => "OPTIONS",
  value    => "-g -x -c /etc/myntp.conf",
}

manage entry with comment

shellvar { "HOSTNAME":
  ensure  => present,
  target  => "/etc/sysconfig/network",
  comment => "My server's hostname",
  value   => "host.example.com",
}

export values

shellvar { "HOSTNAME":
  ensure  => exported,
  target  => "/etc/sysconfig/network",
  value   => "host.example.com",
}

unset values

shellvar { "HOSTNAME":
  ensure  => unset,
  target  => "/etc/sysconfig/network",
}

force quoting style

Values needing quotes will automatically get them, but they can also be explicitly enabled. Unfortunately the provider doesn't help with quoting the values themselves.

shellvar { "RSYNC_IONICE":
  ensure   => present,
  target   => "/etc/default/rsync",
  value    => "-c3",
  quoted   => "single",
}

delete entry

shellvar { "RSYNC_IONICE":
  ensure => absent,
  target => "/etc/default/rsync",
}

remove comment from entry

shellvar { "HOSTNAME":
  ensure  => present,
  target  => "/etc/sysconfig/network",
  comment => "",
}

replace commented value with entry

shellvar { "HOSTNAME":
  ensure    => present,
  target    => "/etc/sysconfig/network",
  value     => "host.example.com",
  uncomment => true,
}

uncomment a value

shellvar { "HOSTNAME":
  ensure    => present,
  target    => "/etc/sysconfig/network",
  uncomment => true,
}

array values

You can pass array values to the type.

There are two ways of rendering array values, and the behavior is set using the array_type parameter. array_type takes three possible values:

  • auto (default): detects the type of the existing variable, defaults to string;
  • string: renders the array as a string, with a space as element separator;
  • array: renders the array as a shell array.

For example:

shellvar { "PORTS":
  ensure     => present,
  target     => "/etc/default/puppetmaster",
  value      => ["18140", "18141", "18142"],
  array_type => "auto",
}

will create PORTS="18140 18141 18142" by default, and will change PORTS=(123) to PORTS=("18140" "18141" "18142").

shellvar { "PORTS":
  ensure     => present,
  target     => "/etc/default/puppetmaster",
  value      => ["18140", "18141", "18142"],
  array_type => "string",
}

will create PORTS="18140 18141 18142" by default, and will change PORTS=(123) to PORTS="18140 18141 18142".

shellvar { "PORTS":
  ensure     => present,
  target     => "/etc/default/puppetmaster",
  value      => ["18140", "18141", "18142"],
  array_type => "array",
}

will create PORTS=("18140" "18141" "18142") by default, and will change PORTS=123 to PORTS=(18140 18141 18142).

Quoting is honored for arrays:

  • When using the string behavior, quoting is global to the string;
  • When using the array behavior, each value in the array is quoted as requested.

appending to arrays

shellvar { "GRUB_CMDLINE_LINUX":
  ensure       => present,
  target       => "/etc/default/grub",
  value        => "cgroup_enable=memory",
  array_append => true,
}

will change GRUB_CMDLINE_LINUX="quiet splash" to GRUB_CMDLINE_LINUX="quiet splash cgroup_enable=memory".

shellvar { "GRUB_CMDLINE_LINUX":
  ensure       => present,
  target       => "/etc/default/grub",
  value        => ["quiet", "cgroup_enable=memory"],
  array_append => true,
}

will also change GRUB_CMDLINE_LINUX="quiet splash" to GRUB_CMDLINE_LINUX="quiet splash cgroup_enable=memory".

removing from arrays

shellvar { "GRUB_CMDLINE_LINUX":
  ensure       => absent,
  target       => "/etc/default/grub",
  value        => "cgroup_enable=memory",
  array_append => true,
}

will change GRUB_CMDLINE_LINUX="quiet splash cgroup_enable=memory" to GRUB_CMDLINE_LINUX="quiet splash".

shellvar { "GRUB_CMDLINE_LINUX":
  ensure       => absent,
  target       => "/etc/default/grub",
  value        => ["quiet", "cgroup_enable=memory"],
  array_append => true,
}

will also change GRUB_CMDLINE_LINUX="splash cgroup_enable=memory" to GRUB_CMDLINE_LINUX="splash".

Issues

Please file any issues or suggestions on GitHub.

puppet-augeasproviders_shellvar's People

Contributors

bastelfreak avatar domcleal avatar ekohl avatar igalic avatar jeannegreulich avatar jmgreu avatar maxadamo avatar p6wg7 avatar raphink avatar ruleant avatar smortex avatar speer avatar trevor-vaughan avatar treydock avatar tuxmea avatar zilchms avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

puppet-augeasproviders_shellvar's Issues

Error with Puppet 5.3.3

Getting this error with Puppet 5.3.3 and latest version of this module

Mar 12 10:26:33 puppet-test puppet-agent[73542]: Could not retrieve catalog from remote server: Error 500 on SERVER: Server Error: Could not autoload puppet/type/shellvar: Attempt to redefine entity 'http://puppet.com/2016.1/runtime/type/shellvar'. Set at unknown location on node puppet-test.ten.osc.edu

I've run into this before with the puppet/splunk module: voxpupuli/puppet-splunk#156

Exporting array value fails

# /tmp/foo
FOO=("bar")
shellvar { 'FOO':
  ensure => exported,
  target => '/tmp/foo',
}

yields:

/augeas/files/tmp/foo/error/message = Failed to match 
    { /export/ }?(() | { /[0123456789]+/ = /(([^\001-\004\t\n "'-);`]|\\\\")+|("([^\001-\004"\\]|\\\\[^\001-\004\n]|[\t ]*\\\\\n[\t ]*)*"|'[^\001-\004']*')+|`[^\001-\004\n`]*`|``[^\001-\004\t\n ;`]+``|\\$\\([^\001-\004\n#)\\]*\\))/ }{ /[0123456789]+/ = /(([^\001-\004\t\n "'-);`]|\\\\")+|("([^\001-\004"\\]|\\\\[^\001-\004\n]|[\t ]*\\\\\n[\t ]*)*"|'[^\001-\004']*')+|`[^\001-\004\n`]*`|``[^\001-\004\t\n ;`]+``|\\$\\([^\001-\004\n#)\\]*\\))/ }*)({ /#comment/ = /[^\001-\004\t\n\r ][^\001-\004\n]*[^\001-\004\t\n\r ]|[^\001-\004\t\n\r ]/ } | ())
  with tree
    { "1" = ""bar"" } { "export" }
Error: /Stage[main]/Main/Shellvar[FOO]: Could not evaluate: Failed to save Augeas tree to file. See debug logs for details.

meaning the "export" node is added after the "1" node, when it should be inserted before.

Bulk updates requires a lot of studdering

Problem: If one has to set a lot of variables in many files (for example, using each()), you have to repeat yourself a lot.

For example, this is the cleanest I've been able to write for setting many variables.

  Shellvar {
    ensure => present,
    target => $ifile,
  }

  shellvar { "${name} DEVICE": variable => 'DEVICE', value  => $name}
  shellvar { "${name} NAME": variable => 'NAME', value  => $name}
  shellvar { "${name} HWADDR": variable => 'HWADDR', value => $hwaddress}
  shellvar { "${name} TEAM_MASTER": variable => 'TEAM_MASTER', value => $team_master}
  shellvar { "${name} DEVICETYPE": variable => 'DEVICETYPE', value  => 'TeamPort'}
  shellvar { "${name} ONBOOT": variable => "TYPE", value => 'yes'}
  shellvar { "${name} TYPE": variable => 'TYPE', value => 'Ethernet'}
  shellvar { "${name} NM_CONTROLLED": value => 'NM_CONTROLLED', variable => 'no'}

There are a few problems with this:

  • The namevar repeats the variable name, which is error prone. They may get out of sync.
  • Specifying variable => and value => again and again is very verbose. It hides meaning.
  • It is slow. Each one of these will require starting up augeas, parsing the file, etc.
  • You didn't notice that "ONBOOT" sets the wrong variable, or that NM_CONTROLLED reversed the value and the variable.

Proposed Solution: For mass updates, it would be more natural to have a function that takes a filename and a list of settings:

  shellvar_bulk( $ifile, [
          "DEVICE=$name", 
          "NAME=$name", 
          "HWADDR=$hwaddress", 
          "TEAM_MASTER=$TEAM_MASTER",
          'DEVICETYPE=TeamPort',
          'ONBOOT=yes',
          'TYPE=Ethernet',
          'NM_CONTROLLED=no',
     ])

support ensure => sourced

It would be useful to have ensure => sourced for adding parsed files in configuration files:

shellvar { "Source my other conffile":
  ensure   => sourced,
  variable => '/path/to/variables.env',
}

shellvar doesn't work properly for export

Want to edit below line in puppet

/opt/dtp/etc/dtp.conf
export CHO='NON-PROD'

Used shellvar resource, please find the snippet below

shellvar { 'CHO':
  ensure => exported,
  target => '/opt/dtp/etc/dtp_config',
  value  => 'NON-PROD',
}

when I run first time it's removing the word "export", got o/p like javascriptDTP="NON-PROD"

If I run second time, getting below o/p
javascript"Notice: /Stage[main]/Example::Dtp/Shellvar[DTP]/ensure: ensure changed 'present' to 'exported'
and the o/p is export DTP="NON-PROD

please refer the below url
http://stackoverflow.com/questions/33168034/unable-to-print-augeas-for-own-configuration

Cannot use both comment = "" and uncomment = true

As discussed on with raphik on IRC, I can use one of them but not both at the same:

08:51 < raphink> the problem seems to come from comment => ""
08:52 < raphink> shellvar { 'TMPTIME': ensure => present, target => "/tmp/rcS", value => "42", }
08:52 < raphink> and
08:52 < raphink> shellvar { 'TMPTIME': ensure => present, target => "/tmp/rcS", value => "42", uncomment => true }
08:52 < raphink> both work
08:52 < raphink> but comment => "" doesn't
08:52 < acecile> I saw that on the official documentation
08:52 < acecile> I guess
08:52 < acecile> :D
08:52 < raphink> you can open a bug against augeasproviders_ssh for that
08:52 -!- dcaro|not_here is now known as dcaro
08:52 < raphink> yes, it's used to remove the comment associated with the entry
08:53 < raphink> shellvar { 'TMPTIME': ensure => present, target => "/tmp/rcS", value => "42", comment => "" }
08:53 < raphink> works as well
08:53 < raphink> but not the combination of specifying uncomment (to either false or true) and comment
08:54 < raphink> sorry, meant augeasproviders_shellvar for the bug report

rspec Unknown resource type: 'shellvar'

Hello,

I'm trying to add specs using rspec-puppet for a very simple module.

When I run rake rspec, I get this error:

 1) lldp should compile into a catalogue without dependency cycles
     Failure/Error: it { is_expected.to compile }
       error during compilation: Evaluation Error: Error while evaluating a Resource Statement, Unknown resource type: 'shellvar' at /Users/xnicollet/stack/git/gitlab/sre/puppet/modules/lldp/spec/fixtures/modules/lldp/manifests/init.pp:50:3 on node rem-xnicollet-mbp.ds.stackexchange.com
     # ./spec/classes/lldp_spec.rb:4:in `block (2 levels) in <top (required)>'

This is the lldp module:

class lldp (
  String $options = '-c -I "*,!team*"',
){
  package { 'lldpd':
    ensure => latest,
  }

  shellvar { 'options':
    ensure   => present,
    target   => '/etc/sysconfig/lldpd',
    quoted   => 'double',
    variable => 'LLDPD_OPTIONS',
    value    => $options,
  }

  $sname = 'lldpd.service'
  service { $sname:
    ensure   => running,
    enable   => true,
    provider => systemd,
  }

  Package['lldpd']->Shellvar['options']~>Service[$sname]
}

This is the rspec file lldp_spec.rb:

require 'spec_helper'

describe 'lldp' do
        it { is_expected.to compile }
end

Thank you for your help!

undefined method `split' for nil:NilClass when file contains managed variable name in comments with no assignment

Overview

If a variable to be managed exists only in the comments does not include an assignment, then the user will receive a ruby error and will not be able to manage the target file using the provider.

Expected result

Expected to be able to insert shell variables as required without error, irrespective of commented out entries

Actual result

Ruby Error:

Error: Could not set 'present' on ensure: undefined method `split' for nil:NilClass at 7:/vagrant/test.pp
Error: Could not set 'present' on ensure: undefined method `split' for nil:NilClass at 7:/vagrant/test.pp
Wrapped exception:
undefined method `split' for nil:NilClass
Error: /Stage[main]/Main/Shellvar[/tmp/test.sh UMASK]/ensure: change from absent to present failed: Could not set 'present' on ensure: undefined method `split' for nil:NilClass at 7:/vagrant/test.pp

Testcase

The following testcase reproduces the error:

$target = "/tmp/test.sh"
file { $target:
  ensure => file,
  content => "# UMASK sets the initial shell file creation mode mask.  See umask(1)."
}

shellvar {"${target} UMASK":
  ensure   => present,
  variable => "UMASK",
  value    => "0770",
  target   => $target,
  require  => File[$target],
}

NOTE: testcase will reset itself every run because of the file resource

Shellvar doesn't work on Debian Squeeze

Upgraded from dom/augeasprov 1.2 to the last 2.1.3/2.1.1 , and one of the last system we have with a squeeze doesn't like puppet anymore, with this in the conf:

shellvar { 'apticron-system':
        ensure => 'present',
        target => '/etc/apticron/apticron.conf',
        variable => 'SYSTEM',
        value => "$::fqdn",
        require => Package['apticron'],
}

Running puppet agent on the target machine returns this:

err: Could not run Puppet configuration client: Resource type shellvar does not support parameter false

No more informations when adding -d and -v; it works fine when the shellvar stuff is commented.

shellvar crashes

Shellvar crashes when trying to create a new variable line and a comment for it exists that has no value.
I am tring to update the value for CRYPTO_POLICY in /etc/sysconfig/sshd that is installed with EL8. It has
the line
"# CRYPTO_POLICY="

And when the provider reads in the comment it crashes because it tries to do a split on the value and the value =nil.
There does not seem to be a way to skip the reading of the comment.

problem with exclude in yum.conf

I try to use shellvar to set excludes in /etc/yum.conf

shellvar{'exclude':
ensure=>present,
target=>'/etc/yum.conf',
value=>'mongodb-org*'
}

Puppet run error

Error: /Stage[main]/Mongodb::Install/Shellvar[exclude]: Could not evaluate: Augeas didn't load /etc/yum.conf with Shellvars.lns from /var/lib/puppet/lib/augeas/lenses: Syntax error (line:1, character:0)

Cannot alias Shellvar, resource already declared

The following code works fine with version 2.2.2, but fails with catalog-compilation error on version 2.2.3.

  exec { 'update grub':
    command     => '/usr/sbin/update-grub',
    refreshonly => true,
  }

  shellvar { 'default to noop elevator':
    ensure       => present,
    target       => '/etc/default/grub',
    variable     => 'GRUB_CMDLINE_LINUX',
    value        => 'elevator=noop',
    array_append => true,
    notify       => Exec['update grub'],
  }

  if ($::kernel == 'Linux' and $::virtual == 'kvm') {
    shellvar { 'configure serial console':
      ensure       => present,
      target       => '/etc/default/grub',
      variable     => 'GRUB_CMDLINE_LINUX',
      value        => 'console=ttyS0,115200n8',
      array_append => true,
      notify       => Exec['update grub'],
    }
  }

Error message:

Error: Could not retrieve catalog from remote server:
Error 500 on SERVER: Server Error: Evaluation Error: Error while evaluating a Resource Statement,
Cannot alias Shellvar[configure serial console] to ["/etc/default/grub", "GRUB_CMDLINE_LINUX"] at
[[second resource definition]];
resource ["Shellvar", "/etc/default/grub", "GRUB_CMDLINE_LINUX"] already declared at
[[first resource definition]] at
[[second resource definition]]
on node [[detracted]]
Warning: Not using cache on failed catalog

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.