How to copy files to all machines using puppet master?

48,779

Puppet is a bit of a monster to get your head around, so learning by example is no bad thing. In the below I'm assuming you're using modules - please say in a comment if you're not or if you need more details about how to put the module together.

Let's say you start a new module called mymodule. In the puppet home directory (usually /etc/puppet) on the puppet master you should create the module manifests and files directory:

mkdir -p modules/mymodule/manifests
mkdir -p modules/mymodule/files

Then create a file in that directory named init.pp and enter:

class myfile {
    file { '/home/operator1/Desktop/Backup':
        ensure => directory,
        mode => '0755',
        owner => 'operator1',
        group => 'operator1',
    }

    file { "/home/operator1/Desktop/Backup/datas.xls":
        mode => "0644",
        owner => 'operator1',
        group => 'operator1',
        source => 'puppet:///modules/module_name/datas.xls',
    }
}

Then put the datas.xls file into the module's files directory - in this example in modules/mymodule/files/. (Note there can also be a templates directory for templates).

In the manifests/site.pp file you need to import the module and include the class by doing something like:

import 'mymodule'

node base {
    include myfile
}

node server1 inherits base {}
node server2 inherits base {
    # extra config here
}

Make sure that all your nodes inherit from base and that should be all you need to do. As of puppet 0.25 you can use regular expressions in the node name, eg:

node server[0-9] inherits base {}

Let me know in comments if you require further clarification.

Setting up a client to talk to the puppet master

On the client, you need to do:

sudo apt-get install puppet

Then edit /etc/default/puppet and change START=no to START=yes.

Also edit /etc/puppet/puppet.conf and add a line to the [main] section to tell it where to find the puppet master:

server=puppet.mydomain.com

Then we can do a test run with sudo puppetd --test. If you get key errors you may need to go on to the puppet master server and sign the client key. To check the exact name you can do sudo puppetca --list and then sudo puppetca --sign server1.mydomain.com (or whatever the server name was from the list command).

Now start the puppet service with sudo service puppet start and you should be away. The puppet service will run every hour, so if you update your puppet recipes then all your clients will also be updated.

Deleting Files

I note in the original question you wanted to know how to delete files. You would edit the manifests/init.pp to be

file { "/home/operator1/Desktop/Backup/datas.xls":
    ensure => absent,
}

Other useful tips

If you are having trouble there are a few things you can do. On any machine with puppet installed you can check your syntax by running

puppet --parseonly --ignoreimport myfile.pp

or check the whole lot by taking out the --ignoreimport flag, though that can lead to some funny error messages that aren't really errors I've found. You can also run puppet live on a puppet client machine by doing:

sudo puppetd --test

which shows various useful output, with errors and warnings highlighted in different colours. If you want even more detail you could run:

sudo puppetd --test --debug

but that generally generates so much output that it is hard to wade through, so only do that if you've already tried the previous steps and are stuck and need to see everything being done.

Note this is based on puppet 0.25.x which is what I use at work currently, and is also the version in Ubuntu 10.04. The puppet code in the main section will definitely still work, but later versions of puppet have new flags that can help with debugging output.

Share:
48,779

Related videos on Youtube

karthick87
Author by

karthick87

Updated on September 18, 2022

Comments

  • karthick87
    karthick87 over 1 year

    I have installed puppetmaster in ubuntu 11.04 and i have installed puppet in all my clients. I have joined all my puppet clients with puppet master. Is it possible to copy a file to all these puppet clients from puppet master?

    For example:

    I have the file named datas.xls in my Desktop (Puppet Master). How do i copy this files to all my puppet clients in the following location ( /home/operator1/Desktop/Backup/) ?

    Update:

    • Hi still the file is not getting shared.
    • How to modify this line puppet:///modules/module_name/datas.xls the file to be copied is under this location /etc/puppet/modules/mymodule/manifests/datas.xls ?

    Error on Client: (Resolved)

    root@testing:~# puppetd --test 
    err: Could not retrieve catalog from remote server: Error 400 on SERVER: Could not find class sudo at /etc/puppet/manifests/site.pp:2 on node testing.chn.jd.com
    warning: Not using cache on failed catalog
    err: Could not retrieve catalog; skipping run
    

    Error 2 on Client:

    root@tme13:~# puppetd --test
    err: Could not run Puppet configuration client: Could not retrieve local facts: bad URI(is not URI?): http://169.254.169.254/2008-02-01/meta-data/<HTML><HEAD><TITLE>HTTP access denied</TITLE></HEAD><BODY><img src/
    

    New Update:

    How to apply this module to all nodes? Such that the file will be copied to all nodes.

    • nilsonneto
      nilsonneto over 12 years
      ... but didnt you already ask this question? askubuntu.com/questions/61080/…
    • karthick87
      karthick87 over 12 years
      Yes but the solution is not working anymore.
    • karthick87
      karthick87 over 12 years
      Also it is not very clear, the user just copied the content from some websites.
    • Rinzwind
      Rinzwind over 12 years
      Still not a reason to make a new question(?) btw I copied that one from a link Jorge gave me and he told me to c/p it ;) afaik what is in that answer still works!?
    • Rinzwind
      Rinzwind over 12 years
      here's another reference: serverfault.com/questions/200101/…
    • karthick87
      karthick87 over 12 years
      Hi @Rinzwind see i m just a beginner in using puppet. I just need some neat and exact solution for my question. I have referred n number of links.
    • Hamish Downer
      Hamish Downer over 12 years
      You say the solution is not working any more - could you add the old solution to the question so we can see why it isn't working.
    • Hamish Downer
      Hamish Downer over 12 years
      The error you've added is due to line 2 of your site.pp file referencing a class called sudo that puppet can't find. You need to define the sudo class or get rid of the reference to it.
    • karthick87
      karthick87 over 12 years
      Thankyou @HamishDowner i have solved it. New error arised in client. Have a look @ it and give me a solution pls.
    • Hamish Downer
      Hamish Downer over 12 years
      @karthick87: I don't have enough information to deal with that. Might be time for a new question. I would also recommend putting the output of puppetd --test --debug in that question. Might also need the local config - /etc/default/puppet and any files under /etc/puppet/
    • karthick87
      karthick87 over 12 years
      @HamishDowner pls see the new update.
    • Hamish Downer
      Hamish Downer over 12 years
      @karthick87: I have added more info. I would say at this point that if you need help with specifics it would be better to have a series of different questions rather than trying to get one monster answer. If you post links to future questions in comments here I'll find them and answer them.
  • karthick87
    karthick87 over 12 years
    Let me try this solution tonight, and let you know the updates. Thanks a lot :)
  • karthick87
    karthick87 over 12 years
    How to modify this line puppet:///modules/module_name/datas.xls the file to be copied is under this location /etc/puppet/modules/mymodule/manifests/datas.xls
  • karthick87
    karthick87 over 12 years
    Oke thank you,. how to run the manifests?
  • karthick87
    karthick87 over 12 years
    How to apply this module to all nodes from puppetmaster? Instead of applying it to every node forcefully by running puppet agent --test
  • Hamish Downer
    Hamish Downer over 12 years
    @karthick87 - just make sure the puppet service is running on each node - sudo service puppet start - and it should run puppet within the hour. Then it will continue running, and if you added a second file to your config later, then it would be copied to every node without you having to do anything.