Search code examples
puppet

Puppet: loops for creating file and symlink simultaneously


I would like to parse array and create files and symlink in another directory. I can create files in sites-available and would like to create a symlink in sites-enabled Could you prompt me on how I can do it, please? Can I do at once?

  $domainsnames.each |String $domain| {
    file {"/etc/nginx/sites-available/${domain}.conf":
      # ensure  => link,
      content => template('nginx_on_templates/virtualhost.conf.erb'),
      # target  => "/etc/nginx/sites-enabled/${domain}.conf",
      mode    => '0644',
      owner   => 'root',
      group   => 'root',
    }
  }

Thank in advance, Rostyslav


Solution

  • You probably are managing both sites-available and sites-enabled, and you want to simulate what a2ensite enable <site-name> does, correct?

    In that case, you have something like:

     $domainsnames.each |String $domain| {
        file {"/etc/nginx/sites-available/${domain}.conf":
          ensure  => file,
          content => template('nginx_on_templates/virtualhost.conf.erb'),
          mode    => '0644',
          owner   => 'root',
          group   => 'root',
        }
        file {"/etc/nginx/sites-enabled/${domain}.conf":
          ensure  => link,
          target  => "/etc/nginx/sites-available/${domain}.conf",
        }
      }
    

    I also recommend you taking a look at the puppetlabs/apache module, where you don't have to manage the .conf yourself, but you manage the configurations for your virtual host:

    e.g.:

    apache::vhost { 'user.example.com':
      port          => '80',
      docroot       => '/var/www/user',
      docroot_owner => 'www-data',
      docroot_group => 'www-data',
    }