Advertisement
Guest User

Untitled

a guest
Apr 28th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. In this section, we will create our own learner's NGINX module which can be used to server a couple of websites:
  2.  
  3. `/etc/hosts/nginx/manifests/init.pp`
  4.  
  5. ```
  6. class nginx {
  7.  
  8. package { 'nginx': ensure => installed }
  9.  
  10. service { 'nginx':
  11. ensure => running,
  12. enable => true, # Start the service on system reboot.
  13. require => Package['nginx'] # Forcing the correct order.
  14. }
  15.  
  16. exec { 'reload nginx':
  17. command => '/usr/sbin/service nginx reload',
  18. require => Package['nginx'],
  19. refreshonly => true, # Execute only if some resource has changed.
  20. }
  21.  
  22. }
  23. ```
  24.  
  25.  
  26. `manifests/vhost.pp`
  27.  
  28. ```
  29. class nginx::vhost {
  30.  
  31. $default_parent_root = "/home/ubuntu/nginxsites-puppet"
  32. $dir_tree = [ "$default_parent_root"]
  33. file { $dir_tree :
  34. owner => 'ubuntu',
  35. group => 'ubuntu',
  36. ensure => 'directory',
  37. mode => '777',
  38. }
  39.  
  40.  
  41. define apply( $domain="UNSET",$root="UNSET"){
  42.  
  43. include nginx # Class was declared inside init.pp
  44.  
  45. # Default value overrides
  46.  
  47. if $domain == 'UNSET' {
  48. $vhost_domain = $name
  49. } else {
  50. $vhost_domain = $domain
  51. }
  52.  
  53. if $root == 'UNSET' {
  54. $vhost_root = "$default_parent_root/${name}"
  55. } else {
  56. $vhost_root = $root
  57. }
  58.  
  59.  
  60. # Creating the virtual host conf file out of the template in nginx/templates directory
  61.  
  62. file { "/etc/nginx/sites-available/${vhost_domain}.conf":
  63. content => template('nginx/vhost.erb'), # vhost.erb is present in nginx/templates/
  64. require => Package['nginx'],
  65. notify => Exec['reload nginx'], # Resource was declared in init.pp
  66. }
  67.  
  68. # Enabling the site by creating a sym link from sites-available to sites-enabled
  69.  
  70. file { "/etc/nginx/sites-enabled/${vhost_domain}.conf":
  71. ensure => link,
  72. target => "/etc/nginx/sites-available/${vhost_domain}.conf",
  73. require => File["/etc/nginx/sites-available/${vhost_domain}.conf"],
  74. notify => Exec['reload nginx'],
  75. }
  76.  
  77. addStaticFiles{ "staticfiles-${vhost_root}":
  78. default_parent_root => $default_parent_root,
  79. vhost_root => $vhost_root,
  80. vhost_domain => $vhost_domain
  81. }
  82. }
  83.  
  84. define addStaticFiles( $default_parent_root, $vhost_root , $vhost_domain ){
  85.  
  86. $dir_tree = [ "$vhost_root" ]
  87. file { $dir_tree :
  88. owner => 'ubuntu',
  89. group => 'ubuntu',
  90. ensure => 'directory',
  91. mode => '777',
  92. }
  93. -> # This arrow ensures that the dir structure is created first.
  94. file { ["$vhost_root/index.html"]:
  95. owner => 'ubuntu',
  96. group => 'ubuntu',
  97. source => "puppet:///modules/nginx/${vhost_domain}/index-html", # index.html was dropped under nginx/files/
  98. mode => '755',
  99. }
  100. }
  101. }
  102. ```
  103.  
  104. `/etc/puppet/manifests/site.pp`
  105.  
  106.  
  107. ```
  108. node vagrant-ubuntu-trusty-32 {
  109. nginx::vhost::apply{ "web1":
  110. domain => "site1.puppet.sbose.in",
  111. root => "/home/ubuntu/site1"
  112. }
  113. nginx::vhost::apply{"web2":
  114. domain => "site2.puppet.sbose.in",
  115. root => "/home/ubuntu/site2"
  116.  
  117. }
  118. }
  119. ```
  120.  
  121. Inside `/etc/puppet/modules/nginx/files` create a `site1` and `site2` directory which would have respective `index-html` files.
  122.  
  123.  
  124. Now - the icing on the cake.. manually add the `/etc/hosts` entries.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement