Managing Files via Puppet Let Me Count The Ways Mike Arnold ( - - PowerPoint PPT Presentation

managing files via puppet
SMART_READER_LITE
LIVE PREVIEW

Managing Files via Puppet Let Me Count The Ways Mike Arnold ( - - PowerPoint PPT Presentation

Managing Files via Puppet Let Me Count The Ways Mike Arnold ( razorsedge ) Puppet Camp Phoenix 2015 Agenda Intro Managing Files Managing Lines Various Patterns Questions Links Intro Who is Mike Arnold? Unix Systems Administrator


slide-1
SLIDE 1

Managing Files via Puppet

Let Me Count The Ways

Mike Arnold ( )

Puppet Camp Phoenix 2015

razorsedge

slide-2
SLIDE 2

Agenda

Intro Managing Files Managing Lines Various Patterns Questions Links

slide-3
SLIDE 3

Intro

slide-4
SLIDE 4

Who is Mike Arnold?

Unix Systems Administrator Puppet Certified Professional 15 years in IT Presently doing: Hadoop Infrastructure Engineering Building Puppet modules

slide-5
SLIDE 5

What Is This Topic?

Puppet can manage files. (This may be obvious.) entire files

  • r just lines

via static content

  • r templates

Lets see all the ways.

slide-6
SLIDE 6

Managing Files

slide-7
SLIDE 7

Basic File resource:

file { '/tmp/testFile': ensure => present, mode => '0644',

  • wner => 'root',

group => 'root', }

slide-8
SLIDE 8

With source attribute:

file { '/tmp/testFileA': ensure => present, mode => '0644', source => 'puppet:///modules/example/fileA', }

slide-9
SLIDE 9

With multiple source attributes:

file { '/tmp/testFileB': ensure => present, mode => '0644', source => [ "puppet:///modules/example/fileB.${::operatingsystem}", 'puppet:///modules/example/fileB', ], }

slide-10
SLIDE 10

With content attribute:

file { '/tmp/testFileC': ensure => present, mode => '0644', content => 'Some fancy string.', }

Note: no carriage return

slide-11
SLIDE 11

With content attribute string variables:

file { '/tmp/testFileD': ensure => present, mode => '0644', content => "Your operating system is: ${::operatingsystem}\ ${::operatingsystemrelease}\nYour CPU architecture\ is: ${::architecture}\n", }

slide-12
SLIDE 12

With content attribute template():

$variableA = 'good' $variableB = 'horrible' file { '/tmp/testFileE': ensure => present, mode => '0644', content => template('example/templateA.erb'), } This is a <%= @variableA %> day. There will be a <%= @variableB %> calamity.

slide-13
SLIDE 13

With mutiple content attribute template()s:

$variableA = 'good' $variableB = 'horrible' file { '/tmp/testFileF': ensure => present, mode => '0644', content => template('example/templateA.erb','example/templateB.erb'), } We are using <%= scope['::osfamily'] %> osfamily.

slide-14
SLIDE 14

With content attribute inline_template():

$options = [ 'blue', 'heavy', 'yummy' ] $item = 'The sky is' file { '/tmp/testFileG': ensure => present, mode => '0644', content => inline_template("${item}: <%= @options.join ' ' %>\n"), }

slide-15
SLIDE 15

With content attribute file():

file { '/tmp/testFileH': ensure => present, mode => '0644', content => file('/etc/hosts'), }

slide-16
SLIDE 16

With content attribute epp():

$variableA = 'good' $variableB = 'horrible' file { '/tmp/testFileI': ensure => present, mode => '0644', content => epp('example/templateA.epp'), } This is a <%= $variableA %> day. There will be a <%= $variableB %> calamity.

puppet apply --parser=future

slide-17
SLIDE 17

Difference between source and content?

Unlike content, the source attribute can be used to recursively copy directories if the recurse attribute is set to true or remote.

slide-18
SLIDE 18

concat

The concat module constructs files from multiple fragments in an

  • rdered way.

concat { '/tmp/testFileJ': ensure => present, mode => '0644' } concat::fragment { 'testfileJ­01': target => '/tmp/testFileJ', content => "This is a concat line 01.\n",

  • rder => '01',

} concat::fragment { 'testfileJ­02': target => '/tmp/testFileJ', source => 'puppet:///modules/example/fileA',

  • rder => '02',

}

slide-19
SLIDE 19

Managing Lines

slide-20
SLIDE 20

augeas

Apply a change or an array of changes to the filesystem using the augeas tool.

file { '/tmp/testFileK': ensure => present, mode => '0644', content => file('/etc/resolv.conf'), } ­> augeas { 'testFileK' : incl => '/tmp/testFileK', # only needed for this demo lens => 'Resolv.lns', # only needed for this demo changes => 'set domain example.net', }

slide-21
SLIDE 21

file_line

The file_line resource ensures that a given line, including whitespace at the beginning and end, is contained within a file. If the line is not present, Puppet will add the line. Multiple resources can be declared to manage multiple lines in the same file.

file { '/tmp/testFileL': ensure => present, mode => '0644', } ­> file_line { 'testFileL': ensure => present, path => '/tmp/testFileL', line => 'This line shall be present.', }

slide-22
SLIDE 22

file_line with line replacement:

You can also use match to replace existing lines.

file { '/tmp/testFileM': ensure => present, mode => '0644', content => file('/etc/resolv.conf'), } ­> file_line { 'testFileM#search': ensure => present, path => '/tmp/testFileM', line => 'search localdomain', match => '^search .*', }

slide-23
SLIDE 23

inifile

The inifile module allows you to manage settings and subsettings in INI-style configuration files. This module tries hard not to manipulate your file any more than it needs to. In most cases, it should leave the original whitespace, comments, ordering, etc. intact.

ini_setting { 'testFileN#foo#bar': ensure => present, path => '/tmp/testFileN', section => 'foo', setting => 'bar', value => 'GIBBERISH', }

slide-24
SLIDE 24

inifile adding to an existing section:

file { '/tmp/testFileO': ensure => present, mode => '0644', content => file('/usr/share/perl5/vendor_perl/XML/SAX/ParserDetails.ini'), } ­> ini_setting { 'testFileO#foo#bar': ensure => present, path => '/tmp/testFileO', section => 'Build', setting => 'testN', value => 'WeDidIt', }

slide-25
SLIDE 25

datacat

The datacat module constructs a file by stitching line fragments together into the same line in one or multiple files.

datacat { '/tmp/testFileP': ensure => present, mode => '0644', template => 'example/templateP.erb', } datacat_fragment { "${::fqdn} in device hostgroup": target => '/tmp/testFileP', data => { myhostgroup => [ $::fqdn ], },

  • rder => '01',

} $ilo_fqdn = regsubst($::fqdn, '\.', '­ilo.') datacat_fragment { "${ilo_fqdn} in device hostgroup": target => '/tmp/testFileP', data => { myhostgroup => [ $ilo_fqdn ], },

  • rder => '02',

}

slide-26
SLIDE 26

The File and Concat resource can make backups of the file being modified into the Puppet filebucket. File_line, inifile, etc do not.

slide-27
SLIDE 27

Various Patterns

slide-28
SLIDE 28

sudo

class { 'sudo': # only needed for this demo purge => false, # only needed for this demo config_file_replace => false, # only needed for this demo } # only needed for this demo sudo::conf { 'web': source => 'puppet:///modules/example/etc/sudoers.d/web', } sudo::conf { 'admins': priority => 10, content => "%admins ALL=(ALL) NOPASSWD: ALL", } sudo::conf { 'joe': priority => 60, source => 'puppet:///modules/example/etc/sudoers.d/joe', }

slide-29
SLIDE 29

augeasproviders

sshd_config { 'PermitRootLogin': ensure => present, value => 'no', }

grep PermitRootLogin /etc/ssh/sshd_config

slide-30
SLIDE 30

augeasproviders

kernel_parameter { 'elevator': ensure => present, value => 'deadline', }

grep linux16 /boot/grub2/grub.cfg

slide-31
SLIDE 31

augeasproviders

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

cat /etc/sysconfig/network

slide-32
SLIDE 32

augeasproviders

sysctl { 'net.ipv4.ip_forward': ensure => present, value => '1', comment => 'This is a routing test.', }

sysctl net.ipv4.ip_forward cat /etc/sysctl.conf

slide-33
SLIDE 33

Apache vhost

apache::vhost { 'first.example.com': port => '80', docroot => '/var/www/first', }

cat /etc/httpd/conf.d/25-first.example.com.conf

slide-34
SLIDE 34

Questions?

slide-35
SLIDE 35

Links

https://docs.puppetlabs.com/references/latest/type.html#file- attribute-content https://docs.puppetlabs.com/references/latest/type.html#file- attribute-source https://docs.puppetlabs.com/references/latest/function.html#template https://docs.puppetlabs.com/references/latest/function.html#file https://docs.puppetlabs.com/references/latest/function.html#epp https://forge.puppetlabs.com/puppetlabs/concat

slide-36
SLIDE 36

Links

https://docs.puppetlabs.com/references/latest/type.html#augeas https://puppetlabs.com/blog/module-of-the-week- puppetlabsstdlib-puppet-labs-standard-library https://forge.puppetlabs.com/puppetlabs/stdlib https://forge.puppetlabs.com/puppetlabs/inifile https://forge.puppetlabs.com/richardc/datacat

slide-37
SLIDE 37

Links

https://forge.puppetlabs.com/saz/sudo https://forge.puppetlabs.com/herculesteam/augeasproviders https://forge.puppetlabs.com/puppetlabs/apache

slide-38
SLIDE 38

Contact

Mike Arnold <puppet@razorsedge.org> https://intelligentsysadmin.wordpress.com/ https://github.com/razorsedge https://forge.puppetlabs.com/razorsedge This presentation sourcecode can be found at:

https://github.com/razorsedge/presentation-managing-files-via-puppet