Installing a puppet module from a manifest script

后端 未结 8 2051
不思量自难忘°
不思量自难忘° 2020-12-12 15:27

I\'m using puppet to provision a vagrant (ubuntu based) virtual machine. In my script I need to:

sudo apt-get build-dep python-lxml

I know

相关标签:
8条回答
  • 2020-12-12 15:51

    Using answers from this question I created this script:

    #!/bin/bash
    
    function install_module {
       IFS=':' read module version <<< "$1"
       if (puppet module list | grep $module ) >/dev/null; then
        echo "Module $module is already installed"
       else
            if [ -z "$version" ]; then
                puppet module install $module
            else 
            puppet module install -v $version $module
        fi
        fi  
    }
    
    if dpkg --compare-versions `puppet --version` "lt" 3.8.7; then
        sudo apt-get remove --purge -y puppet
        sudo apt-get -y autoremove
    fi
    
    if which puppet >/dev/null; then
        echo "Puppet is already installed"
    else
        echo "Installing puppet"
        wget https://apt.puppetlabs.com/puppetlabs-release-trusty.deb
        sudo dpkg -i puppetlabs-release-trusty.deb
        sudo apt-get update
        sudo apt-get install -y puppet=3.8.7*
        mkdir -p /etc/puppet/modules
    fi
    
    for var in "$@" 
    do
        install_module "$var"
    done
    

    Then you can use it from your Vagrantfile like this:

    puppet_modules = [
        'puppetlabs-stdlib:4.16.0',
        'puppetlabs-apt:2.3.0',
        'puppet-nodejs:2.3.0'
        ]
    
    config.vm.provision "shell", path: "puppet/scripts/puppet.sh", args: puppet_modules.join(" ")
    

    It removes puppet from vm if version is lower than 3.8.7, then installs puppet 3.8.7, and then installs all the modules.

    It would probably work only on debian/ubuntu boxes.

    0 讨论(0)
  • 2020-12-12 16:01

    @brain_bacon's strategy worked almost perfectly for me, with one small caveat - if the module already exists, then the provisioning script fails, halting the provisioning process. The following adjustment fixed this:

    config.vm.provision :shell, :run => "always" do |shell|
      shell.inline = %{
        mkdir -p /etc/puppet/modules;
        function install_module {
          folder=`echo $1 | sed s/.*-//`
          if [ ! -d /etc/puppet/modules/$folder ]; then
            puppet module install $1
          fi
        }
        install_module stdlib
        install_module apt
        install_module ruby
      }
    end
    
    0 讨论(0)
提交回复
热议问题